Think in Java (8): Holding object, thinkjava
1. Differences between various maps
HashMap: Does not store its elements in any obvious Order (same as HashSet)
TreeMap: stores keys in ascending order of comparison results (same as TreeSet)
LinkedHashMap: stores keys in the insert Order (same as LinkedHashSet)
2. ListIterator is a more powerful sub-type of Iterator. Iterator can only move forward, but ListIterator can move in two directions.
Public class ListIteration {public static void main (String [] args) {List <Pet> pets = Pets. arrayList (8); ListIterator <Pet> it = pets. listIterator (); // Forwards: while (it. hasNext () {System. out. print (it. next () + "," + it. nextIndex () + "," + it. previusindex () + ";") ;}// Backwards: while (it. hasPrevious () {System. out. print (it. previous (). id () + "");}}}
3. The elements in the TreeSet are sorted in ascending Lexicographic Order by default. Of course, we can input a comparator to customize the sorting.
Public class UniqueWordsAlphabetic {public static void main (String [] args) {Set <String> words = new TreeSet <String> (String. CASE_INSENSITIVE_ORDER); words. add ("a"); words. add ("c"); words. add ("B"); System. out. println (words );}}