What is the Java Collections Framework?

Similar documents
CS Ananda Gunawardena

9/16/2010 CS Ananda Gunawardena

CONTAİNERS COLLECTİONS

Java Collections Framework: Interfaces

Class 32: The Java Collections Framework

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

Generic classes & the Java Collections Framework. *Really* Reusable Code

The Java Collections Framework and Lists in Java Parts 1 & 2

CSC 1214: Object-Oriented Programming

Collections (Collection Framework) Sang Shin Java Technology Architect Sun Microsystems, Inc.

Collections class Comparable and Comparator. Slides by Mark Hancock (adapted from notes by Craig Schock)

Lecture 15 Summary. Collections Framework. Collections class Comparable and Comparator. Iterable, Collections List, Set Map

Le L c e t c ur u e e 8 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Collections

Sets and Maps. Part of the Collections Framework

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

Lecture 15 Summary 3/11/2009. By the end of this lecture, you will be able to use different types of Collections and Maps in your Java code.

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces

Introduction to Collections

Programmieren II. Collections. Alexander Fraser. May 28, (Based on material from T. Bögel)

Generic Programming. *Really* reusable code

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University

Vector (Java 2 Platform SE 5.0) Overview Package Class Use Tree Deprecated Index Help

Generics Collection Framework

Lecture 6 Collections

Algorithms. Produced by. Eamonn de Leastar

Arrays organize each data element as sequential memory cells each accessed by an index. data data data data data data data data

Important Dates. Game State and Tree. Today s topics. Game Tree and Mini-Max. Games and Mini-Max 3/20/14

COMP6700/2140 Abstract Data Types: Queue, Set, Map

Java Collection Framework

Generics and collections

Type Parameters: E - the type of elements returned by this iterator Methods Modifier and Type Method and Description

ArrayList. Introduction. java.util.arraylist

Abstract data types (again) Announcements. Example ADT an integer bag (next) The Java Collections Framework

Overview of Java ArrayList, HashTable, HashMap, Hashet,LinkedList

EECS 2011 M: Fundamentals of Data Structures

CS61B Lecture #17. Last modified: Mon Oct 1 13:40: CS61B: Lecture #17 1

Tha Java Programming Language

An Interface with Generics

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

boolean add(object o) Method Description (from docs API ) Method Description (from docs API )

Collections (Java) Collections Framework

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

Lecture 6: ArrayList Implementation

The Java Collections Framework. Chapters 7.5

Collections. The Java Collections Framework. The Interfaces

Interfaces, collections and comparisons

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Java Collections. Wrapper classes. Wrapper classes

Java Collections. Engi Hafez Seliem

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

36. Collections. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Abstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures

11. Collections of Objects

Data Structures and Algorithms

CS11 Java. Winter Lecture 8

Recap. List Types. List Functionality. ListIterator. Adapter Design Pattern. Department of Computer Science 1

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

CS S-05 Abstract Data Types and Lists 1

Case Study: Debugging a Discovered Specification for java.util.arraylist by Using Algebraic Interpretation

Topic 10: The Java Collections Framework (and Iterators)

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

CSCI Object Oriented Design: Java Review Execution, I/O and New Features George Blankenship. Java Review: Execution, IO & Java 5

JAVA COLLECTION FRAMEWORK & SETS

COMP200 GENERICS. OOP using Java, from slides by Shayan Javed

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

Pieter van den Hombergh Richard van den Ham. February 8, 2018

SSJ User s Guide. Package simevents Simulation Clock and Event List Management

Java Language Features

CMSC 202H. Containers and Iterators

Lecture 4. The Java Collections Framework

CMSC 202. Containers

Get started with the Java Collections Framework By Dan Becker

EXAMINATIONS 2016 TRIMESTER 2

A simple map: Hashtable

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList

CS2110: Software Development Methods. Maps and Sets in Java

JAVA PROGRAMMING COURSE NO. IT-705(A) SECTION - A

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. June 8, 2017

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

Java Collections Framework

Java Collections Framework. 24 April 2013 OSU CSE 1

Object-Oriented Programming in the Java language

INTRODUCTION TO DATA AND PROCEDURE

Computer Science II (Spring )

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

PIC 20A Collections and Data Structures

ECE 122. Engineering Problem Solving with Java

List... NOTE We use List as illustrative example of collections Study the others from textbook!

Lists. The List ADT. Reading: Textbook Sections

EXAMINATIONS 2017 TRIMESTER 2

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4

Collections and Maps

List ADT. Announcements. The List interface. Implementing the List ADT

CSC 1052 Algorithms & Data Structures II: Lists

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

