SlideShare une entreprise Scribd logo
1  sur  7
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

Contenu connexe

Tendances

Correction Examen 2016-2017 POO .pdf
Correction Examen 2016-2017 POO .pdfCorrection Examen 2016-2017 POO .pdf
Correction Examen 2016-2017 POO .pdfslimyaich3
 
Chapitre 5 classes abstraites et interfaces
Chapitre 5  classes abstraites et interfacesChapitre 5  classes abstraites et interfaces
Chapitre 5 classes abstraites et interfacesAmir Souissi
 
Chapitre 6 traitement des exceptions
Chapitre 6  traitement des exceptionsChapitre 6  traitement des exceptions
Chapitre 6 traitement des exceptionsAmir Souissi
 
Chapitre 3 elements de base de java
Chapitre 3  elements de base de javaChapitre 3  elements de base de java
Chapitre 3 elements de base de javaAmir Souissi
 
Héritage et polymorphisme- Jihen HEDHLI
Héritage et polymorphisme- Jihen HEDHLIHéritage et polymorphisme- Jihen HEDHLI
Héritage et polymorphisme- Jihen HEDHLIJihenHedhli1
 
Exercice 1 java Héritage
Exercice 1 java HéritageExercice 1 java Héritage
Exercice 1 java HéritageNadaBenLatifa
 
Algorithmique et Structures de Données II
Algorithmique et Structures de Données IIAlgorithmique et Structures de Données II
Algorithmique et Structures de Données IIRiadh Bouslimi
 
Exercices uml-corrige
Exercices uml-corrigeExercices uml-corrige
Exercices uml-corrigeAmineMouhout1
 
c# programmation orientée objet (Classe & Objet)
c# programmation orientée objet (Classe & Objet)c# programmation orientée objet (Classe & Objet)
c# programmation orientée objet (Classe & Objet)Mahfoud EL HOUDAIGUI
 
Fondamentaux java
Fondamentaux javaFondamentaux java
Fondamentaux javaInes Ouaz
 
UML Part 3- diagramme de séquences mansouri
UML Part 3- diagramme de séquences mansouriUML Part 3- diagramme de séquences mansouri
UML Part 3- diagramme de séquences mansouriMansouri Khalifa
 
Introduction à l’orienté objet en Python
Introduction à l’orienté objet en PythonIntroduction à l’orienté objet en Python
Introduction à l’orienté objet en PythonAbdoulaye Dieng
 
Chapitre 2 complexité
Chapitre 2 complexitéChapitre 2 complexité
Chapitre 2 complexitéSana Aroussi
 
Diagramme de Séquence
Diagramme de SéquenceDiagramme de Séquence
Diagramme de SéquenceabdoMarocco
 
chap2 algorithme de recherche.pdf
chap2 algorithme de recherche.pdfchap2 algorithme de recherche.pdf
chap2 algorithme de recherche.pdfdonixwm
 

Tendances (20)

Correction Examen 2016-2017 POO .pdf
Correction Examen 2016-2017 POO .pdfCorrection Examen 2016-2017 POO .pdf
Correction Examen 2016-2017 POO .pdf
 
Polymorphisme (cours, résumé)
Polymorphisme (cours, résumé)Polymorphisme (cours, résumé)
Polymorphisme (cours, résumé)
 
Chapitre 5 classes abstraites et interfaces
Chapitre 5  classes abstraites et interfacesChapitre 5  classes abstraites et interfaces
Chapitre 5 classes abstraites et interfaces
 
Chapitre 6 traitement des exceptions
Chapitre 6  traitement des exceptionsChapitre 6  traitement des exceptions
Chapitre 6 traitement des exceptions
 
Chapitre 3 elements de base de java
Chapitre 3  elements de base de javaChapitre 3  elements de base de java
Chapitre 3 elements de base de java
 
COURS_PYTHON_22.ppt
COURS_PYTHON_22.pptCOURS_PYTHON_22.ppt
COURS_PYTHON_22.ppt
 
Héritage et polymorphisme- Jihen HEDHLI
Héritage et polymorphisme- Jihen HEDHLIHéritage et polymorphisme- Jihen HEDHLI
Héritage et polymorphisme- Jihen HEDHLI
 
Exercice 1 java Héritage
Exercice 1 java HéritageExercice 1 java Héritage
Exercice 1 java Héritage
 
