0% found this document useful (0 votes)
28 views7 pages

Exercices Java Collection

Uploaded by

Eya KHammar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views7 pages

Exercices Java Collection

Uploaded by

Eya KHammar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

ArrayList;
import [Link];

public class ArrayListDemo {


public static void main(String[] args) {
// Step 1: Create an ArrayList to store student names

// Step 2: Add 4 students to the list

// Step 3: Use Iterator to replace each name with its uppercase version

// Step 4: Retrieve and print the second student

// Step 5: Update the second student name with "Barbara"

// Step 6: Remove the first student

// Step 7: Check if a student exists in the list

// Step 8: Print the size of the list


21
// Step 9: Clear the list and check if it's empty
// Step 1: Create an ArrayList to store student names
ArrayList<String> students = new ArrayList<>();

// Step 2: Add 4 students to the list


[Link]("Alice");
[Link]("Bob");
[Link]("Charlie");
[Link]("Maria");
[Link]("Initial list of students: " + students);

// Step 3: Use Iterator to replace each name with its uppercase version
Iterator<String> iterator = [Link]();
int index = 0; // To keep track of indices while iterating
while ([Link]()) {
String name = [Link](); // Get the current name
[Link](index, [Link]()); // Replace with uppercase
index++;
}
[Link]("list of students in upper case: " + students);

22
// Step 4: Retrieve and print the second student
String secondStudent = [Link](1); // Index starts at 0
[Link]("The second student is: " + secondStudent);

// Step 5: Update the second student name with "Barbara"


[Link](1, "Barbara"); // Replace "Bob" with "Barbara"
[Link]("List after updating the second student: " + students);

// Step 6: Remove the first student


[Link](0); // Remove the first student (Alice)
[Link]("List after removing the first student: " + students);

// Step 7: Check if a student exists in the list


if ([Link]("Charlie")) {
[Link]("Charlie is in the list.");
} else {
[Link]("Charlie is not in the list.");
}

23
// Step 8: Print the size of the list
[Link]("The number of students in the list: " + [Link]());

// Step 9: Clear the list and check if it's empty


[Link]();
if ([Link]()) {
[Link]("The list is now empty.");
}

24
import [Link];
public class HashMapDemo {
public static void main(String[] args) {

// Création d'un HashMap


HashMap<Integer, String> map = new HashMap<>();

// 1. Ajouter des éléments (put)


[Link](1, "Alice");
[Link](2, "Bob");
[Link](3, "Charlie");
[Link]("HashMap après les ajouts : " + map);

// 2. Remplacer une valeur pour une clé existante


[Link](2, "David");
[Link]("HashMap après remplacement de Bob par David : " + map);
25
// 3. Obtenir une valeur avec une clé (get)
String value = [Link](1);
[Link]("Valeur pour la clé 1 : " + value);

// 4. Vérifier si une clé ou une valeur existe (containsKey, containsValue)


boolean hasKey = [Link](3);
boolean hasValue = [Link]("Charlie");
[Link]("Contient la clé 3 ? " + hasKey);
[Link]("Contient la valeur 'Charlie' ? " + hasValue);

// 5. Supprimer une paire clé-valeur (remove)


[Link](3);
[Link]("HashMap après suppression de la clé 3 : " + map);

// 6. Taille du HashMap (size)


[Link]("Taille du HashMap : " + [Link]());

// 7. Parcourir le HashMap avec une boucle (entrySet)


[Link]("Parcours des éléments du HashMap :");
for ([Link]<Integer, String> entry : [Link]()) {
[Link]("Clé : " + [Link]() + ", Valeur : " + [Link]());
26
}
// 8. Effacer tous les éléments (clear)
[Link]();
[Link]("HashMap après effacement : " + map);

// 9. Vérifier si le HashMap est vide (isEmpty)


boolean isEmpty = [Link]();
[Link]("Le HashMap est-il vide ? " + isEmpty);
}
}

27

You might also like