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

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
Advertisements

Java Programming: Advanced Topics 1 Collections and Utilities.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
JAVA Programming (Session 7) When you are willing to make sacrifices for a great cause, you will never be alone. Instructor:
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Collections CS3250. Sources  Slides by Professor Chuck Allison  Core Java, by Cay S. Horstmann and Gary Cornell  The Java Tutorial 
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 21 Generics.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
15-Jun-15 Lists in Java Part of the Collections Framework.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
1 L42 Collections (2). 2 OBJECTIVES  To use collections framework algorithms to manipulate  search  sort  and fill collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
12-Jul-15 Lists in Java Part of the Collections Framework.
The Java Collections Package C. DeJong Fall 2001.
Chapter 19 Java Data Structures
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
Java's Collection Framework
Collections. Why collections? Collections are used to hold a collection of objects. List holds objects based on order of insertion and can hold non unique.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
111 © 2002, Cisco Systems, Inc. All rights reserved.
COLLECTIONS Byju Veedu s/Collection.html.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
Chapter 18 Java Collections Framework
Computer Science 209 Software Development Java Collections.
תוכנה 1 תרגול 8 – מבני נתונים גנריים. 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework.
Collections –data structures and Algorithms L. Grewe.
Data structures and algorithms in the collection framework 1.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Arrays, ArrayLists, and Collections. Rationale Suppose we have a program to compute the average of three numbers. We could write a simple little method.
The Java Collections Framework Based on
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
Java 2 Collections Bartosz Walter Software Engineering II.
1 Interfaces in Java’s Collection Framework Rick Mercer.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
13 Collections Framework. 2 Contents What is Collection? Collections Framework Collections Hierarchy Collections Implementations Set List Map.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Priority Queues. Priority Queue ADT A priority queue stores a collection of entries Each entry is a pair (key, value) Main methods of the Priority Queue.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
University of Limerick1 Collections The Collection Framework.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
©Karsten Lundqvist Example: Array and Arrays 1 import java.util.*; public class Example { private int intValues[] = { 1, 2, 3, 4, 5, 6 }; private double.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Java Collections OOP tirgul No
Chapter 19 Java Data Structures
Software Development Java Collections
University of Central Florida COP 3330 Object Oriented Programming
Môn: Lập trình Hướng đối tượng (Object Oriented Programming)
Collections Not in our text.
Collections Framework
CSE 1020: The Collection Framework
CS 240 – Advanced Programming Concepts
Presentation transcript:

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

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.

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

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

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

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);

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);

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);

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);

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") );