SlideShare a Scribd company logo
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

What's hot

Polymorphisme (cours, résumé)
Polymorphisme (cours, résumé)Polymorphisme (cours, résumé)
Polymorphisme (cours, résumé)
Anis Bouhachem Djer
 
Correction Examen 2016-2017 POO .pdf
Correction Examen 2016-2017 POO .pdfCorrection Examen 2016-2017 POO .pdf
Correction Examen 2016-2017 POO .pdf
slimyaich3
 
Chapitre5: Classes et objets
Chapitre5: Classes et objetsChapitre5: Classes et objets
Chapitre5: Classes et objets
Aziz Darouichi
 
Exercice 2 java Héritage
Exercice 2  java HéritageExercice 2  java Héritage
Exercice 2 java Héritage
NadaBenLatifa
 
Tp java ee.pptx
Tp java ee.pptxTp java ee.pptx
Tp java ee.pptx
Eric Bourdet
 
Java
JavaJava
Cours de c
Cours de cCours de c
Cours de c
Nada Riahi
 
Cours python
Cours pythonCours python
Cours pythonsalmazen
 
Langage C#
Langage C#Langage C#
Corrige tp java
Corrige tp javaCorrige tp java
Corrige tp java
Maya Medjdoub
 
01 correction-td smia-s2-info2
01 correction-td smia-s2-info201 correction-td smia-s2-info2
01 correction-td smia-s2-info2
L’Université Hassan 1er Settat
 
Chap 6 : classes et interfaces
Chap 6 : classes et interfacesChap 6 : classes et interfaces
Chap 6 : classes et interfaces
Aziz Darouichi
 
La gestion des exceptions avec Java
La gestion des exceptions avec JavaLa gestion des exceptions avec Java
La gestion des exceptions avec Java
Papa Cheikh Cisse
 
Cours c#
Cours c#Cours c#
Cours c#
zan
 
Introduction à JavaScript
Introduction à JavaScriptIntroduction à JavaScript
Introduction à JavaScript
Abdoulaye Dieng
 
Cours c++
Cours c++Cours c++
Cours c++
Nahla BelHaj
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
Jaouad Rachek
 
TP C++ : enoncé
TP C++ : enoncéTP C++ : enoncé
Chapitre 6 traitement des exceptions
Chapitre 6  traitement des exceptionsChapitre 6  traitement des exceptions
Chapitre 6 traitement des exceptions
Amir Souissi
 
UML Part 4- diagrammres de classes et d'objets mansouri
UML Part 4- diagrammres de classes et d'objets mansouriUML Part 4- diagrammres de classes et d'objets mansouri
UML Part 4- diagrammres de classes et d'objets mansouri
Mansouri Khalifa
 

What's hot (20)

Polymorphisme (cours, résumé)
Polymorphisme (cours, résumé)Polymorphisme (cours, résumé)
Polymorphisme (cours, résumé)
 
Correction Examen 2016-2017 POO .pdf
Correction Examen 2016-2017 POO .pdfCorrection Examen 2016-2017 POO .pdf
Correction Examen 2016-2017 POO .pdf
 
Chapitre5: Classes et objets
Chapitre5: Classes et objetsChapitre5: Classes et objets
Chapitre5: Classes et objets
 
Exercice 2 java Héritage
Exercice 2  java HéritageExercice 2  java Héritage
Exercice 2 java Héritage
 
Tp java ee.pptx
Tp java ee.pptxTp java ee.pptx
Tp java ee.pptx
 
Java
JavaJava
Java
 
Cours de c
Cours de cCours de c
Cours de c
 
Cours python
Cours pythonCours python
Cours python
 
Langage C#
Langage C#Langage C#
Langage C#
 
Corrige tp java
Corrige tp javaCorrige tp java
Corrige tp java
 
01 correction-td smia-s2-info2
01 correction-td smia-s2-info201 correction-td smia-s2-info2
01 correction-td smia-s2-info2
 
Chap 6 : classes et interfaces
Chap 6 : classes et interfacesChap 6 : classes et interfaces
Chap 6 : classes et interfaces
 
La gestion des exceptions avec Java
La gestion des exceptions avec JavaLa gestion des exceptions avec Java
La gestion des exceptions avec Java
 
