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
 22: 
 23: import java.util.*;
 24: 
 25: class CollectionsDemo
 26: {
 27:    public static void main ( String [] args )
 28:    {
 29:       List<String> names = new ArrayList<String>();
 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: " +
 40:          Arrays.deepToString( names.toArray() ) );
 41: 
 42:       if ( names.contains("Sam") )  // Note, "contains" uses "equal".
 43:       {
 44:          int i = names.indexOf( "Sam" );  // returns -1 if not found
 45:          String s = names.get( i );
 46:          System.out.println( "\nIndex of \"" + s + "\" is "
 47:             + i + "." );
 48: 
 49:          System.out.println( "Removing \"Sam\"." );
 50:          names.remove( i );  // names.remove( "Sam" ) works too.
 51:          System.out.println( "Num of remaining names: "
 52:             + names.size() );
 53: 
 54:          System.out.println( "Sorting names.\n" );
 55:          Collections.sort( names );
 56: 
 57:          System.out.println( "The remaining names are:\n" );
 58:          for ( String name : names )
 59:             System.out.println( name );
 60: 
 61:          // Using an Iterator: needed when altering collection:
 62:          Iterator<String> it = names.iterator();
 63:          while ( it.hasNext() )
 64:          {
 65:             String name = it.next();
 66:             if ( name.endsWith( "e" ) )
 67:             {  System.out.println( "Removing \"" + name + "\"" );
 68:                it.remove();
 69:             }
 70:          }
 71:          System.out.println( "\nThe remaining names are:\n" );
 72:          for ( String name : names )
 73:             System.out.println( name );
 74: 
 75:          names.clear();  // removes all elements
 76:       }
 77: 
 78:        // Demo of creating and using a Map:
 79:        Map<String, Integer> m = new HashMap<>();
 80:        m.put( "January", 31 );       m.put( "February", 28 );
 81:        m.put( "March", 31 );         m.put( "April", 30 );
 82:        m.put( "May", 31 );           m.put( "June", 30 );
 83:        m.put( "July", 31 );          m.put( "August", 31 );
 84:        m.put( "September", 30 );     m.put( "October", 31 );
 85:        m.put( "November", 30 );      m.put( "December", 31 );
 86: 
 87:        int days = m.get( "July" );  // Try changing July to "Jully"!
 88: 
 89:        System.out.println( "\nDays in July: " + days );
 90: 
 91:        // Note that Maps don't preserve key order, so sort the keys:
 92:        System.out.println( "\nMap (sorted keys):" );
 93:        List<String> keys = new ArrayList<>( m.keySet() );
 94:        Collections.sort( keys );
 95:        for ( String key : keys ) {
 96:           System.out.println( key + " has " + m.get(key) + " days." );
 97:        }
 98: 
 99:        // Dumping the Map again, this time using an iterator unsorted):
100:        System.out.println( "\nMap (unsorted keys):" );
101:        Iterator<String> it = m.keySet().iterator();
102:        while ( it.hasNext() ) {
103:           String key = it.next();
104:           System.out.println( key + " has " + m.get(key) + " days." );
105:        }
106:    }
107: }