AccessibleStreamable Callable Cloneable Closeable Compilable Destroyable Externalizable Flushable Formattable Invocable ItemSelectable Iterable JMXAddressable Joinable Pageable Printable Readable Referenceable Refreshable Runnable Scrollable StateEditable Streamable Transferable TypeVariable
for(Object obj : col) { System.out.println(obj); }
// Create and populate array list. ArrayList<Object> col = new ArrayList<Object>( ); col.add(5); col.add("dog"); col.add(false); System.out.println(col); // Obtain and use iterator from array list. Iterator itr = col.iterator( ); while (itr.hasNext( )) { Object element = itr.next( ); System.out.println(element); }
ArrayList HashMap HashSet ArrayDequeue LinkedList LinkedHashSet LinkedHashMap TreeSet TreeMap
import java.util.ArrayList; import java.util.Calendar; import java.util.Iterator; public class Test { public static void main(String[] args) { ArrayList<Dog> col = new ArrayList<Dog>( ); Calendar c1 = Calendar.getInstance( ); c1.set(2017, 10,17); Dog d1 = new Dog("Molly", 'F', "Golden Lab", c1, 1111, true); System.out.println(d1); Calendar c2 = Calendar.getInstance( ); c1.set(2017, 8,15); Dog d2 = new Dog("Sushi", 'M', "Pomeranian", c2, 2222, false); System.out.println(d2); Calendar c3 = Calendar.getInstance( ); c3.set(2012, 3,5); Dog d3 = new Dog("Sasha", 'F', "Rough Collie", c3, 3333, false); System.out.println(d3); col.add(d1); col.add(d2); col.add(d3); // Implicit Iterator for(Dog d : col) { System.out.println(d); } // Explicit Iterator Iterator<Dog> itr = col.iterator( ); while (itr.hasNext( )) { Dog element = itr.next( ); System.out.println(element); } } }
Key | Value | Value Datatype |
---|---|---|
"C111" | "apple" | String |
"C222" | 194 | Integer |
"C333" | 3.14159 | Double |
"C444" | 'W' | Character |
"C555" | true | Boolean |
import java.util.HashMap; public class Main { public static void main(String[] args) { HashMap<String, Object> hm = new HashMapS<tring, Object>(50, 0.5f); hm.put("C111", "apple"); hm.put("C222", 194); hm.put("C333", 3.14159); hm.put("C444", 'W'); hm.put("C555", true); System.out.println(hm); System.out.println(hm.get("C444")); } }
import java.util.Calendar; import java.util.HashMap; public class Main { public static void main(String[] args) { Calendar c1 = Calendar.getInstance( ); c1.set(2017, 10,17); Dog d1 = new Dog("Molly", 'F', "Golden Lab", c1, 1111, true); //System.out.println(d1); Calendar c2 = Calendar.getInstance( ); c1.set(2017, 8,15); Dog d2 = new Dog("Sushi", 'M', "Pomeranian", c2, 2222, false); //System.out.println(d2); Calendar c3 = Calendar.getInstance( ); c3.set(2012, 3,5); Dog d3 = new Dog("Sasha", 'F', "Rough Collie", c3, 3333, false); HashMap<Integer, Dog> col = new HashMap<Integer, Dog>(50, 0.5f); col.put(1111, d1); col.put(2222, d2); col.put(3333, d3); System.out.println(col); } }
hashCode = socialSecurityNumber % 50Fourteen sets of initials are stored in the hashtable of size 50, so the load factor is 14 / 50 = 28%.