SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.

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.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
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.
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.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
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.
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.
The Java Collections Package C. DeJong Fall 2001.
Chapter 19 Java Data Structures
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
Java's Collection 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.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
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.
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.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Chapter 18 Java Collections Framework
1/20/03A2-1 CS494 Interfaces and Collection in Java.
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.
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:
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
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.
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.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
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
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
Chapter 19 Java Data Structures
Programming & Data Structures
Introduction to Collections
Introduction to Collections
Introduction to Collections
Part of the Collections Framework
Collections in Java The objectives of this lecture are:
Introduction to Collections
Collections Framework
Introduction to Collections
Hashing in java.util
Set and Map IS 313,
Presentation transcript:

SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework

SEG Topic G - Java Collections Framework2 Collections Frameworks A collection is an object that represents a group of objects A collections framework is a unified architecture for representing and manipulating collections -the manipulation should be independent of the implementation details Advantages of having a collections framework -reduces programming effort by providing useful data structures and algorithms -increases performance by providing high-performance data structures and algorithms -interoperability between the different collections -Having a common language for manipulating the collections

SEG Topic G - Java Collections Framework3 The components of Java Collections Framework from the user’s perspective Collection Interfaces: form the basis of the framework -such as sets, lists and maps Implementations Classes: implementations of the collection interfaces Utilities - Utility functions for manipulating collections such as sorting, searching… The Java Collections Framework also includes: -algorithms -wrapper implementations -add functionality to other implementations such as synchronization

SEG Topic G - Java Collections Framework4 Collections Interfaces SortedMap MapCollection Set SortedSet List Java allows using: Lists Sets Hash tables (maps)

SEG Topic G - Java Collections Framework5 Interface: Collection The Collection interface is the root of the collection hierarchy Some Collection implementations allow -duplicate elements and others do not -the elements to be ordered or not JDK doesn't provide any direct implementations of this interface -It provides implementations of more specific sub interfaces like Set and List public interface Collection { // Basic Operations int size(); boolean isEmpty(); boolean contains(Object element); boolean add(Object element); boolean remove(Object element); Iterator iterator(); // Bulk Operations boolean containsAll(Collection c); boolean addAll(Collection c); boolean removeAll(Collection c); boolean retainAll(Collection c); void clear(); // Array Operations Object[] toArray(); Object[] toArray(Object a[]); }

