CollectionsDemo.java
Download CollectionsDemo.java
1: // Demo of ArrayList and Map, showing how to create them, add objects,
2: // search for objects, remove objects, and iterate over them.
3: //
4: // Collections hold objects; you must "wrap" primitive types to add
5: // them. This is easier since Java 5, which automatically converts
6: // primitives to their wrapper class and back, as needed ("autoboxing").
7: // Another Java 5 feature is "generics", which allows you to create a
8: // collection of elements of a specific type. If you do this, you can
9: // avoid using "cast" operators.
10: //
11: // Collections have a "capacity", which is the amount of memory
12: // reserved for it. Adding new elements to a collection is very
13: // efficient if the current size is less than the current capacity.
14: // Otherwise the collection must be grown.
15: //
16: // Collections can be converted to arrays and vice-versa. Using the
17: // utility methods in java.util.Collections an ArrayList can be
18: // easily searched, sorted, reversed, etc. (Note shown here.)
19: //
20: // Written 3/2005 by Wayne Pollock, Tampa Florida USA.
21: // Updated 2015, 2017
22:
23: import java.util.*;
24:
25: class CollectionsDemo
26: {
27: public static void main ( String [] args )
28: {
29: List<String> names = new ArrayList<>();
30:
31: names.add( new String( "Jane" ) );
32: names.add( new String( "Anna" ) );
33: names.add( new String( "Hiro" ) );
34: names.add( new String( "Kim" ) );
35: names.add( new String( "Sam" ) );
36: names.add( new String( "Wayne" ) );
37:
38: // Quick and dirty way to print a collection:
39: System.out.println( "Initial contents: " + names );
40: // For collections of collections print them this way:
41: // Arrays.deepToString( names.toArray() ) );
42:
43: if ( names.contains("Sam") ) // Note, "contains" uses "equal".
44: {
45: int i = names.indexOf( "Sam" ); // returns -1 if not found
46: String s = names.get( i );
47: System.out.println( "\nIndex of \"" + s + "\" is "
48: + i + "." );
49:
50: System.out.println( "Removing \"Sam\"." );
51: names.remove( i ); // names.remove( "Sam" ) works too.
52: System.out.println( "Num of remaining names: "
53: + names.size() );
54: }
55:
56: System.out.println( "Sorting names.\n" );
57: Collections.sort( names );
58:
59: System.out.println( "The remaining names are:\n" );
60: for ( String name : names )
61: System.out.println( name );
62:
63: // Using an Iterator: needed when altering collection:
64: Iterator<String> it = names.iterator();
65: while ( it.hasNext() )
66: {
67: String name = it.next();
68: if ( name.endsWith( "e" ) )
69: { System.out.println( "Removing \"" + name + "\"" );
70: it.remove();
71: }
72: }
73:
74: System.out.println( "\nThe remaining names are:\n" );
75: for ( String name : names )
76: System.out.println( name );
77:
78: names.clear(); // removes all elements
79:
80: // Demo of creating and using a Map:
81: Map<String, Integer> m = new HashMap<>();
82: m.put( "January", 31 ); m.put( "February", 28 );
83: m.put( "March", 31 ); m.put( "April", 30 );
84: m.put( "May", 31 ); m.put( "June", 30 );
85: m.put( "July", 31 ); m.put( "August", 31 );
86: m.put( "September", 30 ); m.put( "October", 31 );
87: m.put( "November", 30 ); m.put( "December", 31 );
88:
89: int days = m.get( "July" );
90:
91: System.out.println( "\nDays in July: " + days );
92:
93: // Since Java 8, there must be a dozen ways to iterate over a
94: // collection. I show four of them here. Generally speaking,
95: // the forEach way is most efficient. Note these methods work
96: // for any type of collection; Maps are actually the most difficult.
97:
98: // Since Maps don't preserve key order, sort the keys first:
99: System.out.println( "\nMap (sorted keys):" );
100: List<String> keys = new ArrayList<>( m.keySet() );
101: Collections.sort( keys );
102: // Dumping the sorted Map using a for-each loop:
103: for ( String key : keys ) {
104: System.out.println( key + " has " + m.get(key) + " days." );
105: }
106:
107: // Dumping the unsorted Map again, this time using an iterator):
108: System.out.println( "\nMap (unsorted keys) via iterator:" );
109: Iterator<String> iter = m.keySet().iterator();
110: while ( iter.hasNext() ) {
111: String key = iter.next();
112: System.out.println( key + " has " + m.get(key) + " days." );
113: }
114:
115: // Dumping the unsorted Map again, this time using forEach):
116: System.out.println( "\nMap (unsorted keys) via forEach:" );
117: m.forEach ( (k,v) -> System.out.println( k + " has " + v + " days." ) );
118:
119: // Dumping the unsorted Map again, this time using a stream):
120: System.out.println( "\nMap (unsorted keys) via a stream:" );
121: m.entrySet().stream().forEach(e ->
122: System.out.println( e.getKey() + " has " + e.getValue() + " days." )
123: );
124:
125: // This runs:
126: Integer numDays = m.get( "Pollockuary" );
127: System.out.println( "\nDays in Pollockuary: " + numDays );
128:
129: // This would fail with a NullPointerException:
130: // int days = m.get( "Pollockuary" );
131: }
132: }