Java Collections Framework COMP53 Oct 24, 2007. Collections Framework A unified architecture for representing and manipulating collections Allows collections.

Slides:



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

JAVA Programming (Session 7) When you are willing to make sacrifices for a great cause, you will never be alone. Instructor:
Collections CS3250. Sources  Slides by Professor Chuck Allison  Core Java, by Cay S. Horstmann and Gary Cornell  The Java Tutorial 
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 7 Object Oriented Programming in Java Advanced Topics Collection.
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.
1 The Collection Interface public interface Collection extends Iterable { boolean add(E e); boolean addAll(Collection c); void clear(); boolean contains(Object.
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.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
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.
1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.
„Generics and collections”. Generics and collections Generics From JDK They are similar to C++ templates They allow to eliminate runtime exceptions.
The Java Collections Package C. DeJong Fall 2001.
Java's Collection Framework
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
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.
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
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.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
List data type(ADT). Lists Elements : a 1,a 2,a 3,… a i-1,a i, a i+1,…a n Null List contains: 0 elements Types of Operations on list 1.Insertion 2.Deletion.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
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
Object Oriented Programming in Java Habib Rostami Lecture 7.
CS-2852 Data Structures Week 4, Class 1 - Review Review! Thursday Exam I - Review Implementing ArrayList Big-O Iterators – High-level description Linked.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
©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 OOP tirgul No
Programming & Data Structures
Introduction to Collections
Introduction to Collections
JAVA Collections Framework Set Interfaces & Implementations
Programming in Java Lecture 11: ArrayList
Introduction to Collections
Introduction to Collections
Collections in Java The objectives of this lecture are:
Collections Not in our text.
Introduction to Collections
Collections Framework
Introduction to Collections
Part of the Collections Framework
Java Generics & Iterators
Presentation transcript:

Java Collections Framework COMP53 Oct 24, 2007

Collections Framework A unified architecture for representing and manipulating collections Allows collections to be manipulated independently of the details of their representation. Reduces programming effort while increasing performance. Reduces effort in designing and learning new APIs, and fosters software reuse. Based on six collection interfaces and algorithms to manipulate them.

Core Collection Interfaces These are the interfaces that define the operations that are expected for various kinds of collections. This is an inheritance hierarchy. Collection SetListQueue Map SortedMap SortedSet

Core Collections Collection: the root of the collection hierarchy. A collection represents a group of objects known as its elements. Set: a collection that cannot contain duplicate elements. This interface models the mathematical set abstraction. List: an ordered collection (sometimes called a sequence). Lists can contain duplicate elements. Queue: a collection used to hold multiple elements prior to processing. Map: an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value. SortedSet and SortedMap: Same functionality as Set and Map, but elements are ordered.

Core Collection Interfaces These are the interfaces that define the operations that are expected for various kinds of collections. This is an inheritance hierarchy. Collection SetListQueue Map SortedMap SortedSet For the moment, we’ll focus on the collections we know. We’ll return to the others later.

The Collection Interface public interface Collection extends Iterable { // Basic operations int size(); boolean isEmpty(); boolean contains(Object element); boolean containsAll(Collection c); Iterator iterator(); These operations are required. All collections must implement them.

The Collection Interface public interface Collection extends Iterable { boolean add(E element); boolean remove(Object element); boolean addAll(Collection c); boolean removeAll(Collection c); boolean retainAll(Collection c); void clear(); These operations are optional – collections may leave them unimplemented. Unimplemented methods must throw UnsupportedOperationException.

The Collection Interface public interface Collection extends Iterable { // Array operations Object[] toArray(); T[] toArray(T[] a); These operations are also required. Every collection must be convertible to an array.

The List Interface public interface List extends Collection { // Positional access E get(int index); E set(int index, E element); boolean add(E element); void add(int index, E element); E remove(int index); boolean addAll(int index, Collection c); get() is required, others are optional.

The List Interface public interface List extends Collection { // Search int indexOf(Object o); int lastIndexOf(Object o); // Iteration ListIterator listIterator(); ListIterator listIterator(int index); // Range-view List subList(int from, int to); These operations are required.

The Queue Interface public interface Queue extends Collection { boolean add(E element); // from Collection E remove(); E element(); boolean offer(E e); E poll(); E peek(); } First three throw exceptions on failure. Last three return false or null on failure.

Stack and Queue Operations JCFSTACK ADTQUEUE ADT add offer pushenqueue remove poll popdequeue element peek topfront

Legacy Collections These collections from older versions of Java are still available: Vector – old-style List HashTable – old-style Map

List Implementations ArrayList and LinkedList both implement the List interface – as defined in sections 3.1,3.2 and previous lecture Most of the time, use ArrayList, which offers constant-time positional access and is just plain fast. If you frequently add elements to the beginning of the list or iterate to delete elements from list’s interior, consider using LinkedList.