[PDF] Introduction to the Java collections Framework • Lists • Sets • Maps





Previous PDF Next PDF



Collections en Java

Une collection gère un groupe d'un ensemble d'objets d'un type donné ; ou bien (Java 2) est apparu le framWork de collections qui tout en gardant les ...



Programmation Objet Java–Collections

Java–Collections Dans la bibliothèque Java on retrouve ... Désavantage de l'interface Collection : trop générale ne permet que d'accès basique sur les.



Les collections en Java

Les collections en Java. ? Les interfaces racine Collection et Map. ? Digression 1: les interfaces Java. ? Digression 2: les classes génériques.



VIII- Les collections.pdf

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



Introduction to the Java collections Framework • Lists • Sets • Maps

The Java Collections Framework is a library of classes and interfaces for working with collections of objects. A collection is an object which can store 



Les Collections

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.



Collections et tables de hachage

ensembles : collections d'objets sans répétition de valeurs les collections sont typées depuis java 1.5 une interface définit le contrat des collections.



Chapitre 8 Collections en Java

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 





JAVA : Syntaxe de base

Les fichiers sources (xxx.java) sont transformés en un langage intermédiaire (xxx.class) par un compilateur (commande javac) qui peut être interprété soit par 



[PDF] Collections en Java

Une collection gère un groupe d'un ensemble d'objets d'un type donné ; ou bien c'est un objet qui sert à stocker d'autres objets



