1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights.

Slides:



Advertisements
Similar presentations
Introduction to Computation and Problem Solving Class 32: The Java® Collections Framework Prof. Steven R. Lerman and Dr. V. Judson Harward.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
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.
Collections CS3250. Sources  Slides by Professor Chuck Allison  Core Java, by Cay S. Horstmann and Gary Cornell  The Java Tutorial 
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
15-Jun-15 Lists in Java Part of the Collections Framework.
© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)
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.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 17 Advanced Java Concepts Data Structure Support.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
05 - Containers DRAFT COPY © S. Uchitel, 2004 Container OrderedDuplicates BagsNOYES SetsNONO ListsYESYES MapsNO.
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 Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
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.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities Chapter 4.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
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.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
111 © 2002, Cisco Systems, Inc. All rights reserved.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
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.
1 Java Collection: Data structure framework. 2 Background collections store and organize objects for efficient access Java collections: traditional data.
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
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:
Sadegh Aliakbary Sharif University of Technology Fall 2012.
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
©SoftMoore ConsultingSlide 1 Java Collections Framework.
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.
Set and Map IS 313, Skeletons  Useful coding patterns  Should understand how and why they work how to use  possible quiz material.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
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/20/05A-1 © 2001 T. Horton CS 494 Adv. SW Design and Development A Tasting…. Course 1: Design patterns: Intro, example Course 2: Inheritance, Interfaces,
1 Collections. 2 Concept A collection is a data structure – actually, an object – to hold other objects, which let you store and organize objects in useful.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
University of Limerick1 Collections The Collection Framework.
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
Collections Dwight Deugo Nesa Matic
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
©Karsten Lundqvist Example: Array and Arrays 1 import java.util.*; public class Example { private int intValues[] = { 1, 2, 3, 4, 5, 6 }; private double.
Java Collections CHAPTER 3-February-2003
Java Collections OOP tirgul No
Fundamental of Java Programming
Efficiency of in Binary Trees
Programming & Data Structures
Introduction to Collections
Introduction to Collections
Part of the Collections Framework
Collections in Java The objectives of this lecture are:
Introduction to Collections
Introduction to Collections
Java Generics & Iterators
Set and Map IS 313,
Presentation transcript:

1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights reserved. Licensed to Advanced Web Technologies Modified by Habib Rostami.Lecture 9

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.2 Interface-based design Separate interface from implementationSeparate interface from implementation Built in to Java languageBuilt in to Java language PolymorphismPolymorphism –List l = new LinkedList(); –Calling l.add() invokes method of class LinkedList

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.3 Collections Framework Interoperability between unrelated APIsInteroperability between unrelated APIs Reduces the effort required to learn APIsReduces the effort required to learn APIs Reduces the effort required to design and implement APIsReduces the effort required to design and implement APIs Fosters software reuseFosters software reuse

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.4 Project Goals Small APISmall API –Number of interfaces –Number of methods per interface –Low "conceptual weight" Builds on existing Java collections (Vector, Hashtable)Builds on existing Java collections (Vector, Hashtable) Interconvertible with Java arraysInterconvertible with Java arrays

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.5 Interface-Based Design –interface List {…} –class LinkedList implements List {…} –… –List l = new LinkedList(); –l.add( new Date() ); –Date d = (Date)l.get(0);

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.6 Overview: Core Interfaces CollectionCollection SetSet ListList MapMap SortedSetSortedSet SortedMapSortedMap

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.7 Overview: Utilities Utility InterfacesUtility Interfaces –Comparator –Iterator Utility ClassesUtility Classes –Collections –Arrays

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.8 Collection A group of objectsA group of objects Major methods:Major methods: –int size(); –boolean isEmpty(); –boolean contains(Object); –Iterator iterator(); –Object[] toArray(); –boolean add(Object); –boolean remove(Object); –void clear();

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.9 Set interface Set extends Collectioninterface Set extends Collection An unordered collection of objectsAn unordered collection of objects No duplicate elementsNo duplicate elements Same methods as CollectionSame methods as Collection –Semantics are different, so different interface needed for design Implemented by:Implemented by: –HashSet, TreeSet

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.10 List interface List extends Collectioninterface List extends Collection An ordered collection of objectsAn ordered collection of objects Duplicates allowedDuplicates allowed

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.11 List Details Major additional methods:Major additional methods: –Object get(int); –Object set(int, Object); –int indexOf(Object); –int lastIndexOf(Object); –void add(int, Object); –Object remove(int); –List subList(int, int); add() inserts remove() deletes Implemented by:Implemented by: –ArrayList, LinkedList, Vector

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.12 Map interface Map (does not extend Collection)interface Map (does not extend Collection) An object that maps keys to valuesAn object that maps keys to values Each key can have at most one valueEach key can have at most one value Replaces java.util.Dictionary interfaceReplaces java.util.Dictionary interface Ordering may be provided by implementation class, but not guaranteedOrdering may be provided by implementation class, but not guaranteed

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.13 Map Details Major methods:Major methods: –int size(); –boolean isEmpty(); –boolean containsKey(Object); –boolean containsValue(Object); –Object get(Object); –Object put(Object, Object); –Object remove(Object); –void putAll(Map); –void clear(); Implemented by:Implemented by: –HashMap, Hashtable, ….

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.14 Accessing all members of Map MethodsMethods –Set keySet(); –Collection values(); –Set entrySet(); Map.EntryMap.Entry –Object that contains a key-value pair getKey(), getValue()

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.15 Iterator Represents a loopRepresents a loop Created by Collection.iterator()Created by Collection.iterator() Similar to EnumerationSimilar to Enumeration –Improved method names –Allows a remove() operation on the current item

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.16 Iterator Methods boolean hasNext()boolean hasNext() –Returns true if the iteration has more elements Object next()Object next() –Returns next element in the iteration void remove()void remove() –Removes the current element from the underlying Collection

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.17 ListIterator interface ListIterator extends Iteratorinterface ListIterator extends Iterator Created by List.listIterator()Created by List.listIterator() Adds methods toAdds methods to –traverse the List in either direction –modify the List during iteration Methods added:Methods added: –hasPrevious(), previous() –nextIndex(), previousIndex() –set(Object), add(Object)

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.18 Set Implementations HashSetHashSet –a Set backed by a hash table TreeSetTreeSet –A balanced binary tree implementation –Imposes an ordering on its elements

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.19 List Implementations ArrayListArrayList –a resizable-array implementation like Vector LinkedListLinkedList –a doubly-linked list implementation –May provide better performance than ArrayList if elements frequently inserted/deleted within the List –For queues and double-ended queues (deques) VectorVector –a resizable-array implementation of a List

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.20 Map Implementations HashMapHashMap –A hash table implementation of Map –Like Hashtable, but supports null keys & values TreeMapTreeMap –A balanced binary tree implementation –Imposes an ordering on its elements HashtableHashtable –Synchronized hash table implementation of Map interface, with additional "legacy" methods.

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.21 Sorting Collections.sort() static methodCollections.sort() static method SortedSet, SortedMap interfacesSortedSet, SortedMap interfaces –Collections that keep their elements sorted –Iterators are guaranteed to traverse in sorted order Ordered Collection ImplementationsOrdered Collection Implementations –TreeSet, TreeMap

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.22 Sorting (cont.) Comparable interfaceComparable interface –Must be implemented by all elements in SortedSet –Must be implemented by all keys in SortedMap –Method: int compareTo(Object o) –Defines "natural order" for that object class Comparator interfaceComparator interface –Defines a function that compares two objects –Can design custom ordering scheme –Method: int compare(Object o1, Object o2)

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.23 Sorting (cont.) Total vs. Partial OrderingTotal vs. Partial Ordering –Technical, changes behavior per object class Sorting ArraysSorting Arrays –Use Arrays.sort(Object[]) –Equivalent methods for all primitive types Arrays.sort(int[]), etc.

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.24 Unsupported Operations An implementation class may elect not to support a particular method of the interfaceAn implementation class may elect not to support a particular method of the interface UnsupportedOperationException is a runtime (unchecked) exceptionUnsupportedOperationException is a runtime (unchecked) exception

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.25 Utility Classes Collections classCollections class Static methods:Static methods: –sort(List) –binarySearch(List, Object) –reverse(List) –shuffle(List) –fill(List, Object) –copy(List dest, List src) –min(Collection) –max(Collection) –synchronizedX, unmodifiableX factory methods

Java Collections Framework 1.0tCopyright © 1998 Purple Technology, Inc.26 Utility Classes Arrays classArrays class Static methods that act on Java arrays:Static methods that act on Java arrays: –sort –binarySearch –equals –fill –asList - returns an ArrayList composed of this array's contents