TP C++ : Correction
TP C++ : CorrectionTP C++ : Correction
TP C++ : Correction
 
Introduction à Python
Introduction à PythonIntroduction à Python
Introduction à Python
 
Algorithmique et Structures de Données II
Algorithmique et Structures de Données IIAlgorithmique et Structures de Données II
Algorithmique et Structures de Données II
 
Exercices uml-corrige
Exercices uml-corrigeExercices uml-corrige
Exercices uml-corrige
 
c# programmation orientée objet (Classe & Objet)
c# programmation orientée objet (Classe & Objet)c# programmation orientée objet (Classe & Objet)
c# programmation orientée objet (Classe & Objet)
 
Fondamentaux java
Fondamentaux javaFondamentaux java
Fondamentaux java
 
Polymorphisme, interface et classe abstraite
Polymorphisme, interface et classe abstraitePolymorphisme, interface et classe abstraite
Polymorphisme, interface et classe abstraite
 
UML Part 3- diagramme de séquences mansouri
UML Part 3- diagramme de séquences mansouriUML Part 3- diagramme de séquences mansouri
UML Part 3- diagramme de séquences mansouri
 
Introduction à l’orienté objet en Python
Introduction à l’orienté objet en PythonIntroduction à l’orienté objet en Python
Introduction à l’orienté objet en Python
 
Chapitre 2 complexité
Chapitre 2 complexitéChapitre 2 complexité
Chapitre 2 complexité
 
Diagramme de Séquence
Diagramme de SéquenceDiagramme de Séquence
Diagramme de Séquence
 
chap2 algorithme de recherche.pdf
chap2 algorithme de recherche.pdfchap2 algorithme de recherche.pdf
chap2 algorithme de recherche.pdf
 

Similaire à POO Java Chapitre 3 Collections

Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Aijaz Ali Abro
 
collection framework in java
collection framework in javacollection framework in java
collection framework in javaMANOJ KUMAR
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_FrameworkKrishna Sujeer
 
Java Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptxJava Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptxrangariprajwal4554
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.pptYonas D. Ebren
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsSyed Afaq Shah MACS CP
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using Ccpjcollege
 
Collections generic
Collections genericCollections generic
Collections genericsandhish
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfIntroduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfarpitaeron555
 

Similaire à POO Java Chapitre 3 Collections (20)

Collections framework
Collections frameworkCollections framework
Collections framework
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)
 
collection framework in java
collection framework in javacollection framework in java
collection framework in java
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_Framework
 
Java Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptxJava Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptx
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and Collections
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
 
Collection Framework-1.pptx
Collection Framework-1.pptxCollection Framework-1.pptx
Collection Framework-1.pptx
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using C
 
Core & advanced java classes in mumbai
Core & advanced java classes in mumbaiCore & advanced java classes in mumbai
Core & advanced java classes in mumbai
 
Lists
ListsLists
Lists
 
javacollections.pdf
javacollections.pdfjavacollections.pdf
javacollections.pdf
 
Collections (1)
Collections (1)Collections (1)
Collections (1)
 
Collections generic
Collections genericCollections generic
Collections generic
 
Python for Beginners
Python  for BeginnersPython  for Beginners
Python for Beginners
 
Lab Manual-OOP.pdf
Lab Manual-OOP.pdfLab Manual-OOP.pdf
Lab Manual-OOP.pdf
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfIntroduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdf
 
C# Collection classes
C# Collection classesC# Collection classes
C# Collection classes
 

Plus de Mouna Torjmen

Plus de Mouna Torjmen (6)

TIC & E-Learning
TIC & E-LearningTIC & E-Learning
TIC & E-Learning
 
Apprentissage Par Projet APP
Apprentissage Par Projet APPApprentissage Par Projet APP
Apprentissage Par Projet APP
 
Chapitre 4 no sql
Chapitre 4 no sqlChapitre 4 no sql
Chapitre 4 no sql
 
Chapitre 3 spark
Chapitre 3 sparkChapitre 3 spark
Chapitre 3 spark
 
Chapitre 2 hadoop
Chapitre 2 hadoopChapitre 2 hadoop
Chapitre 2 hadoop
 
Chapitre1 introduction
Chapitre1 introductionChapitre1 introduction
Chapitre1 introduction
 

Dernier

Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfrs7054576148
 

Dernier (20)

Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 

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