24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.

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.
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.
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.
Collections CS3250. Sources  Slides by Professor Chuck Allison  Core Java, by Cay S. Horstmann and Gary Cornell  The Java Tutorial 
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
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.
Collections A First Glimpse. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector is a kind of collection.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
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.
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.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities Chapter 4.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
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.
(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.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
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.
Sets and Maps Chris Nevison. Set Interface Models collection with no repetitions subinterface of Collection –has all collection methods has a subinterface.
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:
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.
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.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
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 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.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
Java Collections OOP tirgul No
Fundamental of Java Programming
Programming & Data Structures
Introduction to Collections
Introduction to Collections
JAVA Collections Framework Set Interfaces & Implementations
TCSS 342, Winter 2006 Lecture Notes
Introduction to Collections
Introduction to Collections
Collections A First Glimpse.
Part of the Collections Framework
Collections in Java The objectives of this lecture are:
Introduction to Collections
Collections Framework
Introduction to Collections
Collections A First Glimpse 16-Apr-19.
Part of the Collections Framework
Presentation transcript:

24-Jun-15 Introduction to Collections

2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections are defined in java.util The Collections framework is mostly about interfaces There are a number of predefined implementations Java 5 introduced generics and “genericized” all the existing collections Vectors have been redefined to implement Collection Trees, linked lists, stacks, hash tables, and other classes are implementations of Collection Arrays do not implement the Collection interfaces

3 Types of Collection Java supplies several types of Collection : Set : cannot contain duplicate elements, order is not important SortedSet : like a Set, but order is important List : may contain duplicate elements, order is important Java also supplies some “collection-like” things: Map : a “dictionary” that associates keys with values, order is not important SortedMap : like a Map, but order is important While you can get all the details from the Java API, you are expected to learn (i.e. memorize): The signatures of the “most important” methods in each interface The most important implementations of each interface

4 The Collections hierarchy

5 Collections are ADTs Collections are one of the best-designed parts of Java, because They are elegant: they combine maximum power with maximum simplicity They are uniform: when you know how to use one, you almost know how to use them all You can easily convert from one to another

6 The Collection interface Much of the elegance of the Collections Framework arises from the intelligent use of interfaces The Collection interface specifies (among many other operations): boolean add(E o) boolean contains(Object o) boolean remove(Object o) boolean isEmpty() int size() Object[] toArray() Iterator iterator() You should learn all the methods of the Collection interface--all are important

7 The Iterator interface An iterator is an object that will return the elements of a collection, one at a time interface iterator boolean hasNext() Returns true if the iteration has more elements E next() Returns the next element in the iteration void remove() Removes from the underlying collection the last element returned by the iterator (optional operation)

8 The Set interface A set is a collection in which: There are no duplicate elements (according to equals ), and Order is not important interface Set implements Collection, Iterable The methods of Set are exactly the ones in Collection The following methods are especially interesting: boolean contains(Object o) // membership test boolean containsAll(Collection c) //subset test boolean addAll(Collection c) // union boolean retainAll(Collection c) // intersection boolean removeAll(Collection c) // difference addAll, retainAll, and removeAll return true if the receiving set is changed, and false otherwise

9 The List interface A list is an ordered sequence of elements interface List implements Collection, Iterable Some important List methods are: void add(int index, E element) E remove(int index) boolean remove(Object o) E set(int index, E element) E get(int index) int indexOf(Object o) int lastIndexOf(Object o) ListIterator listIterator() A ListIterator is like an Iterator, but has, in addition, hasPrevious and previous methods

10 The SortedSet interface A SortedSet is a Set for which the order of elements is important interface SortedSet implements Set, Collection, Iterable Two of the SortedSet methods are: E first() E last() More interestingly, only Comparable elements can be added to a SortedSet, and the set’s Iterator will return these in sorted order The Comparable interface is covered in a separate lecture

11 The Map interface A map is a data structure for associating keys and values Interface Map The two most important methods are: V put(K key, V value) // adds a key-value pair to the map V get(Object key) // given a key, looks up the associated value Some other important methods are: Set keySet() Returns a set view of the keys contained in this map. Collection values() Returns a collection view of the values contained in this map

12 The SortedMap interface A sorted map is a map that keeps the keys in sorted order Interface SortedMap Two of the SortedMap methods are: K firstKey() K lastKey() More interestingly, only Comparable elements can be used as keys in a SortedMap, and the method Set keySet() will return a set of keys whose iterator will return them sorted order The Comparable interface is covered in a separate lecture

13 Some implementations class HashSet implements Set class TreeSet implements SortedSet class ArrayList implements List class LinkedList implements List class Vector implements List class Stack extends Vector Important methods: push, pop, peek, isEmpty class HashMap implements Map class TreeMap implements SortedMap All of the above provide a no-argument constructor

14 Learn these slides well! I hate to ask students to memorize lists of methods, but this time I will You should know the signatures of all the methods in this set of slides Once you have a better feel for how these collections are used, the methods will make sense and be easier to remember

15 The End