import java.util.*; // A brief review of how to use, iterate through, and // remove from sets/maps. public class SetMapReview { public static void main(String[] args) { // Create a set of Strings, add some names Set colors = new HashSet(); colors.add("red"); colors.add("yellow"); colors.add("blue"); colors.add("orange"); colors.add("new blue"); // Print out each string on its own line // Iterator: // hasNext // next // remove Iterator colorsIt = colors.iterator(); while (colorsIt.hasNext()) { String color = colorsIt.next(); System.out.println(color); } System.out.println(); System.out.println(); // Remove strings starting with 'o' Iterator otherIt = colors.iterator(); while (otherIt.hasNext()) { String color = otherIt.next(); if (color.startsWith("o")) { otherIt.remove(); } } // print out what the set now contains for (String color : colors) { System.out.println(color); } // Create a map of the strings to their length. // HashMap : unsorted, but way faster // TreeMap : sorted Map lengths = new HashMap(); for (String color : colors) { lengths.put(color, color.length()); } System.out.println(); System.out.println(); // Print out each mapping of name to its length //System.out.println(lengths); for (String color : lengths.keySet()) { System.out.println("The length of " + color + " is..." + lengths.get(color)); } System.out.println(); System.out.println(); // of type Set System.out.println("Keys in my map: " + lengths.keySet()); } }