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] 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
2Framework
Interfaces (ADT, Abstract Data Types)
Implementations (of ADT)
Algorithms (sort)
java.util.*
Java 5 released!
Lots of changes about collections
3Interfaces
Collection
SetList
Map
SortedSet
SortedMap
Group containers
Associative containers
Queue
Iterable
4 Implementations
TreeSet
TreeMapArray
ListLinked
ListHashMap
Linked
HashMap
Linked
HashSet
HashSet
Map
Sorted
Map
Collection
SetList
Sorted
Set
Queue
Priority
Queue 5Internals
LinkedList
Linked list
LinkedHashMapTreeMapHashMapMap
ArrayListList
LinkedHashSet TreeSetHashSetSet
Hash table
Linked list
Balanced
treeResizable
array Hash table interface data structure classes 6Collection
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 CollectionT()
T(Collection c)
7Collection 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()
8Collection example
Collection persons =
new LinkedListSystem.out.println( persons.size() );
Collection copy =
new TreeSetPerson[] array = copy.toArray();
System.out.println( array[0] );
9 MapAn 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 implementersT()
T(Map m)
10Map 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()
11Map example
MapPerson bob = people.get³5%7*51´
if( bob == null)6\VPHPBRXPBSULQPOQ ³1RP IRXQG´
int populationSize = people.size(); 12Generic collections
From Java 5, all collection interfaces
and classes have been redefined asGenerics
Use of generics lead to code that is
safer
more compact
easier to understand
equally performing
13Generic list -excerpt
public interface ListIterator iterator();
public interface IteratorE next();
booleanhasNext(); 14Example
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)CollectionSetList
SortedSet
Queue
16 List Can contain duplicateelements
Insertion orderis preserved
User can define insertion point
Elements can be accessed by position
Augments Collection interface
17List 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)
18List implementations
ArrayList
get(n)
Constant time
Insert (beginning)
and delete while iteratingLinear
LinkedList
get(n)
Linear time
Insert (beginning)
and delete while iteratingConstant
19List 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()
20Example I
LinkedListll =
new LinkedListExample 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; iListgarage = 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; iExample 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 23Queue
Collection whose elements have an
order (not and ordered collection though
Defines a headposition where is the
first element that can be accessedpeek()
poll()
24Queue implementations
LinkedList
head is the first element of the list
FIFO: Fist-In-First-Out
PriorityQueue
head is the smallest element
25Queue example
Queue fifo =
new LinkedListQueue pq =
new PriorityQueueSystem.out.println(fifo.peek()); // 3
System.out.println(pq.peek());// 1
26Set