תרגול 7 – מבני נתונים גנריים רובי בוים ומתי שמרת

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 The Collections API.
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.
Java Collection Framework. Interface Collection add(o) Add a new element clear() Remove all elements contains(o) Membership checking. IsEmpty() Whether.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
Java's Collection Framework
Java Collections Framework A presentation by Eric Fabricant.
CS Collection and Input/Output Classes CS 3331 Fall 2009.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
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.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 18 Java Collections Framework
תוכנה 1 תרגול 8 – מבני נתונים גנריים. 2 Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework.
Collections –data structures and Algorithms L. Grewe.
Data structures and algorithms in the collection framework 1.
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:
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.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
University of Limerick1 Collections The Collection Framework.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
©Karsten Lundqvist Example: Array and Arrays 1 import java.util.*; public class Example { private int intValues[] = { 1, 2, 3, 4, 5, 6 }; private double.
תוכנה 1 תרגול 8 – מבני נתונים גנריים. Java Collections Framework 2 Collection: a group of elements Interface Based Design: Java Collections Framework.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Java Collections CHAPTER 3-February-2003
Slides by Donald W. Smith
Java Collections OOP tirgul No
Advanced Programming in Java
Java's Collection Framework
Chapter 19 Java Data Structures
Software Development Java Collections
Advanced Programming in Java
Interfaces in Java’s Collection Framework
CS240: Advanced Programming Concepts
Advanced Programming in Java
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Road Map CS Concepts Data Structures Java Language Java Collections
Java Collections Overview
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
Collections A First Glimpse.
Part of the Collections Framework
Java Collections Framework
תרגול 8 – מבני נתונים גנריים
Collections James Brucker.
Advanced Programming in Java
Introduction to Collections
CSE 1020: The Collection Framework
Introduction to Collections
Containers and Iterators
Collections A First Glimpse 16-Apr-19.
Part of the Collections Framework
CS 240 – Advanced Programming Concepts
Chapter 16 Generic Collections
Introduction to Java Collection
Practical Session 3 Java Collections
Presentation transcript:

תרגול 7 – מבני נתונים גנריים רובי בוים ומתי שמרת תוכנה 1 תרגול 7 – מבני נתונים גנריים רובי בוים ומתי שמרת

Java Collections Framework Collection: a group of elements Interface Based Design: Java Collections Framework Interfaces Implementations Algorithms

Online Resources Java 6 API Specification: http://java.sun.com/javase/6/docs/api/ The Collections framework in java.util Sun Tutorial: http://java.sun.com/docs/books/tutorial/collections/

Collection Interfaces Unordered Rejects duplicates <<interface>> Collection<E> <<interface>> Map<K,V> Unordered Rejects duplicates extends <<interface>> Set<E> <<interface>> List<E> <<interface>> Queue<E> <<interface>> SortedMap<K,V> Interfaces can add functionality to other interfaces by extending those interfcaes Ordered Allows duplicates FIFO Order Allows duplicates Ordered Rejects duplicates <<interface>> SortedSet<E> Ordered Rejects duplicates

A Simple Example Collection<String> stringCollection = … Collection<Integer> integerCollection = … stringCollection.add("Hello"); integerCollection.add(5); integerCollection.add(new Integer(6)); stringCollection.add(7); integerCollection.add(“world”); stringCollection = integerCollection;

A Simple Example Collection<String> stringCollection = … Collection<Integer> integerCollection = … stringCollection.add("Hello"); integerCollection.add(5); integerCollection.add(new Integer(6)); stringCollection.add(7); integerCollection.add(“world”); stringCollection = integerCollection; מצביעים ל Collection של מחרוזות ושל מספרים Collection אינו מחזיק טיפוסים פרימיטיביים, לכן נשתמש ב Integer, Double, Float וכדומה נראה בהמשך אילו מחלקות מממשות מנשק זה

A Simple Example Collection<String> stringCollection = … Collection<Integer> integerCollection = … stringCollection.add("Hello");  integerCollection.add(5);  integerCollection.add(new Integer(6));  stringCollection.add(7);  integerCollection.add(“world”);  stringCollection = integerCollection; 

Collection extends Iterable <<interface>> Iterable<E> has only one method: Iterator<E> iterator(); <<interface>> Collection<E> <<interface>> Set<E> <<interface>> List<E> <<interface>> Queue<E> <<interface>> SortedSet<E>

The Iterator Interface Provide a way to access the elements of a collection sequentially without exposing the underlying representation Methods: hasNext() - Returns true if there are more elements next() - Returns the next element remove() - Removes the last element returned by the iterator (optional operation) Command and Query

Iterating over a Collection Explicitly using an Iterator Using foreach synatx for (Iterator<String> iter = stringCollection.iterator(); iter.hasNext(); ) { System.out.println(iter.next()); } for (String str : stringCollection) { System.out.println(str); }

Collection Implementations Class Name Convention: <Data structure> <Interface> Data Structures General Purpose Implementations Linked List Balanced Tree Resizable Array Hash Table TreeSet (SortedSet) HashSet Set Interfaces LinkedList ArrayDeque Queue ArrayList List TreeMap (SortedMap) HashMap Map

General Purpose Implementations No Direct Implementation <<interface>> Collection <<interface>> Map <<interface>> Set <<interface>> List <<interface>> Queue <<interface>> SortedMap <<interface>> SortedSet HashSet TreeSet ArrayList LinkedList HashMap TreeMap

List Example Output: [3, 1, 1] Interface List Example List<Integer> list = new ArrayList<Integer>(); list.add(3); list.add(1); list.add(new Integer(1)); list.add(new Integer(6)); list.remove(list.size()-1); System.out.println(list); Implementation List holds Integer references (auto-boxing) List allows duplicates Invokes List.toString() remove() can get index or reference as argument Output: Insertion order is kept [3, 1, 1]

Set Example Output: [1, 3] or [3, 1] Set<Integer> set = new HashSet<Integer>(); set.add(3); set.add(1); set.add(new Integer(1)); set.add(new Integer(6)); set.remove(6); System.out.println(set); A set does not allow duplicates. It does not contain: two references to the same object two references to null references to two objects a and b such that a.equals(b) remove() can get only reference as argument Output: [1, 3] or [3, 1] Insertion order is not guaranteed

Queue Example Output: [1, 1, 6] Queue<Integer> queue = new LinkedList<Integer>(); queue.add(3); queue.add(1); queue.add(new Integer(1)); queue.add(new Integer(6)); queue.remove(); System.out.println(queue); Elements are added at the end of the queue remove() may have no argument – head is removed Output: [1, 1, 6] FIFO order

Values (phone numbers) Map Example Map<String,String> map = new HashMap<String,String>(); map.put("Dan", "03-9516743"); map.put("Rita", "09-5076452"); map.put("Leo", "08-5530098"); map.put("Rita", "06-8201124"); System.out.println(map); Output: {Leo=08-5530098, Dan=03-9516743, Rita=06-8201124} No duplicates Unordered Keys (names) Values (phone numbers) Dan 03-9516743 Rita 09-5076452 Leo 08-5530098 06-8201124

Values (phone numbers) SortedMap Example SortedMap <String,String>map = new TreeMap<String,String> (); map.put("Dan", "03-9516743"); map.put("Rita", "09-5076452"); map.put("Leo", "08-5530098"); map.put("Rita", "06-8201124"); System.out.println(map); Output: {Dan=03-9516743, Leo=08-5530098, Rita=06-8201124} lexicographic order Keys (names) Values (phone numbers) Dan 03-9516743 Rita 06-8201124 Leo 08-5530098

Map Collection Views Three views of a Map<K,V> as a collection keySet values entrySet Set<K> Collection<V> Set<Map.Entry<K,V>> The Set of key-value pairs (implement Map.Entry)

Iterating Over the Keys of a Map Map<String,String> map = new HashMap<String,String> (); map.put("Dan", "03-9516743"); map.put("Rita", "09-5076452"); map.put("Leo", "08-5530098"); map.put("Rita", "06-8201124"); for (Iterator<String> iter= map.keySet().iterator(); iter.hasNext(); ) { System.out.println(iter.next()); } Output: Leo Dan Rita

Iterating Over the Keys of a Map Map<String,String> map = new HashMap<String,String> (); map.put("Dan", "03-9516743"); map.put("Rita", "09-5076452"); map.put("Leo", "08-5530098"); map.put("Rita", "06-8201124"); for (String key : map.keySet()) { System.out.println(key); } Output: Leo Dan Rita

Iterating Over the Key-Value Pairs of a Map Map<String,String> map = new HashMap<String,String>(); map.put("Dan", "03-9516743"); map.put("Rita", "09-5076452"); map.put("Leo", "08-5530098"); map.put("Rita", "06-8201124"); for (Iterator<Map.Entry<String,String>> iter= map.entrySet().iterator(); iter.hasNext(); ) { Map.Entry<String,String> entry = iter.next(); System.out.println(entry.getKey() + ": " + entry.getValue()); } Output: Leo: 08-5530098 Dan: 03-9516743 Rita: 06-8201124

Iterating Over the Key-Value Pairs of a Map Map<String,String> map = new HashMap<String,String> (); map.put("Dan", "03-9516743"); map.put("Rita", "09-5076452"); map.put("Leo", "08-5530098"); map.put("Rita", "06-8201124"); for (Map.Entry<String,String> entry: map.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } Output: Leo: 08-5530098 Dan: 03-9516743 Rita: 06-8201124

Collection Algorithms Defined in the Collections class Main algorithms: sort binarySearch reverse shuffle min max

Sorting Arguments: A C D B Output: [A, B, C, D] import the package of List, Collections and Arrays import java.util.*; public class Sort { public static void main(String args[]) { List<String> list = Arrays.asList(args); Collections.sort(list); System.out.println(list); } returns a List-view of its array argument. Arguments: A C D B Output: [A, B, C, D] lexicographic order

Sorting (cont.) Sort a List l by Collections.sort(l); If the list consists of String objects it will be sorted in lexicographic order. Why? String implements Comparable<String>: public interface Comparable<T> { public int compareTo(T o); } Error when sorting a list whose elements do not implement Comparable or are not mutually comparable. User defined comparator Collections.sort(List, Comparator);

Best Practice <with generics> Specify an element type only when a collection is instantiated: Set<String> s = new HashSet<String>(); public void foo(HashSet<String> s){…} public void foo(Set<String> s) {…} s.add() invokes HashSet.add() Interface Implementation Works, but… Better! polymorphism