Java Collections OOP tirgul No. 7 2009.

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.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
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.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
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.
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.
„Generics and collections”. Generics and collections Generics From JDK They are similar to C++ templates They allow to eliminate runtime exceptions.
Java's Collection Framework
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Data structures and algorithms in the collection framework 1 Part 2.
Computer Science 209 Software Development Java Collections.
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
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:
Sadegh Aliakbary Sharif University of Technology Fall 2012.
©SoftMoore ConsultingSlide 1 Java Collections Framework.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
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.
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.
©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 CHAPTER 3-February-2003
Fundamental of Java Programming
Advanced Programming in Java
Chapter 19 Java Data Structures
Software Development Java Collections
Programming & Data Structures
Advanced Programming in Java
University of Central Florida COP 3330 Object Oriented Programming
Interfaces in Java’s Collection Framework
Introduction to Collections
Introduction to Collections
JAVA Collections Framework Set Interfaces & Implementations
TCSS 342, Winter 2006 Lecture Notes
Java语言程序设计 马 皓
Practical Session 3 Java Collections
Introduction to Collections
Introduction to Collections
Part of the Collections Framework
Practical Session 4 Java Collections
Collections in Java The objectives of this lecture are:
Advanced Programming in Java
Introduction to Collections
Collections Framework
Introduction to Collections
Data Structures II AP Computer Science
Part of the Collections Framework
CS 240 – Advanced Programming Concepts
Java Generics & Iterators
Presentation transcript:

Java Collections OOP tirgul No. 7 2009

History JDK 1.0: Vector, Dictionary, Hashtable, Stack, Enumeration JDK 1.2: Collection, Iterator, List, Set, Map, ArrayList, HashSet, TreeSet, HashMap, WeakHashMap JDK 1.4: RandomAccess, IdentityHashMap, LinkedHashMap, LinkedHashSet JDK 1.5: Queue, java.util.concurrent, ... JDK 1.6: Deque, ConcurrentSkipListSet/Map, ... JDK 1.7: TransferQueue, LinkedTransferQueue

public interface Collection<E>/Map<K,V> Collection = a group of objects public interface Collection<E>/Map<K,V> How to choose right collection? Implementation Interface Duplicates? List, Queue Set Map - + Complexity? Add/remove/search O(1)/O(LogN)/O(N) Sorted? SortedSet SortedMap + Requirements? equals() hashCode() Comparable Keys Map Set - + Null support? Synchronized?

Collection<E> Map<K,V> Set<E> List<E> 7 Basic collection interfaces… Iterable<E> Collection<E> Map<K,V> Set<E> List<E> Queue<E> SortedMap<K,V> SortedSet<E>

Collection<E> interface: Basic, general, flexible operations to retrieve/add/remove members public interface Collection<E> extends Iterable<E> { // Basic operations int size(); boolean isEmpty(); boolean contains(Object element); boolean add(E element); //optional boolean remove(Object element); //optional Iterator<E> iterator(); // Bulk operations boolean containsAll(Collection<?> c); boolean addAll(Collection<? extends E> c); //optional boolean removeAll(Collection<?> c); //optional boolean retainAll(Collection<?> c); //optional void clear(); //optional // Array operations Object[] toArray(); <T> T[] toArray(T[] a); }

Map<K,V> interface: Access to values using keys public interface Map<K,V> { // Basic operations V put(K key, V value); V get(Object key); V remove(Object key); boolean containsKey(Object key); boolean containsValue(Object value); int size(); boolean isEmpty(); // Bulk operations void putAll(Map<? extends K, ? extends V> m); void clear(); // Collection Views Set<K> keySet(); Collection<V> values(); Set<Map.Entry<K,V>> entrySet(); // Interface for entrySet elements interface Entry { K getKey(); V getValue(); V setValue(V value); }

Queue & List Interfaces Ordered sequences of objects public interface List<E> extends Collection<E> { // Positional access E get(int index); E set(int index, E element); //optional boolean add(E element); //optional void add(int index, E element); //optional E remove(int index); //optional boolean addAll(int index, Collection<? extends E> c); //optional // Search int indexOf(Object o); int lastIndexOf(Object o); // Iteration ListIterator<E> listIterator(); ListIterator<E> listIterator(int index); // Range-view List<E> subList(int from, int to); } public interface Queue<E> extends Collection<E> { E element(); boolean offer(E e); E peek(); E poll(); E remove(); - The ‘getTextureString’ is just an illustration

Basic implementations LinkedHashMap   TreeMap HashMap Map LinkedList ArrayList List LinkedHashSet TreeSet HashSet Set Interfaces Hash Table + Linked List Linked List Balanced Tree Resizable Array Hash Table Implementations Conventions equals+ hashCode equals compareTo

Computational complexity Mind the constants!!!

Computational complexity Add Remove Get Contains ArrayList O(1) O(N) LinkedList HashSet O(1) avg TreeSet O(logN)

Iterators • Fail-fast - work on live data, but become invalid when live data is modified • Weakly consistent - work on live data, safe, reflect updates and deletes, but not inserts • Snapshot - work on a snapshot of the live data, fast, safe, but possibly stale

Implementing new collection size… …isEmpty… …removeAll… …contains… …iterator… …add… …containsAll…… …remove Too many useful functions in interfaces? Abstract classes! AbstractCollection<E> AbstractList<E> AbstractMap<K,V> AbstractSet<E>

Working with collections import java.util.collections; Static library with many useful algorithms Searching… int pos = Collections.binarySearch(list, key); Counting… int frequency = Collections.frequency(myColl,item) ; Shuffling, sorting, reversing, performing set operations and much more…

Collection algorithms: - min - max - frequency - disjoint Factories: - EMPTY_SET - EMPTY_LIST - EMPTY_MAP - emptySet - emptyList - emptyMap - singleton - singletonList - singletonMap - nCopies - list(Enumeration) Comparators: - reverseOrder Miscellaneous: - addAll - enumeration Wrappers: - unmodifiableCollection - unmodifiableSet - unmodifiableSortedSet - unmodifiableList - unmodifiableMap - unmodifiableSortedMap - synchronizedCollection - synchronizedSet - synchronizedSortedSet - synchronizedList - synchronizedMap - synchronizedSortedMap - checkedCollection - checkedSet - checkedSortedSet - checkedList - checkedMap - checkedSortedMap Collection algorithms: - min - max - frequency - disjoint List algorithms: - sort - binarySearch - reverse - shuffle - swap - fill - copy - replaceAll - indexOfSubList - lastIndexOfSubList Collection factories: - EMPTY_SET - EMPTY_LIST - EMPTY_MAP - emptySet - emptyList - emptyMap - singleton - singletonList - singletonMap - nCopies list(Enumeration) Collection Wrappers: - unmodifiableCollection - unmodifiableSet - unmodifiableSortedSet - unmodifiableList - unmodifiableMap - unmodifiableSortedMap - synchronizedCollection - synchronizedSet - synchronizedSortedSet - synchronizedList - synchronizedMap - synchronizedSortedMap - checkedCollection - checkedSet - checkedSortedSet - checkedList - checkedMap - checkedSortedMap

More info and more operations: Exploring the code… (in Eclipse with JRE installed) Type collection name Ctrl-Left-mouse View More info and more operations: java API documentation Java Collection Tutorial http://java.sun.com/docs/books/tutorial/collections/index.html