2. Introduction
• Collection :
objet qui regroupe de multiples éléments dans une seule entité.
• Utilisation de collections pour
- stocker, retrouver et manipuler des données
- transmettre des données d ’une méthode à une autre
• Par exemple, un tableau est une collection
2
• Par exemple, un tableau est une collection
• Le JDK fournit d’autres types de collections sous la forme de classes et
d’interfaces
• Ces classes et interfaces sont dans le paquetage java.util
•Avant le JDK 5.0, les collections peuvent contenir des objets de n’importe quel
type
• A partir du JDK 5.0, on peut indiquer le type des objets contenus dans une
collection grâce à la généricitéMouna Torjmen Khemakhem
3. Types de collections
2 hiérarchies principales :
- Collection<E>
- Map<K,V>
• Map correspond aux collections indexées par des clés:
un élément de type V d’une map est retrouvé
rapidement si on connaît sa clé de type Krapidement si on connaît sa clé de type K
3
Mouna Torjmen Khemakhem
• Une classe exemple de Collection: ArrayList
import java.util.ArrayList;
• Une classe exemple de Map: HashMap
import java.util.HashMap;
4. ArrayList
import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListTest {
public static void main(String args[])
ArrayList<String> list = new ArrayList<String>();
//add
list.add("Apple");
list.add("Google");
list.add("Samsung");
list.add("Microsoft");
//checking if ArrayList is empty
System.out.println(« Empty ? " +
list.isEmpty());
// removing an Object from ArrayList
System.out.println(« Before removing : " +
list);
list.remove(3); //removing fourth object
System.out.println("after removing : " + list);
// finding index of Object in List
System.out.println("What is index of Apple: "list.add("Microsoft");
//contains: use of equals method
System.out.println("Does list contains Apple :" +
list.contains("Apple"));
System.out.println("Does list contains Verizon :" +
list.contains("Verizon"));
// size
System.out.println("Size of ArrayList is : " +
list.size());
// replacing an object
System.out.println("before updating : " + list);
list.set(3, "Bank of America");
System.out.println("after update : " + list);
}
System.out.println("What is index of Apple: "
+ list.indexOf("Apple"));
// converting List to Array
String[] array = list.toArray(new String[]{});
System.out.println("Array from ArrayList : " +
Arrays.toString(array));
// removing all elements
list.clear();
System.out.println("Size of ArrayList after
clear : " + list.size());
}
4
Mouna Torjmen Khemakhem
5. Output ArrayList
/*
Does list contains Apple :true
Does list contains Verizon :false
Size of ArrayList is : 4
list before updating : [Apple, Google, Samsung, Microsoft]
list after update : [Apple, Google, Samsung, Bank of America]list after update : [Apple, Google, Samsung, Bank of America]
Does this ArrayList is empty : false
ArrayList before removing element : [Apple, Google, Samsung, Bank of
America]
ArrayList after removing element : [Apple, Google, Samsung]
What is index of Apple in this list : 0
Array from ArrayList : [Apple, Google, Samsung]
Size of ArrayList after clear : 0
*/
http://java67.blogspot.com/2012/11/java-arraylist-example-contains-add-set.htmlMouna Torjmen Khemakhem
6. HashMap
import java.util.HashMap;
import java.util.Iterator;
public class HashMapExample{
public static void main(String args[]){
HashMap hashMap = new HashMap();
// adding value into HashMap
hashMap.put("One", new Integer(1));
hashMap.put("Two", new Integer(2));
hashMap.put("Three", new Integer(3));
System.out.println("HashMap size" + hashMap.size() );
Integer one = (Integer) hashMap.get("One");
System.out.println("Value mapped with key
"One" is " + one);
System.out.println("Retrieving all keys ");
Iterator iterator = hashMap.keySet().iterator();
while(iterator. hasNext()){
System.out.println(iterator.next());
}
if(hashMap.containsValue(new Integer(1))){
System.out.println("HashMap contains 1 as value");
}else{
System.out.println("HashMap does not contain 1 as
value");
}
if( hashMap.containsKey("One") ){
System.out.println("HashMap contains One as key");
}else{
System.out.println("HashMap does not contain One
as value");
}
}
System.out.println("Retrieving all values ");
iterator = hashMap.entrySet().iterator();
while(iterator. hasNext()){
System.out.println(iterator.next());
}
System.out.println( hashMap.remove("One") + "
is removed from the HashMap.");
}
}
6
Mouna Torjmen Khemakhem
7. Output HashMap
/*
OUTPUT of the above given Java HashMap Example would be :
HashMap contains 3 key value pair.
HashMap contains 1 as value
HashMap contains One as key
Value mapped with key "One" is 1
Retrieving all keys from the HashMapRetrieving all keys from the HashMap
Three
Two
One
Retrieving all values from the HashMap
Three=3
Two=2
One=1
1 is removed from the HashMap.
*/
http://www.javadeveloper.co.in/java-example/java-hashmap-example.htmlMouna Torjmen Khemakhem