Detailed Small Tasks for Java Collections
Task 1: Remove Duplicates from a List
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 LinkedHashSet<>(inputList); // preserves insertion order
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 of Items
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: Find the Largest Number
Write a method that takes a list of integers and returns the largest number. Use the
Collections class to find the maximum number.
Example:
```java
import java.util.*;
public class FindLargest {
public static int findLargest(List<Integer> numbers) {
return Collections.max(numbers);
}
}
```
Test Case:
```java
List<Integer> numbers = Arrays.asList(1, 3, 7, 2, 8, 5);
System.out.println(findLargest(numbers)); // Output: 8
```
Task 4: Sum of Even Numbers
Write a method that takes a list of integers and calculates the sum of all even numbers in the
list.
Ensure the method handles both positive and negative integers correctly.
Example:
```java
import java.util.*;
public class SumEvenNumbers {
public static int sumEvenNumbers(List<Integer> numbers) {
int sum = 0;
for (int num : numbers) {
if (num % 2 == 0) {
sum += num;
}
}
return sum;
}
}
```
Test Case:
```java
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
System.out.println(sumEvenNumbers(numbers)); // Output: 12 (2 + 4 + 6)
```
Task 5: Reverse a List
Write a method that takes a list of strings and returns a new list with the strings reversed.
You can use a ListIterator to iterate over the list in reverse order.
Example:
```java
import java.util.*;
public class ReverseList {
public static List<String> reverseList(List<String> list) {
List<String> reversed = new ArrayList<>();
ListIterator<String> iterator = list.listIterator(list.size());
while (iterator.hasPrevious()) {
reversed.add(iterator.previous());
}
return reversed;
}
}
```
Test Case:
```java
List<String> list = Arrays.asList("apple", "banana", "cherry");
System.out.println(reverseList(list)); // Output: [cherry, banana, apple]
```