Transcription:

1 of 13

What is the Java Collections Framework? To begin with, what is a collection?. I have a collection of comic books. In that collection, I have Tarzan comics, Phantom comics, Superman comics and several more. I hold my collection of emails in an email folder on my computer. In the context of object oriented programming, a collection is an object that groups multiple objects into a single collection. It is used to store, retrieve, manipulate and transmit a collection of data from one method to another. In Java, the Collections Framework is an architecture that can be used for representing and manipulating collections. All collections frameworks involve three things: 1. Interfaces 2. Implementations 3. Algorithms Interfaces are ADT (abstract data types) used to represent collections. Interfaces are typically used to manipulate collections without being bound to a particular implementation. Implementations are Java programs that are written based on the interfaces to provide the functionality, in this case, that of the collections. Typically these implementations can be reused. Algorithms are the specific set of steps of the Java program used in implementation. Some examples are the searching and sorting algorithms used in implementing the collections interface. The benefits of using a Collections Framework are 1. It offers rich and reusable data structures and algorithms so useful in problem solving 2. The data structures and algorithms are usually high-quality and high-performance ones 3. Various implementations of the interfaces are interchangeable 4. Offers interoperability between unrelated APIs Interfaces Collection interfaces enable a degree of control over the collections and facilitate passing collections to myriad of methods. In other words, interfaces enable manipulation of collections independent of implementation. In Java, the Collection interfaces are Set o List Sorted Set We also have the Map interfaces, but it is not really treated as a part of the Collections interface hierarchy. It has 2 of 13

Map o Sorted Map It is important to know that the JDK does not provide separate interfaces for each variant of each collection type. Some salient features are 1. It enables managing the number of core collection interfaces easily 2. The only variants possible are a. Immutable b. fixed-size c. append-only 3. The modification operations in each of the interfaces are optional. This is also done to cope with these variants. 4. It is possible that certain implementations may not support some of these operations. If an unsupported operation is invoked, the collection throws an exception. This exception is called the UnsupportedOperationException. 5. It is the responsibility of the different implementations to provide the details of the operations that are supported, in the documentation of the implementation. 6. The JDK's general-purpose implementations support all the optional operations. Links to Good Tutorials on Collection Framework 1. An exhaustive tutorial from NTU, Singapore at http://www.ntu.edu.sg/home/ehchua/programming/java/j5c_collection.html 2. There are a series of video tutorials from the Cave of Programming. The Part 1 of the Java Collection framework is at http://www.youtube.com/watch?v=mkctxtle7xu Learning to Use the Collection Interface The Collection interface can be depicted as a hierarchy. the Collection interface is at the root of the collection hierarchy. Elements are the group of objects that a collection represents. Duplicates: In some collection implementations duplicate elements are allowed, while in others it is not allowed. Ordering: Some implementations of the Collection interface are ordered, while others are not. Implementations: The implementations of the Collection interfaces do not come directly from the JDK. The JDK provides the implementations of its sub-interfaces. The Collection interface is used to pass collections between methods and manipulate them when maximum generality is required. Example: Collection implementations usually have a constructor with a Collection as one of the parameters. This constructor is used to initialize the new Collection to contain all the 3 of 13

elements in the specified Collection. By invoking this constructor, we are able to create a Collection of a desired implementation type, initially containing all the elements in any given Collection. This regardless of whatever is its sub-interface or implementation type. Thus, if we have a collection mycollection, which is a List or a set or any other type of Collection; we can create a new ArrayList having all the elements in mycollection. A way of achieving this is shown below. List mylist = new ArrayList(myCollection); // Given below is the Collection interface public interface Collection { // These are the Basic Operations int size(); boolean isempty(); boolean contains(object element); boolean add(object element); //Optional boolean remove(object element); // Optional Iterator iterator(); // Bulk Operations boolean containsall(collection mycollection); boolean addall(collection mycollection); // Optional boolean removeall(collection mycollection); // Optional boolean retainall(collection mycollection); // Optional void clear(); // Optional // These are Array Operations Object[] toarray(); Object[] toarray(object a[]); The Collection interface has the following methods. 1. To add elements to a collection. The add() method is implemented such that it can allow duplicate elements to be present or it does not allow the duplicate elements to be present. The method returns true if the collection has changed fooling the execution of this method. 2. To remove elements from a collection. The remove() method enables one to remove a single instance of the element from the collection. This method returns true if the collection has changed after execution of the method. 3. To iterate over elements of a collection. The object returned by the iterator() method is similar to an enumeration. But differs in two ways. a. When we call the iterator() method, it allows us to remove an element from the collection during iteration. b. When we are moving through an enumeration, we cannot remove an element from the collection without compromising safety. 4 of 13

