Collections in Java
Collections in Java
Key Concepts:
1. sort(List<T> list):
java
Copy code
List<Integer> numbers = Arrays.asList(5, 2, 9, 1, 6);
Collections.sort(numbers);
System.out.println(numbers); // Output: [1, 2, 5, 6, 9]
2. reverse(List<T> list):
java
Copy code
List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
Collections.reverse(numbers);
System.out.println(numbers); // Output: [4, 3, 2, 1]
3. max(Collection<T> collection):
java
Copy code
List<Integer> numbers = Arrays.asList(3, 7, 1, 4);
int maxValue = Collections.max(numbers);
System.out.println(maxValue); // Output: 7
4. min(Collection<T> collection):
java
Copy code
List<Integer> numbers = Arrays.asList(3, 7, 1, 4);
int minValue = Collections.min(numbers);
System.out.println(minValue); // Output: 1
5. binarySearch(List<T> list, T key):
java
Copy code
List<Integer> numbers = Arrays.asList(1, 3, 4, 6, 7);
int index = Collections.binarySearch(numbers, 4);
System.out.println(index); // Output: 2 (the index of 4)
java
Copy code
import java.util.*;
1. arr1.get(i):
o This is used to access the i-th element in the list arr1. Unlike arrays
where you use arr[i], for lists, you use list.get(index).
o Example:
java
Copy code
List<Integer> numbers = Arrays.asList(5, 2, 9);
System.out.println(numbers.get(0)); // Output: 5
2. size():
java
Copy code
List<Integer> numbers = Arrays.asList(5, 2, 9);
System.out.println(numbers.size()); // Output: 3
3. Collections.sort():
java
Copy code
List<Integer> numbers = Arrays.asList(3, 1, 4, 5);
Collections.sort(numbers);
System.out.println(numbers); // Output: [1, 3, 4, 5]
java
Copy code
import java.util.*;
class Solution {
public long minValue(List<Integer> arr1, List<Integer> arr2) {
// Sort arr1 in ascending order
Collections.sort(arr1);
Key Takeaways:
ChatGPT said:
ChatGPT
Yes, in Java, List and other collections (like Set, Map, etc.) provide several
methods to manipulate and work with the elements. Here's a quick overview of
commonly used methods for various collection types:
get(int index):
java
Copy code
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2,
3));
System.out.println(list.get(1)); // Output: 2
add(E element):
java
Copy code
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
System.out.println(list); // Output: [Apple, Banana]
java
Copy code
List<String> list = new ArrayList<>(Arrays.asList("Apple",
"Banana"));
list.add(1, "Orange");
System.out.println(list); // Output: [Apple, Orange,
Banana]
remove(int index):
java
Copy code
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2,
3));
list.remove(1); // Removes element at index 1
System.out.println(list); // Output: [1, 3]
remove(Object o):
o Removes the first occurrence of the specified element from the list.
o Example:
java
Copy code
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2,
3));
list.remove(Integer.valueOf(2)); // Removes element '2'
System.out.println(list); // Output: [1, 3]
o Replaces the element at the specified position in the list with the
specified element.
o Example:
java
Copy code
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2,
3));
list.set(1, 99); // Replaces the element at index 1
System.out.println(list); // Output: [1, 99, 3]
contains(Object o):
java
Copy code
List<String> list = new ArrayList<>(Arrays.asList("Apple",
"Banana"));
System.out.println(list.contains("Banana")); // Output:
true
clear():
java
Copy code
List<String> list = new ArrayList<>(Arrays.asList("Apple",
"Banana"));
list.clear();
System.out.println(list); // Output: []
size():
java
Copy code
List<String> list = new ArrayList<>(Arrays.asList("Apple",
"Banana"));
System.out.println(list.size()); // Output: 2
add(E element):
java
Copy code
Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
System.out.println(set); // Output: [1, 2]
contains(Object o):
remove(Object o):
java
Copy code
Set<String> set = new HashSet<>(Arrays.asList("Apple",
"Banana"));
set.remove("Banana");
System.out.println(set); // Output: [Apple]
size():
java
Copy code
Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3));
System.out.println(set.size()); // Output: 3
o Associates the specified value with the specified key in the map.
o Example:
java
Copy code
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.put("Banana", 2);
System.out.println(map); // Output: {Apple=1, Banana=2}
get(Object key):
o Returns the value to which the specified key is mapped, or null if the
map contains no mapping for the key.
o Example:
java
Copy code
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
System.out.println(map.get("Apple")); // Output: 1
containsKey(Object key):
o Returns true if the map contains a mapping for the specified key.
o Example:
java
Copy code
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
System.out.println(map.containsKey("Apple")); // Output:
true
remove(Object key):
o Removes the mapping for the specified key from the map if present.
o Example:
java
Copy code
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.remove("Apple");
System.out.println(map); // Output: {}
size():
java
Copy code
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
System.out.println(map.size()); // Output: 1
clear():
java
Copy code
Map<String, Integer> map = new HashMap<>();
map.put("Apple", 1);
map.clear();
System.out.println(map); // Output: {}