SCJPTps Collections
SCJPTps Collections
. Iterator(). List index based methods. add(int, Object), set(int, Object). Iterator(), listIterator() Set unique(equals() ). No new methods. Queue priority Collection Classes List Implementation ArrayList - Implements List LinkedList implements List, Queue, Deque interace Vector legacy. All methods synchronized. Cant add null. Implements List Set Implementation HashSet unique(equals() ), no ordering LinkedHashSet unique(equals() ), insertion order TreeSet unique(equals() ), sorted order (Comparable/Comparator), implements NavigableSet Queue implementation PriorityQueue - sorted order (Comparable/Comparator), will show order only in methods and not in iterator() or System.out.println() Map Interface Map Keys unique, key-value pair, put(key,value), get(key), putting duplicate key will replace old value, multiple null values allowed Map m = new HashMap (); Set<Map.Entry> entrySet() Collection values() Set keySet() Map<String, Integer> m = new HashMap<String, Integer>(); Set<Map.Entry<String, Integer> > entrySet()
Collection<Integer> values() Set<String> keySet() Map Implementation Classes HashMap Keys unique, 1 null key allowed, no ordering LinkedHashMap - Keys unique, 1 null key allowed, insertion order TreeMap - Keys unique, adding null key will compile but throw NullPointerException at run time Hashtable legacy class, methods synchronized, Keys unique, no null key or value allowed, no ordering Sorting (Comparable / Comparator) NavigableSet interface (TreeSet class) Constructors: TreeSet() TreeSet(Comparator) // Comparable interface // Provide Comparator object
NavigableMap interface (TreeMap class) Constructors: TreeMap() TreeMap(Comparator) // Comparable interface // Provide Comparator object
Comparable - compareTo(Object) Comparator - compare(Object, Object) String and Wrapper classes implement Comparable and equals() method StringBuffer/Builder do NOT implement Comparable and equals() method List li = new ArrayList(); for(Object : li) {} Iterator it = li.iterator() ListIterator it = li.listIterator() // legal // legal // legal
List<String> li = new ArrayList<String> (); for(String : li) {} Iterator<String> it = li.iterator() ListIterator<String> it = li.listIterator() // legal // legal // legal