SEG Topic G - Java Collections Framework6 Interface: Set A Set is a collection that cannot contain duplicate elements A set is not ordered -however, some subsets maintain order using extra data structures This is similar to the mathematical concept of sets public interface Set { // Basic Operations int size(); boolean isEmpty(); boolean contains(Object element); boolean add(Object element); boolean remove(Object element); Iterator iterator(); // Bulk Operations boolean containsAll(Collection c); boolean addAll(Collection c); boolean removeAll(Collection c); boolean retainAll(Collection c); void clear(); // Array Operations Object[] toArray(); Object[] toArray(Object a[]); }

SEG Topic G - Java Collections Framework7 Interface: List A List is ordered (also called a sequence) Lists can contain duplicate elements The user can access elements by their integer index (position) public interface List extends Collection { // Positional Access Object get(int index); Object set(int index, Object element); void add(int index, Object element); Object remove(int index); abstract boolean addAll(int index, Collection c); // Search int indexOf(Object o); int lastIndexOf(Object o); // Iteration ListIterator listIterator(); ListIterator listIterator(int index); // Range-view List subList(int from, int to); }

SEG Topic G - Java Collections Framework8 Interface: Map A Map is an object that maps keys to values Maps cannot contain duplicate keys Each key can map to at most one value Hashtables are an example of Maps public interface Map { // Basic Operations Object put(Object key, Object value); Object get(Object key); Object remove(Object key); boolean containsKey(Object key); boolean containsValue(Object value); int size(); boolean isEmpty(); // Bulk Operations void putAll(Map t); void clear(); // Collection Views public Set keySet(); public Collection values(); public Set entrySet(); // Interface for entrySet elements public interface Entry { Object getKey(); Object getValue(); Object setValue(Object value); }

SEG Topic G - Java Collections Framework9 Interface: SortedSet A SortedSet is a Set that maintains its elements in an order Several additional operations are provided to take advantage of the ordering public interface SortedSet extends Set { // Range-view SortedSet subSet(Object fromElement, Object toElement); SortedSet headSet(Object toElement); SortedSet tailSet(Object fromElement); // Endpoints Object first(); Object last(); // Comparator access Comparator comparator(); }

SEG Topic G - Java Collections Framework10 Interface: SortedMap A SortedMap is a Map that maintains its mappings in ascending key order The SortedMap interface is used for apps like dictionaries and telephone directories public interface SortedMap extends Map { Comparator comparator(); SortedMap subMap(Object fromKey, Object toKey); SortedMap headMap(Object toKey); SortedMap tailMap(Object fromKey); Object firstKey(); Object lastKey(); }

SEG Topic G - Java Collections Framework11 Java Collections Object (java.lang) AbstractList LinkedHashMap HashMap AbstractSequentialList ArrayList Stack Vector LinkedHashSet HashSet TreeSet AbstractCollection Collection List AbstractSet Set SortedSet Map SortedMap TreeMap LinkedList AbstractMap Dictionary Map Hashtable

SEG Topic G - Java Collections Framework12 Java Lists: LinkedList Java uses a doubly-linked list -it can be traversed from the beginning or the end LinkedList provides methods to get, remove and insert an element at the beginning and end of the list -these operations allow a linked list to be used as a stack or a queue LinkedList is not synchronized -problems if multiple threads access a list concurrently -LinkedList must be synchronized externally AbstractList AbstractSequentialList ArrayList Stack Vector LinkedList List AbstractCollection Collection

SEG Topic G - Java Collections Framework13 Java Lists: ArrayList Uses an array to store the elements In addition to the methods of the interface List it provides methods to manipulate the size of the array (e.g. ensureCapacity) More efficient than LinkedList for methods involving indices – get(), set() It is not synchronized AbstractList AbstractSequentialList ArrayList Stack VectorLinkedList List AbstractCollection Collection

SEG Topic G - Java Collections Framework14 Java Lists: Vector Same as an the class ArrayList The main difference is that: -The methods of Vector are synchronized AbstractList AbstractSequentialList ArrayList Stack Vector LinkedList List AbstractCollection Collection

SEG Topic G - Java Collections Framework15 Java Lists: Stack The Stack class represents a last-in- first-out (LIFO) stack of objects The common push and pop operations are provided As well as a method to peek at the top item on the stack is also provided Note: It is considered bad design to make Stack a Subclass of vector Vector operations should not be accessible in a Stack It is designed this way because of a historical mistake AbstractList AbstractSequentialList ArrayList Stack VectorLinkedList List AbstractCollection Collection

SEG Topic G - Java Collections Framework16 Java Sets: HashSet HashSet uses a hash table as the data structure to represent a set HashSet is a good choice for representing sets if order of the elements is not important HashSet methods are not synchronized LinkedHashSet HashSet TreeSet AbstractSet Set SortedSet AbstractCollection Collection

SEG Topic G - Java Collections Framework17 Java Sets: LinkedHashSet Hash table and linked list implementation of the Set interface LinkedHashSet differs from HashSet in that the order is maintained Performance is below that of HashSet, due to the expense of maintaining the linked list Its methods are not synchronized LinkedHashSet HashSet TreeSet AbstractSet Set SortedSet AbstractCollection Collection

SEG Topic G - Java Collections Framework18 Java Sets: TreeSet Stores the elements in a balanced binary tree -A binary tree is a tree in which each node has at most two children TreeSet elements are sorted Less efficient than HashSet in insertion due to the use of a binary tree Its methods are not synchronized LinkedHashSet HashSet TreeSet AbstractSet Set SortedSet AbstractCollection Collection

SEG Topic G - Java Collections Framework19 Java Maps: HashMap Stores the entries in a hash table Efficient in inserting (put()) and retrieving elements (get()) The order is not maintained Unlike HashTable, HashMap’s methods are not synchronized LinkedHashMap HashMap Map SortedMap TreeMap AbstractMap

SEG Topic G - Java Collections Framework20 Java Maps: LinkedHashMap Hash table and linked list implementation of the Map interface LinkedHashMap differs from HashMap in that the order is maintained Performance is below that of HashMap, due to the expense of maintaining the linked list Its methods are not synchronized LinkedHashMap HashMap Map SortedMap TreeMap AbstractMap

SEG Topic G - Java Collections Framework21 Java Maps: TreeMap Red-Black tree based implementation of the SortedMap interface The keys are sorted according to their natural order Less efficient than HashMap for insertion and mapping Its methods are not synchronized LinkedHashMap HashMap Map SortedMap TreeMap AbstractMap

SEG Topic G - Java Collections Framework22 The class Hashtable Same as HashMap except that its methods are synchronized The class Hashtable was introduced in JDK 1.0 and uses Enumerations instead of Iterators Dictionary Map Hashtable

SEG Topic G - Java Collections Framework23 The Iterator Interface JCF provides a uniform way to iterate through the collection elements using the Iterator Interface The Iterator interface contains the following methods -boolean hasNext(): returns true if the iteration has more elements. -Object next(): returns the next element in the iteration. -void remove(): removes from the collection the last element returned by the iterator Iterator replaces Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways: -They allow the caller to remove elements from the collection during the iteration with well-defined semantics (a major drawback of Enumerations) -Method names have been improved

SEG Topic G - Java Collections Framework24 The class Arrays This class contains various static methods for manipulating arrays such as: -Sorting, comparing collections, binary searching… Refer to: for a complete list of utilitieshttp://java.sun.com/j2se/1.4.2/docs/api/

SEG Topic G - Java Collections Framework25 References The Java Collections Framework: x.html The Java APIs: