https://github.com/mukul273/java-collections
Java Collections for beginners
https://github.com/mukul273/java-collections
collections git github java
Last synced: about 2 months ago
JSON representation
Java Collections for beginners
- Host: GitHub
- URL: https://github.com/mukul273/java-collections
- Owner: mukul273
- Created: 2017-03-18T05:29:10.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-03-21T19:43:18.000Z (over 9 years ago)
- Last Synced: 2025-02-15T19:49:07.342Z (over 1 year ago)
- Topics: collections, git, github, java
- Language: Java
- Size: 1.13 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
This is for Basic understanding of Java Collections Framework.
/*
* Consider:
* 1. what you need the collection to do
* 2. are you using the fastest collection for your purposes
* (think about insertion/deletion, retrieval and traversal
*/
//////////////// LISTS ///////////////////////////////////
// Store lists of objects
// Duplicates are allowed
// Objects remain in order
// Elements are indexed via an integer
// cf. shopping list
// Checking for particular item in list is slow
// Looking an item up by index is fast
// Iterating through lists is relatively fast
// Note: you can sort lists if you want to.
// If you only add or remove items at end of list, use ArrayList.
List list1 = new ArrayList();
// Removing or adding items elsewhere in the list?
List list2 = new LinkedList();
////////////////SETS ///////////////////////////////////
// Only store unique values
// Great for removing duplicates
// Not indexed, unlike lists
// Very fast to check if a particular object exists
// If you want to use your own objects, you must implement hashCode() and equals().
// Order is unimportant and OK if it changes?
// HashSet is not ordered.
Set set1 = new HashSet();
// Sorted in natural order? Use TreeSet - must implement Comparable for custom types
// (1,2,3 ..., a,b,c.... etc)
Set set2 = new TreeSet();
// Elements remain in order they were added
Set set3 = new LinkedHashSet();
////////////////MAPS ///////////////////////////////////
// Key value pairs.
// Like lookup tables
// Retrieving a value by key is fast
// Iterating over map values is very slow
// Maps not really optimised for iteration
// If you want to use your own objects as keys, you must implement hashCode() and equals().
// Keys not in any particular order, and order liable to change.
Map map1 = new HashMap();
// Keys sorted in natural order - must implement Comparable for custom types
Map map2 = new TreeMap();
// Keys remain in order added
Map map3 = new LinkedHashMap();
// There are also the SortedSet and SortedMap interfaces.