[PDF] [PDF] Java Collection Framework

java util * ▫ Java 5 released ◇ Lots of changes about collections Collection ▫ Group of elements (references to objects) ▫ It is not specified whether they 



Previous PDF Next PDF





[PDF] Java : les collections

Java Les collections, c'est quoi ? sont des objets permettent de regrouper et List Java List ArrayList LinkedList Vector H H: Research and Training 6 / 50  



[PDF] Collections en Java - Département dinformatique et de recherche

HashSet: les éléments sont rangés suivant une méthode de hachage import java util *; public class SetExample { public static void main(String args[]) { Set 



[PDF] Les collections en Java - Université de Genève

Digression 1: les interfaces Java ❖ Digression 2: les classes génériques ❖ Les collections de données: 1 Les tableaux dynamiques: la classe ArrayList 2



[PDF] Les Collections - LIRMM

Les Collections Cours Java - F Michel Une collection est un objet qui regroupe d'autres un répertoire téléphonique -> une collection de noms associés à 



[PDF] Les Collections - IGM

A quoi cela sert ? ○ Par exemple, java util Arrays sort() demande à ce que le tableau contienne des éléments



[PDF] Les collections - IGM

Il n'existe pas de méthode contains() ou shuffle() qui prend en paramètre un tableau dans java util Arrays mais en utilisant une vue public static void main( String 



[PDF] Programmation Objet Java–Collections - LAMSADE

Ex : import java util *; class ListTest { public static void main(String [] args){ Collection l = new ArrayList(); l add("1"); l add("2"); l add("3"); f(l); }



[PDF] Les collections dans Java 2 Les collections dans Java 2 - Les pages

avoir un sens aussi bien pour les collections autorisant la duplication des éléments (List) que pour celles ne l'autorisant pas (Set) ○ boolean add(Object o ) ○ 



[PDF] Collections Collections Collections javautilArrayList

paquetage java util Peter Sander ESSI-Université de Nice Sophia Antipolis 3 Collections ❍ Problème ○ les tableaux ne répondent pas toujours à tous les



[PDF] Java Collection Framework

java util * ▫ Java 5 released ◇ Lots of changes about collections Collection ▫ Group of elements (references to objects) ▫ It is not specified whether they 

[PDF] parcourir une liste java

[PDF] les collection en java

[PDF] hashtable java open classroom

[PDF] guerre de tranchées date

[PDF] exercices corrigés sur les collections en java pdf

[PDF] java liste vide

[PDF] cours php pdf complet

[PDF] parcours 3éme année du cycle secondaire collégial

[PDF] référentiel parcours avenir

[PDF] contraintes du parcours avenir

[PDF] parcours avenir folios

[PDF] les grandes phases de la seconde guerre mondiale

[PDF] guerre des tranchées 14-18

[PDF] epi parcours avenir stage

[PDF] l'immigration irlandaise aux etats unis

Java Collection Framework

Version March 2009

2

Framework

ƒInterfaces (ADT, Abstract Data Types)

ƒImplementations (of ADT)

ƒAlgorithms (sort)

ƒjava.util.*

ƒJava 5 released!

ŠLots of changes about collections

3

Interfaces

Collection

SetList

Map

SortedSet

SortedMap

Group containers

Associative containers

Queue

Iterable

4

Implementations

TreeSet

TreeMapArray

List

Linked

List

HashMap

Linked

HashMap

Linked

HashSet

HashSet

Map

Sorted

Map

Collection

SetList

Sorted

Set

Queue

Priority

Queue 5

Internals

LinkedList

Linked list

LinkedHashMapTreeMapHashMapMap

ArrayListList

LinkedHashSet TreeSetHashSetSet

Hash table

Linked list

Balanced

tree

Resizable

array Hash table interface data structure classes 6

Collection

ƒGroupof elements (referencesto objects)

ƒIt is not specified whether they are

ŠOrdered / not ordered

ŠDuplicated / not duplicated

ƒFollowing constructors are common to all

classes implementing Collection

ŠT()

ŠT(Collection c)

7

Collection interface

ƒint size()

ƒboolean isEmpty()

ƒboolean contains(Object element)

ƒboolean containsAll(Collection c)

ƒboolean add(Object element)

ƒboolean addAll(Collection c)

ƒboolean remove(Object element)

ƒboolean removeAll(Collection c)

ƒvoid clear()

ƒObject[] toArray()

ƒIterator iterator()

8

Collection example

Collection persons =

new LinkedList(); persons.add QHR 3HUVRQ³$OLŃH´

System.out.println( persons.size() );

Collection copy =

new TreeSet(); copy.addAll(persons);//new TreeSet(persons)

Person[] array = copy.toArray();

System.out.println( array[0] );

9 Map

ƒAn object that associates keys to values

HBJB 661 ҧ 3HUVRQ

ƒKeys and values must be objects

ƒKeysmust be unique

ƒOnly one value per key

ƒFollowing constructors are common to all

collection implementers

ŠT()

ŠT(Map m)

10

Map interface

ƒObject put(Object key, Object value)

ƒObject get(Object key)

ƒObject remove(Object key)

ƒboolean containsKey(Object key)

ƒboolean containsValue(Object value)

ƒpublic Set keySet()

ƒpublic Collection values()

ƒint size()

ƒboolean isEmpty()

ƒvoid clear()

11

Map example

Map people =new HashMap(); people.put ³$IF607´ //ssnQHR 3HUVRQ³$OLŃH´ ³6PLPO´ people.put ³5%7*51´ //ssnQHR 3HUVRQ³5RNHUP´ ³*UHHQ´

Person bob = people.get³5%7*51´

if( bob == null)

6\VPHPBRXPBSULQPOQ ³1RP IRXQG´

int populationSize = people.size(); 12

Generic collections

ƒFrom Java 5, all collection interfaces

and classes have been redefined as

Generics

ƒUse of generics lead to code that is

Šsafer

Šmore compact

Šeasier to understand

Šequally performing

13

Generic list -excerpt

public interface List{ void add(E x);

Iterator iterator();

public interface Iterator{

E next();

booleanhasNext(); 14

Example

ƒUsing a list of Integers

ŠWithout generics ( ArrayListlist )

list.add(0, new Integer(42));intn= ((Integer)(list.get(0))).intValue();

ŠWith generics ( ArrayList list )

list.add(0, new Integer(42));intn= ((Integer)(list.get(0))).intValue();

Š+ autoboxing( ArrayList list )

list.add(0,new Integer(42));inttotal = list.get(0).intValue();

Group containers

(Collections)Collection

SetList

SortedSet

Queue

16 List

ƒCan contain duplicateelements

ƒInsertion orderis preserved

ƒUser can define insertion point

ƒElements can be accessed by position

ƒAugments Collection interface

17

List additional methods

ƒObject get(int index)

ƒObject set(int index, Object element)

ƒvoid add(int index, Object element)

ƒObject remove(int index)

ƒboolean addAll(int index, Collection c)

ƒint indexOf(Object o)

ƒint lastIndexOf(Object o)

ƒList subList(int fromIndex, int toIndex)

18

List implementations

ArrayList

ƒget(n)

ŠConstant time

ƒInsert (beginning)

and delete while iterating

ŠLinear

LinkedList

ƒget(n)

ŠLinear time

ƒInsert (beginning)

and delete while iterating

ŠConstant

19

List implementations

ƒArrayList

ŠArrayList()

ŠArrayList(int initialCapacity)

ŠArrayList(Collection c)

Švoid ensureCapacity(int minCapacity)

ƒLinkedList

Švoid addFirst(Object o)

Švoid addLast(Object o)

ŠObject getFirst()

ŠObject getLast()

ŠObject removeFirst()

ŠObject removeLast()

20

Example I

LinkedListll =

new LinkedList(); ll.add(new Integer(10)); ll.add(new Integer(11)); ll.addLast(new Integer(13)); ll.addFirst(new Integer(20)); //20, 10, 11, 13 21

Example II

Car[] garage = new Car[20];

garage[0] = new Car(); garage[1] = new ElectricCar(); garage[2] = new ElectricCar(); garage[3] = new Car(); for(int i=0; iNull pointer error

Listgarage = new ArrayList(20);

garage.set( 0, new Car() ); garage.set( 1, new ElectricCar() ); garage.set( 2, new ElectricCar() ); garage.set( 3, new Car()); for(int i; iCar c = garage.get(i); c.turnOn(); 22

Example III

List l = new ArrayList(2); // 2 refs to null

l.add(new Integer(11)); // 11 in position 0 l.add(0, new Integer(13)); // 11 in position 1 l.set(0, new Integer(20)); // 13 replaced by 20 l.add(9, new Integer(30)); // NO: out of bounds l.add(new Integer(30)); // OK, size extended 23
Queue

ƒCollection whose elements have an

order (

Šnot and ordered collection though

ƒDefines a headposition where is the

first element that can be accessed

Špeek()

Špoll()

24

Queue implementations

ƒLinkedList

Šhead is the first element of the list

ŠFIFO: Fist-In-First-Out

ƒPriorityQueue

Šhead is the smallest element

25

Queue example

Queue fifo =

new LinkedList();

Queue pq =

new PriorityQueue(); fifo.add(3); pq.add(3); fifo.add(1); pq.add(1); fifo.add(2); pq.add(2);

System.out.println(fifo.peek()); // 3

System.out.println(pq.peek());// 1

26
Set

ƒContains no methods other than those

inherited from Collection

ƒadd()has restriction that no duplicate

elementsare allowed

Še1.equals(e2) == false

e1,e2

ƒIterator

ŠThe elements are traversed in no

particular order

The equals() Contract

It is reflexive: x.equals(x) == true

It is symmetric: x.equals(y) == y.equals(x)

It is transitive: for any reference values x, y, and z, if x.equals(y) == true AND y.equals(z) == true => x.equals(z) == true It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true (or false), provided that no information used in equals comparisons on the object is modified. x.equals(null) == false hashCode

The hashCode() contract

The hashCode() method must consistently

return the same int, if no information used in equals() comparisons on the object is modified.

If two objects are equal for equals() method,

then calling the hashCode() method on the twoquotesdbs_dbs12.pdfusesText_18