Presentation is loading. Please wait.

Presentation is loading. Please wait.

Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.

Similar presentations


Presentation on theme: "Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010."— Presentation transcript:

1 Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010

2 Core Collection Interfaces Set: A collection that cannot contain duplicate elements. List: An ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of a List generally has precise control over where in the List each element is inserted, and can access elements by their integer index (position). Queue: A collection used to hold multiple elements prior to processing. Map: An object that maps keys to values. Maps cannot contain duplicate keys: Each key can map to at most one value.

3 The Hierarchy Collection Set List SortedSet AbstractCollection AbstractList AbstractSet AbstractSequentialList TreeSet HashSet Vector ArrayList LinkedList Stack LinkedHashSet Map SortedMap AbstractMap TreeMap HashMap LinkedHashMap QueueAbstractQueue PriorityQueue

4 Examples… for ( Object o : collection) System.out.println( o); Iterator i = collection.iterator(); while ( i.hasNext() ) System.out.println( i.next() ); import java.util.*; public class FindDups { public static void main( String args[]) { Set s = new HashSet (); for ( String a : args) if ( !s.add( a) ) System.out.println( "Duplicate: " + a); System.out.println( s.size() + " distinct words: “ + s ); } Note use of Set for variable s allows change to TreeSet, for example

5 Examples… Run with… java Freq if it is to be it is up to me to delegate output: 8 distinct words: {to=3, delegate=1, be=1, it=2, up=1, if=1, me=1, is=2} import java.util.*; public class Freq { public static void main( String args[] ) { Map m = new HashMap (); // Initialize frequency table from command line for ( String a : args) { Integer freq = m.get(a); m.put( a, (freq == null ? 1 : freq + 1) ); } System.out.println( m.size() + " distinct words:“ ); System.out.println(m); } Try changing the implementation to TreeMap or LinkedHashMap

6 Collections class… public static void swap( List a, int i, int j) { E tmp = a.get(i); a.set( i, a.get(j) ); a.set( j, tmp); } import java.util.*; public class Shuffle { public static void main( String args[]) { List list = new ArrayList (); for( String a : args) list.add( a); Collections.shuffle( list, new Random()); System.out.println( list); } public static void shuffle( List list, Random rnd) { for( int i = list.size(); i > 1; i--) swap( list, i - 1, rnd.nextInt(i) ); } Can also… sort, reverse, fill, copy, swap, addAll, retainAll, … and binarySearch on sorted list.. int pos = Collections.binarySearch( list, key); if (pos < 0) l.add(-pos-1);

7 From & To Arrays… // ********************************************************************* // Create List from an Array // - asList uses original array, or use new to construct new copy. // ********************************************************************* String[] animals = { "dog", "cat", "mouse", "mouse", "elephant", "horse", "camel"}; // List list = Arrays.asList( "dog", "cat", "mouse", "elephant", "horse", "camel"); // List list = Arrays.asList( animals); // List list = new ArrayList ( Arrays.asList( animals) ); List list = new LinkedList ( Arrays.asList( animals) ); System.out.println( list); // ********************************************************************* // Convert List back to Array // ********************************************************************* // Object[] zoo = list.toArray(); String[] zoo = list.toArray( new String[0] ); for ( String creature : zoo) System.out.println( creature);

8 Polymorphic Algorithms… // ********************************************************************* // Use some of the polymorphic List algorithms // ********************************************************************* // Collections.sort( list, Collections.reverseOrder() ); Collections.sort( list ); System.out.println( list); String key = "giraffe"; int pos = Collections.binarySearch( list, key); if ( pos < 0) { System.out.println( "\"" + key + "\" not found.. adding"); list.add(-pos-1, key); } else System.out.println( "\"" + key + "\" found at " + pos); Collections.reverse( list); Collections.shuffle( list);

9 Sets… // ********************************************************************* // Sets cannot have duplicate elements // - HashSet (unordered), LinkedHashSet (order added), TreeSet (ordered) // ********************************************************************* // Set s = new HashSet (); // Set s = new LinkedHashSet (); Set s = new TreeSet (); for (String a : animals) if ( !s.add( a) ) System.out.println( "Duplicate detected: " + a); System.out.println( s.size() + " distinct words: " + s);

10 Arrays class… // ********************************************************************* // The Arrays class provides some useful methods too... // ********************************************************************* String[] animals = { "dog", "cat", "mouse", "mouse", "elephant", "horse", "camel"}; System.out.println( animals);// doesn't work! System.out.println( Arrays.toString(animals));// does work! Arrays.sort( animals); System.out.println( Arrays.toString(animals));// after sorting, can search... System.out.println( "\"elephant\" is at " + Arrays.binarySearch( animals, "elephant") );


Download ppt "Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010."

Similar presentations


Ads by Google