The Iterator interface is displayed below. public interface Iterator{ boolean hasnext(); Object next(); // Optional ones void remove(); 4. To check if a next element is present. The hasnext() method is similar to the Enumeration.hasMoreElements() method, while the next() method is similar to the Enumeration.nextElement() method. The remove() method removes the last element returned by the next() method from the collection. The remove() method can be called only once for every call to the next() method. An exception is thrown if a violation occurs. Example: Traversing a collection and removing elements. Suppose we want to traverse a collection and simultaneously remove such elements that do not satisfy a specific condition. We can use the iterator to perform this task. static void removeelementthatfailscondition(collection mycollection) { for (Iterator k = mycollection.iterator(); k.hasnext(); ) if (conditionfails(k.next())) k.remove(); Let us spend a minute and examine this segment of code. This method would work regardless of what the collection contains and regardless of the collection implementation. Essentially, this method is polymorphic. Bulk Operations on Collections Bulk operations can be used to manipulate the contents of an entire collection at a time. 1. containsall: 2. addall: a. This returns true if the target collection contains all the elements in the specified collection. a. This adds all the elements of the specified collection to the target collection. 5 of 13

b. The method returns return true if the target collection was modified on executing the method. 3. removeall: a. This removes from the target collection all those elements that are also present in the specified collection. b. The method returns return true if the target collection was modified on executing the method. 4. retainall: 5. clear: a. This removes from the target collection all its elements that are not also present in the specified collection. b. In other words, it retains only those elements in the target collection that are also present in the specified collection. c. The method returns return true if the target collection was modified on executing the method. a. It removes all elements from the collection. Example: To remove all instances of a specified element myelement from the collection mycollection. mycollection.removeall(collections.singleton(myelement)); Example: To remove all null elements from the collection mycollection. mycollection.removeall(collections.singleton(null)); Do note the use of the Collections.singleton(). This is a factory method. This returns a Set that is immutable, and containing the specified element{s). Array Methods These are available to act as bridge between the collections and cases where APIs expect arrays as input. 6 of 13

1. The toarray() method without any arguments creates a new array of objects. 2. The toarray() method can also take parameters to provide an array or to choose the runtime type of the output array. Example: To place the contents of the collection mycollection onto a newly created array of objects whose length is equal to the number of elements in mycollection. Object[] myobject = mycollection.toarray(); Example: To place the contents of mycollection into a newly created array of String type whose length is equal to the number of elements in mycollection. String[] mystrings = (String[]) mycollection.toarray(new String[0]); These are just simple examples. One needs to solve many problems, often of a non-trivial variety to gain good competencies in using the Collections. Learning to Use the Set Interface We have encountered the mathematical abstraction called set. In Java, a set is a special type of collection. It does not contain duplicate elements, just like its mathematical counterpart. The Set interface is a subset of the Collection interface and contains only those methods that are inherited from the Collection interface. It does not contain any other additional method. A set allows for versatile behavior of the equals and hashcode operations. The power of equals and hashcode allow set objects with different implementations to be compared meaningfully. Remember that two Set objects containing the same objects are considered equal. In Java, the Set interface is as displayed below. 7 of 13

public interface Set { // Basic Operations int size(); boolean isempty(); boolean contains(object element); boolean add(object element); // Optional boolean remove(object element); // Optional Iterator iterator(); // These are Bulk Operations boolean containsall(collection mycollection); boolean addall(collection mycollection); // Optional boolean removeall(collection mycollection); // Optional boolean retainall(collection mycollection); // Optional void clear(); // Optional // These are Array Operations Object[] toarray(); Object[] toarray(object a[]); General Purpose Set Implementations 1. HashSet stores its elements in a hash table. This implementation offers excellent performance 2. TreeSet stores its elements in a red-black tree. This implementation guarantees the order of iteration. Example: Removing duplicates from a collection mycolelction. We create a set initially containing all the elements in mycollection. This cannot contain duplicates by definition. Collection distinctcollection = new HashSet(myCollection); 8 of 13

The following are some of the methods that can be used with Set. 1. size(): 2. isempty(): 3. add(): 4. remove(): 5. iterator(): a. This returns the number of elements in a set. a. This returns true if the set is empty. a. This adds a new element to the set if it was not present already. It returns a true to indicate that the element was added or false if not. a. This removes the specified element from the set if it is present and returns a true. It returns false if the element was not removed. a. This returns an iterator of the set. Example: To write a Java program takes the words in its argument list (could be long or short) and prints out any duplicate words present, the number of distinct words, as well as a list of the words with duplicates eliminated. import java.util.*; public class DetectAndRemoveDuplicates { public static void main(string args[]) { Set myset = new HashSet(); for (int k = 0; k < args.length; k++) if (!myset.add(args[k])) System.out.println("Duplicate Found -- "+args[i]); System.out.println("Set Size: "+myset.size()+" Distinct Words Found -- "+ myset); I urge you to execute this program and examine the results. This is an incredibly powerful method that performs an otherwise relatively complicated task rather simply. Example: To write a Java program takes the words in its argument list (could be long or short) and prints out any duplicate words present in alphabetical order, the number of distinct words, as well as a list of the words with duplicates eliminated. 9 of 13

Note that this requirement is quite the same as the earlier example, except that we want the list of words to be displayed in alphabetical order. Now don t rush off to write a fresh program from start. If we use the power of the Java Collection Framework, we would realize that all we need to do is to use the implementation of the set by a TreeSet instead of the earlier HashSet. In the above program, just make the modification in the constructor for the set as follows. Set myset = new TreeSet(); Compile and execute this program to convince yourself that the program would work as intended. This is really powerful stuff. Bulk Operations on Set 1. set1.containsall(set2): a. This returns true if set2 is a subset of set1. 2. set1.addall(set2): a. This makes set1 into the union of set1 and set2. b. The union of two sets is the set containing all the elements found in the two sets. 3. set1.retainall(set2): a. This makes set1 into the intersection of set1 and set2. b. The intersection of two sets is the set containing only those elements that are common in both the sets. 4. set1.removeall(set2): a. This makes set1 into the asymmetric set difference of set1 and set2. b. The set difference of set1 - set2 is the set containing all the elements found in set1 but not in set2. Learning to Use the List Interface In the Java Collection framework, a list is an ordered collection. It is also called a sequence. Lists allow duplicate elements to be present unlike in a set. We can control the place in a list where a new element is to be inserted. Using an integer index, which is used to denote the position of the element in the list, we can access the elements. 10 of 13

The List can use the operations inherited from the Collection interface. Some of the other operations included in the List interface are as follows. 1. Positional Access: This is used to manipulate the elements based on their numerical position in the list. 2. Search: This is used to search for a specified object in the list and return its numerical position. 3. List Iteration: This extends Iterator mechanism for enabling sequential traversal in a list.. 4. Range-View: This is used to carry out arbitrary range operations on the list. The List interface is displayed below. public interface List extends Collection { // Positional Access Object get(int index); // Optional Object set(int index, Object element); // Optional void add(int index, Object element); // Optional Object remove(int index); // Optional abstract boolean addall(int index, Collection mycollection); // Search int indexof(object myobject); int lastindexof(object myobject); // Iteration ListIterator listiterator(); ListIterator listiterator(int index); // Range-View List sublist(int from, int to); General Purpose List Implementations The following are the two general purpose List implementations. 1. ArrayList 2. LinkedList The following are some methods that can be used with List. 11 of 13

1. remove(): 2. add(): 3. addall(): a. This method is used to remove the first occurrence of the specified element from the list. a. Append the specified item at the end of the list. a. Append the specified item at the end of the list. 4. ListIterator(): a. The iterator operation in a list returns the elements of the list in proper sequence b. It allows us to traverse the list in either direction, modify the list during iteration, and obtain the current position of the iterator 5. hasnext(), hasprevious(), next(), previous(): a. They are all inherited by ListIterator() and work the same way as for the other interfaces There are other methods like get(), set(), and remove() that can be used with lists. It would be a good idea to learn how to concatenate two lists, to check if two lists contain the same elements in the same order, and access elements according their positions in a list. There are far too many methods in this interface. A good way of developing competencies is to solve problems that require the use of various methods, rather than listing the methods and memorizing what they do. Tutorials on List Interface There are a number of good tutorials on YouTube. 1. Cave of Programming tutorial is at http://www.youtube.com/watch?v=ymdbl_fceii 2. Authentic one at Oracle Site http://docs.oracle.com/javase/tutorial/collections/interfaces/list.html Other Interfaces in Collection There are other interfaces that are also very powerful and useful in developing applications. They are: 1. The Map Interface 12 of 13

I recommend that you go through the tutorial at http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html. 2. The SortedSet Interface I recommend that you go through the tutorial at http://docs.oracle.com/javase/tutorial/collections/interfaces/sorted-set.html 3. The SortedMap Interface I recommend that you go through the tutorial at http://docs.oracle.com/javase/tutorial/collections/interfaces/sorted-map.html 13 of 13