Java Collections Tasks Based on PPT
Task 1: Remove Duplicates Using HashSet
Write a method that takes a list of integers and removes any duplicate values.
Use a HashSet to remove the duplicates. Make sure to return the result as a new list,
preserving the original order of the elements in the input list.
Example:
```java
import java.util.*;
public class RemoveDuplicates {
public static List<Integer> removeDuplicates(List<Integer> inputList) {
Set<Integer> set = new HashSet<>(inputList); // removes duplicates
return new ArrayList<>(set);
}
}
```
Test Case:
```java
List<Integer> inputList = Arrays.asList(1, 2, 2, 3, 4, 4, 5);
System.out.println(removeDuplicates(inputList)); // Output: [1, 2, 3, 4, 5]
```
Task 2: Counting Occurrences Using HashMap
Write a method that takes a list of strings and counts how many times each string appears.
Use a HashMap where the keys are the strings, and the values are the counts. Ensure the
method handles an empty list correctly.
Example:
```java
import java.util.*;
public class CountOccurrences {
public static Map<String, Integer> countOccurrences(List<String> words) {
Map<String, Integer> map = new HashMap<>();
for (String word : words) {
map.put(word, map.getOrDefault(word, 0) + 1);
}
return map;
}
}
```
Test Case:
```java
List<String> words = Arrays.asList("apple", "banana", "apple", "orange", "banana",
"banana");
System.out.println(countOccurrences(words)); // Output: {apple=2, banana=3, orange=1}
```
Task 3: Generate Random Numbers Using Random Class
Write a method that generates a random number between 1 and 100 using the Random
class.
Ensure that each time the method is called, it returns a different number.
Example:
```java
import java.util.*;
public class GenerateRandom {
public static int generateRandomNumber() {
Random rand = new Random();
return rand.nextInt(100) + 1; // generates a number between 1 and 100
}
}
```
Test Case:
```java
System.out.println(generateRandomNumber()); // Output: random number between 1 and
100
```
Task 4: Checking if a Word is in the Map
Write a method that checks if a word is in the HashMap created in the previous task.
If the word exists, return the count of occurrences, otherwise return 0.
Example:
```java
import java.util.*;
public class CheckWordInMap {
public static int checkWord(Map<String, Integer> map, String word) {
return map.getOrDefault(word, 0);
}
}
```
Test Case:
```java
Map<String, Integer> map = countOccurrences(Arrays.asList("apple", "banana", "apple"));
System.out.println(checkWord(map, "banana")); // Output: 1
System.out.println(checkWord(map, "cherry")); // Output: 0
```
Task 5: Print All Entries from HashMap
Write a method that prints out all entries in the HashMap, showing each word and its
corresponding count.
Example:
```java
import java.util.*;
public class PrintEntries {
public static void printEntries(Map<String, Integer> map) {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
```
Test Case:
```java
Map<String, Integer> map = countOccurrences(Arrays.asList("apple", "banana", "apple"));
printEntries(map);
// Output:
// apple: 2
// banana: 1
```