The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 7 Object Oriented Programming in Java Advanced Topics Collection.
Using Maps. A simple map: Hashtable To create a Hashtable, use: import java.util.*; Hashtable table = new Hashtable(); To put things into a Hashtable,
15-Jun-15 Lists in Java Part of the Collections Framework.
Professor Evan Korth (adapted from Sun’s collections documentation)
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  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.
Collections A First Glimpse. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector is a kind of collection.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 The Collection Interface public interface Collection extends Iterable { boolean add(E e); boolean addAll(Collection c); void clear(); boolean contains(Object.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 17 Advanced Java Concepts Data Structure Support.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
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.
Using Maps. 2 A simple map: Hashtable To create a Hashtable, use: import java.util.*; Hashtable table = new Hashtable(); To put things into a Hashtable,
Java's Collection Framework
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
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.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
The Java Collections Framework Based on
CSC 142 P 1 CSC 142 Collections [Reading: Chapter 10]
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 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
SETS AND MAPS Collections of Data. Advanced Data Structures Often referred to as the Java Collections Framework…. Set and map data types Hash tables Binary.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
Maps Nick Mouriski.
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.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
4-Mar-16 Introduction to Collections. Revision questions True false questions 0 for False 1 for True Please do not answer anything other than the above.
Collections Dwight Deugo Nesa Matic
C19: Collection Classes (don’t forget to look at all the online code examples)
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Java Collections CHAPTER 3-February-2003
Java Collections OOP tirgul No
Fundamental of Java Programming
Efficiency of in Binary Trees
Programming & Data Structures
Using Maps.
Introduction to Collections
Introduction to Collections
JAVA Collections Framework Set Interfaces & Implementations
Introduction to Collections
Introduction to Collections
Collections A First Glimpse.
Part of the Collections Framework
Using Maps.
Collections in Java The objectives of this lecture are:
Introduction to Collections
Collections Framework
L5. Necessary Java Programming Techniques
Introduction to Collections
Collections A First Glimpse 16-Apr-19.
Hashing in java.util
Part of the Collections Framework
Presentation transcript:

The Collections Framework A Brief Introduction

Collections A collection is a structured group of objects –An array is a kind of collection –A Vector is a kind of collection –A linked list is a kind of collection Java 1.2 introduced the Collections Framework and provided many great implementations –Vector s have been redefined to implement Collection –Trees, linked lists, stacks, hash tables, and other classes are implementations of Collection –Arrays do not implement the Collection interfaces

Types of Collection Java supplies several types of Collection : –Set : cannot contain duplicate elements, order is not important –SortedSet : like a Set, but order is important –List : may contain duplicate elements, order is important Java also supplies some “collection-like” things: –Map : a “dictionary” that associates keys with values, order is not important –SortedMap : like a Map, but order is important

The Collections framework There are two groups: Collection s of single elements, and Map s containing key/value pairs These are all interfaces, but there are classes implementing each interface Collection SortedSet ListSet SortedMap Map

Some implementations Each class (in green) implements one or more interfaces (in white), hence implements all the methods declared in those interfaces We will look at the interface methods Collection Set SortedSetHashSet TreeSet List ArrayListLinkedList Vector Stack Map HashtableHashMap SortedMap TreeMap

Collections are ADTs The interfaces of the Collections framework describe the functionality of a data structure but not its implementation Collections are one of the best-designed parts of Java, because –They are elegant: they combine maximum power with maximum simplicity –They are uniform: when you know how to use one, you almost know how to use them all –You can easily convert from one to another

The Collection interface Some simple Collection methods: –int size( ) –boolean isEmpty( ) –boolean contains(Object e) –boolean add(Object e)* –boolean remove(Object e)* –Iterator iterator( ) Some “bulk” Collection methods: –boolean containsAll(Collection c) –boolean addAll(Collection c)* –boolean removeAll(Collection c)* –boolean retainAll(Collection c)* –void clear( ) * Returns true if the Collection was changed, false otherwise

The Set interface A Set is a Collection that does not contain duplicate (“equal”) elements –“Equal” is defined by whatever equals(Object) method applies to the elements of the collection –You need to be sure you have a working equals method –For a HashSet, you also need a working hashCode method The methods of Set are the same as those of Collection, but their effects may vary –Example: If you add an object to a set, and the set already contains that object (as defined by equals ), it will not be added

The SortedSet interface A SortedSet keeps its elements in increasing order Therefore, it must be possible to sort the elements! This means: –The elements must be objects of a type that implement the Comparable interface (alternatively, you need to supply a Comparator when you create the SortedSet ) –Elements must be mutually comparable (e.g. you can’t compare a String to a Button ) –The ordering must be consistent with equals Additional methods include first() and last()

The List interface List implements all the methods defined by Collection, and adds a few of its own: –Object get(int index) –Object set(int index, Object element) –void add(int index, Object element) –Object remove(int index) –boolean addAll(int index, Collection c) –int indexOf(Object o) –int lastIndexOf(Object o) In addition, List supplies a ListIterator that can iterate through the elements forwards or backwards

The Map interface A Map is an object that maps keys to values A map cannot contain duplicate keys –Depending on the implementation, null may or may not be an allowable key Each key can map to at most one value Examples: dictionary, phone book, etc.

Map: Basic operations Object put(Object key, Object value) –Returns the old value for this key, or null if no old value Object get(Object key) Object remove(Object key) boolean containsKey(Object key) boolean containsValue(Object value) int size( ) boolean isEmpty( )

Additional Map methods void putAll(Map t) –Copies one Map into another (adding to whatever is already there) –Example: newMap.putAll(oldMap) void clear() –Example: oldMap.clear() public Set keySet( ) public Collection values( )

A simple map: Hashtable To create a Hashtable, use: import java.util.*; Hashtable table = new Hashtable(); To put things into a Hashtable, use: table.put (key, value ); To retrieve a value from a Hashtable, use: value = ( type )table.get( key );

The SortedMap interface A hash table keeps elements in an (apparently) random order Sometimes you want the keys of a map to be in sorted order (e.g. phone book, dictionary) A map can be implemented with a hash table, but it doesn’t have to be The SortedMap interface implements the Map interface and provides additional methods For efficiency, you want an implementation that keeps its elements in some kind of order

Requirements for SortedMap A SortedMap keeps its elements in the order of increasing key values Therefore, it must be possible to sort the keys! This means: –The keys must be objects of a type that implement the Comparable interface (or the SortedMap must be created with a Comparator ) –Keys must be mutually comparable (e.g. you can’t compare a String to a Button ) –The ordering must be consistent with equals

Some SortedMap Methods Comparator comparator() –Returns the comparator associated with this sorted map, or null if it uses its keys' natural ordering. Object firstKey() –Returns the first (lowest) key currently in this sorted map. Object lastKey() –Returns the last (highest) key currently in this sorted map. There are some other methods, but they are not particularly useful What is more important than these methods is that the Iterator for a SortedMap returns elements in sorted order

Vectors The class Vector has been retrofitted to implement the Collection interface Vector supplies add(Object) and iterator() methods (among others) Let’s look at creating a Vector and iterating through the elements

The Iterator interface interface iterator { // java.lang.util boolean hasNext(); // Returns true if the iteration has more // elements. Object next(); // Returns the next element in the // interation. void remove(); // Removes from the underlying collection // the last element returned by the // iterator (optional operation).

Using a Vector Collection numerals = new Vector(); numerals.add("one"); numerals.add("two"); numerals.add("three"); Iterator iter = numerals.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } Results: one two three

Using a TreeSet Collection numerals = new TreeSet(); numerals.add("one"); numerals.add("two"); numerals.add("three"); Iterator iter = numerals.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } Results: one three two

Constructors Although constructors cannot be defined in an interface, every Java-supplied class that implements Collection has a constructor that takes any other Collection as an argument –Example: Vector myVector = new Vector(myTreeSet); Similarly, most classes that implement the Map interface have a constructor that takes any other Map as an argument –Example: TreeMap myMap = new TreeMap(myHashtable); Thus, it is very easy to convert from one kind of Collection or Map to another

Back to arrays Arrays are not part of the new Collections Framework, but they haven’t been ignored Java 1.2 introduced the new Arrays class, with some useful operations, for example: –static void sort(Object[] a) –static int binarySearch(Object[] a, Object key) –static boolean equals(Object[] a, Object[] a2) –static void fill(Object[] a, Object val) –static void fill(Object[] a, Object val, int from, int to) –static List asList(Object[] a) Although I’ve shown these methods for arrays of Objects, there are corresponding methods for arrays of each of the primitive types

The End