0% found this document useful (0 votes)
10 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)
10 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
You are on page 1/ 7

import java.util.

ArrayList;
import java.util.Iterator;

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


students.add("Alice");
students.add("Bob");
students.add("Charlie");
students.add("Maria");
System.out.println("Initial list of students: " + students);

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

22
// Step 4: Retrieve and print the second student
String secondStudent = students.get(1); // Index starts at 0
System.out.println("The second student is: " + secondStudent);

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


students.set(1, "Barbara"); // Replace "Bob" with "Barbara"
System.out.println("List after updating the second student: " + students);

// Step 6: Remove the first student


students.remove(0); // Remove the first student (Alice)
System.out.println("List after removing the first student: " + students);

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


if (students.contains("Charlie")) {
System.out.println("Charlie is in the list.");
} else {
System.out.println("Charlie is not in the list.");
}

23
// Step 8: Print the size of the list
System.out.println("The number of students in the list: " + students.size());

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


students.clear();
if (students.isEmpty()) {
System.out.println("The list is now empty.");
}

24
import java.util.HashMap;
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)


map.put(1, "Alice");
map.put(2, "Bob");
map.put(3, "Charlie");
System.out.println("HashMap après les ajouts : " + map);

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


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

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


boolean hasKey = map.containsKey(3);
boolean hasValue = map.containsValue("Charlie");
System.out.println("Contient la clé 3 ? " + hasKey);
System.out.println("Contient la valeur 'Charlie' ? " + hasValue);

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


map.remove(3);
System.out.println("HashMap après suppression de la clé 3 : " + map);

// 6. Taille du HashMap (size)


System.out.println("Taille du HashMap : " + map.size());

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


System.out.println("Parcours des éléments du HashMap :");
for (HashMap.Entry<Integer, String> entry : map.entrySet()) {
System.out.println("Clé : " + entry.getKey() + ", Valeur : " + entry.getValue());
26
}
// 8. Effacer tous les éléments (clear)
map.clear();
System.out.println("HashMap après effacement : " + map);

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


boolean isEmpty = map.isEmpty();
System.out.println("Le HashMap est-il vide ? " + isEmpty);
}
}

27

You might also like