import java.util.*; class CollectionTest { public static void main(String[] args) { LinkedList list = Story.getTextAsLinkedList(); // ex a & b System.out.println("# words: " + list.size()); System.out.println("word at pos 45: " + list.get(45)); // ex c Set set = new HashSet(list); System.out.println("\n# different words: " + set.size()); // ex d // The iterator of treeset will return the elements in // alphabetic order, as it will use the order of the // underlying elementtype - here String. Set tSet = new TreeSet(list); System.out.println("\n\nAll the words in alphabetic order\n---------------------------------"); Iterator it = tSet.iterator(); while(it.hasNext()) System.out.print(it.next() + " "); // or alternatively - with the for-loop System.out.println("\n\nAll the words in alphabetic order again\n---------------------------------"); for(String s: tSet) System.out.print( s + " "); // ex e System.out.println("\n\nPrinting all the words and frequency\n------------------------------------"); SortedMap map = new TreeMap(); // first add all elements to a map, // where the key is the word so identical words become identical keys for( String word: list) { int value = 0; if(map.containsKey(word)) value = map.get(word); // get the count map.put(word, value+1); } // now print all keys (the words) and their counts... // The for loop might look a bit cryptical. What it says is // For each entry en the entrySet of the map do the body. // Map.Entry is the type of each entry for( Map.Entry entry: map.entrySet() ) System.out.print("["+ entry.getKey() + ", " + entry.getValue()+"] "); // f // iterate over the list and ... System.out.println("\nprint all words with size 6 or greater\n"); for(String word: list) if (word.length()>=6) System.out.print(word + " "); } }