University of Limerick1 Collections The Collection Framework.

Slides:



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

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
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.
Lecture 3 Introduction to Collections Advanced Java Programming 1 dr inż. Wojciech Bieniecki
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.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
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.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
Chapter 19 Java Data Structures
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
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 Chapter 4.
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.
(c) University of Washington14-1 CSC 143 Java Collections.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
111 © 2002, Cisco Systems, Inc. All rights reserved.
COLLECTIONS Byju Veedu s/Collection.html.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Object Oriented Programming Ders 10: Data Structures Mustafa Emre İlal
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.
Data structures Abstract data types Java classes for Data structures and ADTs.
Collections –data structures and Algorithms L. Grewe.
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:
16-August-2002cse Libraries © 2002 University of Washington1 Class Libraries CSE 142, Summer 2002 Computer Programming 1
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.
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.
Nov 22, 2010IAT 2651 Java Collections. Nov 22, 2010IAT 2652 Data Structures  With a collection of data, we often want to do many things –Organize –Iterate.
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.
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.
Collections Dwight Deugo Nesa Matic
Java Collection Classes Com379PT
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Java Collections CHAPTER 3-February-2003
Chapter 19 Java Data Structures
Programming & Data Structures
Java Collections Overview
TCSS 143, Autumn 2004 Lecture Notes
Collections in Java The objectives of this lecture are:
14.1 The java.util Package.
Presentation transcript:

University of Limerick1 Collections The Collection Framework

University of Limerick2 Objectives u This lecture will enable students to –compare an appreciate a second framework which has some variation to the Swing GUI framework –understand how the Collection Framework is used to representing and manipulating collections –Gain a better understanding of the usage of interface for hiding implementation detail

University of Limerick3 The Collection Framework u The collections framework is a unified architecture for representing and manipulating collections, allowing them to be manipulated independently of the details of their representation u It reduces programming effort while increasing performance u It allows for interoperability among unrelated APIs, reduces effort in designing and learning new APIs, and fosters software reuse u The framework is based on six collection interfaces. It includes implementations of these interfaces, and algorithms to manipulate them

University of Limerick4 Collections in practice u In practice a collection is an object that groups together other objects in a single unit u Collections are used to store, retrieve and manipulate data, and to transmit data from one method to another.

University of Limerick5 Collection Framework u All collection types work with objects rather than primitives u There is a general class called Collection, containing methods that any type of collection should have –inserting, retrieving, removing elements –iterating through a collection –converting to an array –getting the size

University of Limerick6 Collections u Three general types of collections represented as interfaces: –List –Map –Set u The type that is used depends on several factors –how the data is structured –what the needs of the programmer are

University of Limerick7 Collections  Java provides two implementations of each interface –HashSet –TreeSet –ArrayList –LinkedList –HashMap –TreeMap u The implementation that is used depends mostly on performance issues

University of Limerick8 Interfaces V’s implementation

University of Limerick9 List u An ordered collection, also known as a sequence –elements indexed sequentially –elements can be retrieved by index –user has control over where to insert elements –can contain duplicate elements u Implementations include LinkedList, ArrayList, (Vector, Stack Java 1.1)

University of Limerick10 List Example // N.B. List has 2 possible implementations List aList = new ArrayList(); System.out.println("empty: " + List.isEmpty()); aList.add("It’s the"); aList.add(”end of the world"); aList.add(”as we know it!"); System.out.println(aList); Object o = aList.remove(aList.size() - 1); aList.add(”Collections are cool!!!!"); System.out.println(aList); aList.clear(); System.out.println("empty: " + aList.isEmpty());

University of Limerick11 Map u Maps keys to values u Keys must be unique –values do not have to be unique u Each key can map to at most a single value –if another value is added with that key, the previous one is overwritten u Implementations include Hashtable, HashMap, TreeMap

University of Limerick12 Map Example // N.B. Map has 2 possible implementations Map tm = new TreeMap(); //first param is key, second is element tm.put(new Integer(8), "eight"); tm.put(new Integer(3), "three"); tm.put(new Integer(20), "twenty"); tm.put(new Integer(5), "five"); tm.put(new Integer(1), "eight"); tm.put(new Integer(20), "another twenty"); System.out.println(tm);

University of Limerick13 Set u A collection that can’t contain duplicate elements –can contain at most one null element –not ordered, no guarantees that elements will be retrieved in the same order added u Implementations include HashSet and TreeSet

University of Limerick14 Set Example // N.B. Set has 2 possible implementations Set string = new HashSet(); hs.add("I"); hs.add("think"); hs.add("therefore"); hs.add("IBM"); System.out.println(string);

University of Limerick15 Questions u Given the choices List, Map or Set, determine which collection type would be appropriate for each of the following examples: –a deck of cards –a dictionary –a line of customers at a bakery where each has to take a number

University of Limerick16 Iterator u The collections use Iterators –similar to Enumeration, but: »method names improved »allows removal of elements u Each collection type has a method called iterator() which returns an Iterator for that collection

University of Limerick17 Iterator u Methods: –boolean hasNext() »returns true if there are more elements –Object next() »returns the next element –void remove() »removes the last element returned by the iterator »must have called next() »can only call once per call to next()

University of Limerick18 Iterator Example System.out.println("\nSize: " + aList.size()); Iterator iter = aList.iterator(); System.out.println(iter.next()); iter.remove(); System.out.println(iter.next()); System.out.println("\nSize: " + aList.size()); System.out.println(aList);

University of Limerick19 List Iterator u List collection types have an additional iterator called a ListIterator –has all functionality of a normal Iterator –plus the ability to move backward in a list –also, can add new elements

University of Limerick20 List Iterator u Additional methods: –Object previous() »returns the previous element –boolean hasPrevious() –returns true if there are more elements (in reverse) –void add(Object o) »inserts an element before the element that would be returned by next or and after the element that would be returned by previous

University of Limerick21 ListIterator Example System.out.println("\nSize: " + aList.size()); ListIterator iter = aList.listIterator(); System.out.println("Next: " + iter.next()); iter.remove(); System.out.println("\nSize: " + aList.size()); System.out.println("Next: " + iter.next()); System.out.println("Previous: " + iter.previous()); iter.add(" to be "); System.out.println("\nSize: " + aList.size()); System.out.println(aList);

University of Limerick22 Additional Collection Utilities u The Collections class provides many additional methods for manipulating collection objects u This class is found in the java.util package

University of Limerick23 Collections class u Selected methods: –void sort(List) »sorts a list –void shuffle(List) »randomly shuffles a list –void reverse(List) »reverses all the elements in a list –Object min(Collection), Object max(Collection) »returns the minimum or maximum element in a collection

University of Limerick24 Sorting u How do collections know how to sort elements? –many classes, such as String or Integer, include a method called compareTo() that provides default comparison behavior »numeric wrapper objects are sorted by primitive values »Strings sorted in ascending lexicographic order (compared character by character)

University of Limerick25 Sorting u To sort objects of classes that you create –determine rules for how those objects compare with one another –write a custom compareTo() method »for example, a list of Book objects could be sorted by Author or Title