import java.util.*; import java.io.*; public class Ch22_1 // demonstrating three types of containers, ArrayList, PriorityQueue, LinkedList { public static void main(String[] args) throws FileNotFoundException { ArrayList a=new ArrayList<>(); PriorityQueue b=new PriorityQueue<>(); LinkedList c=new LinkedList<>(); File f=new File("Ch22_1.txt"); // the problem calls for obtaining the file from command line, here we hardcode it Scanner in=new Scanner(f); String nextWord; while(in.hasNext()) // while the file has words { nextWord=in.next(); // get the next word (String) a.add(nextWord); // add it to each container b.add(nextWord); c.add(nextWord); } for(String temp:a) // print out each container, notice that the words are output System.out.print(temp+" "); // in the order input, not sorted System.out.println("\n\n"); for(String temp:b) // this is not in the order input, but neither is it sorted! System.out.print(temp+" "); System.out.println("\n\n"); for(String temp:c) System.out.print(temp+" "); System.out.println("\n\n"); Collections.sort(a); // sort containers a and c, we don't have to sort b Collections.sort(c); for(String temp:a) // print out a, now sorted System.out.print(temp+" "); System.out.println("\n\n"); while(b.size()>0) // we print b out by removing each element, which removes the priority element System.out.print(b.remove()+" "); // giving us the output in sorted order System.out.println("\n\n"); for(String temp:c) // print out c, now sorted System.out.print(temp+" "); } }