[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



[PDF] Cours 8 : Les collections Inspiré du livre de Claude Delannoy - Loria

1 Java Licence Professionnelle CISI 2009-2010 Cours 8 : Les collections Ainsi là encore pour des méthodes de type String File



[PDF] Collections Collections Collections javautilArrayList

Il y a deux manières d'ajouter un élément ? à la fin d'un ArrayList avec la méthode boolean add(Object newElement) ? à une position donnée



[PDF] Les collections Java - LIRMM

Algorithmes Conclusion Les collections Java 1 / 34 Cours Java - F Michel Deque : file où on peut traiter le début ou la fin (e g LIFO)



[PDF] listes Collections - CS-108

1 les listes (lists) collection ordonnée dans laquelle un élément donné peut apparaître plusieurs nombre de collections dans ce qui s'appelle le Java



[PDF] Les collections Les collections

9 jui 2014 · 1 Chapitre 7: Les collections ? Définition ? Les classes: Un vecteur est un objet de type Vector (classe hérité de Java 1 0)



[PDF] Java Collection Framework

Java 5 released! ? Lots of changes about collections From Java 5 all collection interfaces true (or false) provided that no information used in

  • Comment faire une collection en Java ?

    La création d'une collection consiste en son instanciation, et l'utilisation de sa méthode add(T) . Voyons ceci sur un exemple. // création d'une collection de String Collection<String> collection = new ArrayList<String>() ; // ajout d'éléments à cette collection collection. add("un") ; collection.
  • Les collections sont des objets qui permettent de gérer des ensembles d'objets. Ces ensembles de données peuvent être définis avec plusieurs caractéristiques : la possibilité de gérer des doublons, de gérer un ordre de tri, etc. Une collection est un regroupement d'objets qui sont désignés sous le nom d'éléments.

Java Collection Framework

Chapter Topics

Introduction to the Java collections Framework

Lists Sets Maps

The Collections Class

2

The Java Collection Framework

The Java Collections Frameworkis a library of

classes and interfaces for working with collections of objects.

A collectionis an object which can store other

objects, called elements. Collections provide methods for adding and removing elements, and for searching for a particular element within the collection. 3

The Main Types of Collections

Lists Sets Maps 4 Lists Lists: List type collections assign an integer (called an index) to each element stored.

Indices of elements are 0 for the element at the

beginning of the list, 1 for the next element, and so on.

Lists permit duplicate elements, which are

distinguished by their position in the list. 5 Sets Set: a collection with no notion of position within the collection for stored elements. Sets do not permit duplicate elements. 6 Maps

A mapis a collection of pairs of objects:

1.A value: this is the object to be stored.

2.A key: this is another object associated with the value, and which can be

used to quickly find the value within the collection.

A map is really a set of keys, with each each key

having a value attached to it.

Maps do not allow duplicate keys.

7

Part of the JCF Hierarchy

8 9

The Collection Interface

Lists and Sets are similar in many ways.

The CollectionInterface describes the operations that are common to both. Maps are fundamentally different from Lists and Sets and are described by a different interface. 10

Some Methods in the Collection Interface

MethodDescription

add(o : E) : booleanAdds an object oto the Collection. The method returns trueif ois successfully added to the collection, false otherwise. clear() : voidRemoves all elements from the collection. contains(o : Object): boolean Returns trueif ois an element of the collection, false otherwise. isEmpty() : booleanReturns trueif there are no elements in the collection, falseotherwise. iterator() : IteratorReturns an object called an iterator that can be used to examine all elements stored in the collection. remove(o : Object) : boolean Removes the object ofrom the collection and returns trueif the operation is successful, falseotherwise. size() : intReturns the number of elements currently stored in the collection. 11

AbstractCollection

The AbstractCollectionclass provides a skeleton

implementation for a Collectionclass by implementing many of the methods of the

Collectioninterface.

Programmers can create a working collection class

by providing implementations for iterator(), size(), and overriding add(o : Object). 12

Iterators

An iteratoris an object that is associated with a

collection. The iterator provides methods for fetching the elements of the collection, one at a time, in some order.

Iterators have a method for removing from the

collection the last item fetched. 13

The Iterator Interface

Iterators implement the Iteratorinterface. This

interface specifies the following methods: hasNext() : boolean next() : E remove() : void

The remove()method is optional, so not all

iterators have it. 14

Methods of the Iterator Interface

MethodDescription

hasNext() : booleanReturns trueif there is at least one more element from the collection that can be returned, false otherwise. next() : EReturns the next element from the collection. remove() : voidRemoves from the collection the element returned by the last call to next(). This method can be called at least one time for each call to next(). 15

The List Interface

The Listinterface extends the Collectioninterface by adding operations that are specific to the position-based, index-oriented nature of a list. 16

List Interface Methods

The methods in the Listinterface describe

operations for adding elements and removing elements from the list based on the index of the element.

There are also methods for determining the index

of an element in the list when the value of an element is known. 17

The List Interface Methods

add(index:int, el:E) : void Adds the element elto the collection at the given index. Throws IndexOutOfBoundsExceptionif indexis negative, or greater than the size of the list. get(index:int):E Returns the element at the given index, or throws IndexOutBoundsExceptionif indexis negative or greater than or equal to the size of the list. indexOf(o:Object):intReturns the least (first) index at which the object ois found; returns -1 if ois not in the list. lastIndexOf(o:Object):intReturns the greatest (last) index at which the object ois found; returns -1 if ois not in the list. listIterator():ListIterator< E>

Returns an iterator specialized to work with List

collections. remove(index:int):ERemoves and returns the element at the given index; throws IndexOutOfBoundsExceptionif indexis negative, or greater than or equal to the size of the list. set(index:int, el:E):EReplaces the element at indexwith the new element el. 18

AbstractList

This is an abstract class that provides a skeletal implementation of a Listcollection.

It extends AbstractCollectionand implements the

Listinterface.

It serves as the abstract superclass for the concrete classes ArrayListand Vector. 19

ArrayList and Vector

ArrayListand Vectorare array-based lists.

Internally, they use arrays to store their elements: whenever the array gets full, a new, bigger array is created, and the elements are copied to the new array.

Vectorhas higher overhead than ArrayListbecause Vectoris synchronizedto make it safe for use in programs with multiple threads.

20

AbstractSequentialList and LinkedList

Array-based lists have high overhead when elements are being inserted into the list, or removed from the list, at positions that are not at the end of the list. LinkedListis a concrete class that stores elements in a way that eliminates the high overhead of adding to, and removing from positions in the middle of the list. LinkedListextends AbstractSequentialList, which in turn, extends AbstractList. 21

Using the Concrete List Classes

The concrete classes ArrayList, Vector, and LinkedListwork in similar ways, but have different performance characteristics. Because they all implement the Listinterface, you can use Listinterface references to instantiate and refer to the different concrete classes. Using a Listinterface instead of the concrete class reference allows you to later switch to a different concrete class to get better performance. 22

Example: ArrayList

import java.util.*; public class Test public static void main(String [ ] args)

ListnameList = new ArrayList();

String [ ] names = {"Ann", "Bob", "Carol"};

// Add to arrayList for (int k = 0; k < names.length; k++) nameList.add(names[k]); // Display name list for (int k = 0; k < nameList.size(); k++)

System.out.println(nameList.get(k));

23

An Example: LinkedList

Because we used a Listreference to refer to the

concrete class objects, we can easily switch from an ArrayListto a LinkedList: the only change is in the class used to instantiate the collection. 24

Example: LinkedList

import java.util.*; public class Test public static void main(String [ ] args)

ListnameList = new LinkedList();

String [ ] names = {"Ann", "Bob", "Carol"};

// Add to arrayList for (int k = 0; k < names.length; k++) nameList.add(names[k]); // Display name list for (int k = 0; k < nameList.size(); k++)

System.out.println(nameList.get(k));

25

Using an Iterator

To use an iterator with a collection,

1.Call the iterator():Iteratormethod of the collection to retrieve an

iterator object.

2.Use the hasNext():booleanmethod to see if there still remain elements to

be returned, and the next():Emethod to return the next available element.

3.If desired, use the remove():voidmethod to remove the element returned

by next(). 26

The Iterator remove() method

The remove()method removes the element returned

by the last call to next().

The remove()method can be called at most one time

for each call to next(). 27

Using an Iterator

List nameList = new ArrayList();

String [ ] names = {"Ann", "Bob", "Carol"};

// Add to arrayList for (int k = 0; k < names.length; k++) nameList.add(names[k]); // Display name list using an iterator Iterator it = nameList.iterator(); // Get the iterator while (it.hasNext()) // Use the iteratorquotesdbs_dbs44.pdfusesText_44
[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] epi parcours avenir stage

[PDF] l'immigration irlandaise aux etats unis

[PDF] immigration aux etats unis au 20eme siecle