Cours c#
Cours c#Cours c#
Cours c#
 
Introduction à JavaScript
Introduction à JavaScriptIntroduction à JavaScript
Introduction à JavaScript
 
Cours c++
Cours c++Cours c++
Cours c++
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
TP C++ : enoncé
TP C++ : enoncéTP C++ : enoncé
TP C++ : enoncé
 
Chapitre 6 traitement des exceptions
Chapitre 6  traitement des exceptionsChapitre 6  traitement des exceptions
Chapitre 6 traitement des exceptions
 
UML Part 4- diagrammres de classes et d'objets mansouri
UML Part 4- diagrammres de classes et d'objets mansouriUML Part 4- diagrammres de classes et d'objets mansouri
UML Part 4- diagrammres de classes et d'objets mansouri
 

Similar to POO Java Chapitre 3 Collections

Collections framework
Collections frameworkCollections framework
Collections framework
Anand Buddarapu
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS 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)
Aijaz Ali Abro
 
collection framework in java
collection framework in javacollection framework in java
collection framework in java
MANOJ KUMAR
 
12_-_Collections_Framework
12_-_Collections_Framework12_-_Collections_Framework
12_-_Collections_Framework
Krishna Sujeer
 
Java Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptxJava Programming Comprehensive Guide.pptx
Java Programming Comprehensive Guide.pptx
rangariprajwal4554
 
02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt02._Object-Oriented_Programming_Concepts.ppt
02._Object-Oriented_Programming_Concepts.ppt
Yonas D. Ebren
 
Lecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and CollectionsLecture 4 - Object Interaction and Collections
Lecture 4 - Object Interaction and Collections
Syed Afaq Shah MACS CP
 
Java Collections Tutorials
Java Collections TutorialsJava Collections Tutorials
Java Collections Tutorials
Prof. Erwin Globio
 
Collection Framework-1.pptx
Collection Framework-1.pptxCollection Framework-1.pptx
Collection Framework-1.pptx
SarthakSrivastava70
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using C
cpjcollege
 
Core & advanced java classes in mumbai
Core & advanced java classes in mumbaiCore & advanced java classes in mumbai
Core & advanced java classes in mumbai
Vibrant Technologies & Computers
 
Nature Activities Binder _ by Slidesgo.pptx
Nature Activities Binder _ by Slidesgo.pptxNature Activities Binder _ by Slidesgo.pptx
Nature Activities Binder _ by Slidesgo.pptx
IllllBikkySharmaIlll
 
Lists
ListsLists
javacollections.pdf
javacollections.pdfjavacollections.pdf
javacollections.pdf
ManojKandhasamy1
 
Collections (1)
Collections (1)Collections (1)
Collections (1)
abdullah619
 
Collections generic
Collections genericCollections generic
Collections generic
sandhish
 
Python for Beginners
Python  for BeginnersPython  for Beginners
Python for Beginners
DrRShaliniVISTAS
 
Lab Manual-OOP.pdf
Lab Manual-OOP.pdfLab Manual-OOP.pdf
Lab Manual-OOP.pdf
MUNAZARAZZAQELEA
 
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
arpitaeron555
 

Similar to 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
 
Nature Activities Binder _ by Slidesgo.pptx
Nature Activities Binder _ by Slidesgo.pptxNature Activities Binder _ by Slidesgo.pptx
Nature Activities Binder _ by Slidesgo.pptx
 
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
 

More from Mouna Torjmen

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

More from 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
 

Recently uploaded

Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
bjmsejournal
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
artificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptxartificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptx
GauravCar
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
21UME003TUSHARDEB
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
Atif Razi
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
Madan Karki
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
bijceesjournal
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 

Recently uploaded (20)

Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
Design and optimization of ion propulsion drone
Design and optimization of ion propulsion droneDesign and optimization of ion propulsion drone
Design and optimization of ion propulsion drone
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
artificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptxartificial intelligence and data science contents.pptx
artificial intelligence and data science contents.pptx
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
Applications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdfApplications of artificial Intelligence in Mechanical Engineering.pdf
Applications of artificial Intelligence in Mechanical Engineering.pdf
 
Seminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptxSeminar on Distillation study-mafia.pptx
Seminar on Distillation study-mafia.pptx
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 

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