SlideShare a Scribd company logo
2
Most read
3
Most read
5
Most read
Chapitre 3
Les collections
Mouna Torjmen Khemakhem
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
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;
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
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
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
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

More Related Content

PDF
POO Java Chapitre 4 Heritage et Polymorphisme
PDF
POO Java Chapitre 2 Encapsulation
PDF
POO Java Chapitre 6 Exceptions
PDF
Chapitre 6 traitement des exceptions
PDF
POO Java Chapitre 1 Classe & Objet
PDF
Chapitre 5 classes abstraites et interfaces
PDF
Chapitre8: Collections et Enumerations En Java
PDF
Chap 6 : classes et interfaces
POO Java Chapitre 4 Heritage et Polymorphisme
POO Java Chapitre 2 Encapsulation
POO Java Chapitre 6 Exceptions
Chapitre 6 traitement des exceptions
POO Java Chapitre 1 Classe & Objet
Chapitre 5 classes abstraites et interfaces
Chapitre8: Collections et Enumerations En Java
Chap 6 : classes et interfaces

What's hot (20)

PDF
POO Java Introduction
PDF
Support POO Java Deuxième Partie
PDF
Correction Examen 2016-2017 POO .pdf
PDF
Conférence: Catalyseurs de l'Intelligence Artificielle et Écosystème des Fram...
PDF
Corrige tp java
PDF
Support POO Java première partie
PDF
Polymorphisme, interface et classe abstraite
PDF
Cours design pattern m youssfi partie 3 decorateur
PDF
Chapitre 4 heritage et polymorphisme
PDF
PL/SQL:les curseurs
PDF
Programmation orientée objet : Object, classe et encapsulation
PDF
Tp n 4 linux
PDF
Support du cours : Programmation Web 2
PDF
Appalications JEE avec Servlet/JSP
PDF
Exercice 2 java Héritage
PDF
Algorithmique et Structures de Données II
PDF
Support programmation orientée aspect mohamed youssfi (aop)
PPSX
Fondamentaux java
PDF
résumé POO java .pdf
PDF
Support de cours technologie et application m.youssfi
POO Java Introduction
Support POO Java Deuxième Partie
Correction Examen 2016-2017 POO .pdf
Conférence: Catalyseurs de l'Intelligence Artificielle et Écosystème des Fram...
Corrige tp java
Support POO Java première partie
Polymorphisme, interface et classe abstraite
Cours design pattern m youssfi partie 3 decorateur
Chapitre 4 heritage et polymorphisme
PL/SQL:les curseurs
Programmation orientée objet : Object, classe et encapsulation
Tp n 4 linux
Support du cours : Programmation Web 2
Appalications JEE avec Servlet/JSP
Exercice 2 java Héritage
Algorithmique et Structures de Données II
Support programmation orientée aspect mohamed youssfi (aop)
Fondamentaux java
résumé POO java .pdf
Support de cours technologie et application m.youssfi
Ad

Similar to POO Java Chapitre 3 Collections (20)

PPT
A2003822018_21789_17_2018_09. ArrayList.ppt
PPT
WDD_lec_06.ppt
PPT
L11 array list
PPTX
arraylistinjava.pptx
PPTX
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
PPTX
Lecture 9
PPTX
collection framework.pptx
PPTX
ArrayList class and useful methods.pptx
PPT
Collections in Java
PDF
Java ArrayList Tutorial | Edureka
PPTX
Java.util
PPTX
Java util
DOC
Advanced core java
PDF
Java Collections Tutorials
PPTX
VTUOOPMCA5THMODULECollection OverV .pptx
PPTX
mca5thCollection OverViCollection O.pptx
PPTX
VTUOOPMCA5THMODULEvCollection OverV.pptx
PPTX
VTUOOPMCA5THMODULECollection OverVi.pptx
PPTX
Collection and framework
PPT
description of Collections, seaching & Sorting
A2003822018_21789_17_2018_09. ArrayList.ppt
WDD_lec_06.ppt
L11 array list
arraylistinjava.pptx
U-III-part-1.pptxpart 1 of Java and hardware coding questions are answered
Lecture 9
collection framework.pptx
ArrayList class and useful methods.pptx
Collections in Java
Java ArrayList Tutorial | Edureka
Java.util
Java util
Advanced core java
Java Collections Tutorials
VTUOOPMCA5THMODULECollection OverV .pptx
mca5thCollection OverViCollection O.pptx
VTUOOPMCA5THMODULEvCollection OverV.pptx
VTUOOPMCA5THMODULECollection OverVi.pptx
Collection and framework
description of Collections, seaching & Sorting
Ad

More from Mouna Torjmen (6)

PDF
TIC & E-Learning
PDF
Apprentissage Par Projet APP
PDF
Chapitre 4 no sql
PDF
Chapitre 3 spark
PDF
Chapitre 2 hadoop
PDF
Chapitre1 introduction
TIC & E-Learning
Apprentissage Par Projet APP
Chapitre 4 no sql
Chapitre 3 spark
Chapitre 2 hadoop
Chapitre1 introduction

Recently uploaded (20)

PPTX
UNIT 4 Total Quality Management .pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
PPT on Performance Review to get promotions
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Sustainable Sites - Green Building Construction
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PDF
composite construction of structures.pdf
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
DOCX
573137875-Attendance-Management-System-original
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
web development for engineering and engineering
UNIT 4 Total Quality Management .pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
PPT on Performance Review to get promotions
OOP with Java - Java Introduction (Basics)
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Sustainable Sites - Green Building Construction
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
CH1 Production IntroductoryConcepts.pptx
Lecture Notes Electrical Wiring System Components
composite construction of structures.pdf
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Digital Logic Computer Design lecture notes
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
573137875-Attendance-Management-System-original
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
web development for engineering and engineering

POO Java Chapitre 3 Collections

  • 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