Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.

Slides:



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

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.
Generics and the ArrayList Class
Chapter 16 Collections, Maps and Iterators Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
© 2006 Pearson Addison-Wesley. All rights reserved16-1 Iterators An iterator is an object that is used with a collection to provide sequential access to.
© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 24 : Collections King Fahd University of Petroleum & Minerals College of Computer.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Iterators in Java. Lecture Objectives To understand the concepts of Java iterators To understand the differences between the Iterator and ListIterator.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
1 Introduction to Searching and Sorting Comparable Interface -Reading p Comparator Interface.
Chapter 19 Java Data Structures
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Lecture 4 The Java Collections Framework. Java Container Classes.
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.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
The Java Collections Framework (JCF) Introduction and review 1.
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.
Chapter 18 Java Collections Framework
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Computer Science 209 Software Development Java Collections.
Data structures and algorithms in the collection framework 1.
The Java Collections Framework By the end of this lecture you should be able to: use the ArrayList class to store a list of objects; use the HashSet class.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Introduction to Generics
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
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.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
©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 Generics.
Chapter 19 Java Data Structures
Software Development Java Collections
JAVA COLLECTIONS LIBRARY
University of Central Florida COP 3330 Object Oriented Programming
CMSC 202 Interfaces.
Java Programming Language
Exception Handling Chapter 9 Edited by JJ.
CMSC 202 Interfaces.
Introduction to Collections
Containers and Iterators
CMSC 202 Interfaces.
Presentation transcript:

Java Collections

Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections

Java Collections A Java collection is any class that holds objects and implements the Collection interface  For example, the ArrayList class is a Java collection class, and implements all the methods in the Collection interface  Collections are used along with iterators The Collection interface is the highest level of Java's framework for collection classes  All of the collection classes discussed here can be found in package java.util

The Collection Landscape

Wildcards Classes and interfaces in the collection framework can have parameter type specifications that do not fully specify the type plugged in for the type parameter  Because they specify a wide range of argument types, they are known as wildcards public void method(String arg1, ArrayList arg2)  In the above example, the first argument is of type String, while the second argument can be an ArrayList with any base type

A bound can be placed on a wildcard specifying that the type used must be an ancestor type or descendent type of some class or interface  The notation specifies that the argument plugged in be an object of any descendent class of String  The notation specifies that the argument plugged in be an object of any ancestor class of String Wildcards (Cont’d)

The Collection Framework The Collection interface describes the basic operations that all collection classes should implement  The method headings for these operations are shown on the next several slides Since an interface is a type, any method can be defined with a parameter of type Collection  That parameter can be filled with an argument that is an object of any class in the collection framework

Method Headings in the Collection Interface

Method Headings in the Collection Interface (Cont’d)

Collection Relationships There are a number of different predefined classes that implement the Collection interface  Programmer defined classes can implement it also A method written to manipulate a parameter of type Collection will work for all of these classes, either singly or intermixed There are two main interfaces that extend the Collection interface: The Set interface and the List interface

Classes that implement the Set interface do not allow an element in the class to occur more than once  The Set interface has the same method headings as the Collection interface, but in some cases the semantics (intended meanings) are different  Methods that are optional in the Collection interface are required in the Set interface Collection Relationships (Cont’d)

Classes that implement the List interface have their elements ordered as on a list  Elements are indexed starting with zero  A class that implements the List interface allows elements to occur more than once  The List interface has more method headings than the Collection interface  Some of the methods inherited from the Collection interface have different semantics in the List interface  The ArrayList class implements the List interface Collection Relationships (Cont’d)

Methods in the Set Interface

Methods in the Set Interface (Cont’d)

Methods in the List Interface

Methods in the List Interface (Cont’d)

Pitfall: Optional Operations When an interface lists a method as "optional," it must still be implemented in a class that implements the interface  The optional part means that it is permitted to write a method that does not completely implement its intended semantics  However, if a trivial implementation is given, then the method body should throw an UnsupportedOperationException

Tip: Dealing with All Those Exceptions The tables of methods for the various collection interfaces and classes indicate that certain exceptions are thrown  These are unchecked exceptions, so they are useful for debugging, but need not be declared or caught In an existing collection class, they can be viewed as run-time error messages In a derived class of some other collection class, most or all of them will be inherited In a collection class defined from scratch, if it is to implement a collection interface, then it should throw the exceptions that are specified in the interface

Concrete Collections Classes The concrete class HashSet implements the Set interface, and can be used if additional methods are not needed  The HashSet class implements all the methods in the Set interface, and adds only constructors  The HashSet class is implemented using a hash table The ArrayList and Vector classes implement the List interface, and can be used if additional methods are not needed  Both the ArrayList and Vector interfaces implement all the methods in the interface List  Either class can be used when a List with efficient random access to elements is needed

The concrete class LinkedList is a concrete derived class of the abstract class AbstractSequentialList  When efficient sequential movement through a list is needed, the LinkedList class should be used The interface SortedSet and the concrete class TreeSet are designed for implementations of the Set interface that provide for rapid retrieval of elements  The implementation of the class is similar to a binary tree, but with ways to do inserting that keep the tree balanced Concrete Collections Classes (Cont’d)

Methods in the HashSet Class

Methods in the HashSet Class (Cont’d)