Skip to content

Commit 15e6768

Browse files
adding ArrayList and Comparator learnings
1 parent e452544 commit 15e6768

2 files changed

Lines changed: 236 additions & 0 deletions

File tree

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package learnCollections;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collection;
6+
import java.util.Collections;
7+
import java.util.List;
8+
9+
/*
10+
1. Unlike a regular array, an ArrayList can grow and shrink as elements are added or removed.
11+
This dynamic resizing is achieved by creating a new array and copying the elements to the new array.
12+
13+
2. Internally, the ArrayList is implemented as an array of Object references.
14+
When you add elements to an ArrayList, you're essentially storing these elements in this internal array.
15+
16+
3. When you create an ArrayList, it has an initial capacity (default is 10).
17+
The capacity refers to the size of the internal array that can hold elements before needing to resize.
18+
19+
4. When we add an element to an ArrayList, the following steps occur:
20+
- Check Capacity: Before adding the new element, ArrayList checks if there is enough space in the internal array (elementData). If the array is full, it needs to be resized.
21+
- Resize if Necessary: If the internal array is full, ArrayList creates a new array with a larger capacity (usually 1.5 times the current capacity) and copies the elements from the old array to the new array.
22+
- Add the Element: The new element is then added to the internal array at the appropriate index, and the size of the ArrayList is incremented.
23+
24+
5. Resizing the Array:
25+
- Initial Capacity: By default, the initial capacity is 10. This means the internal array can hold 10 elements before it needs to grow.
26+
- Growth Factor: When the internal array is full, a new array is created with a size 1.5 times the old array. This growth factor balances memory efficiency & resizing cost.
27+
- Copying Elements: When resizing occurs, all existing elements are copied from the old array to the new array, which is O(n) operation, where n is the number of elements in the ArrayList.
28+
29+
6. Removing Elements:
30+
- Check Bounds: The ArrayList first checks if the index is within the valid range.
31+
- Remove the Element: The element is removed, and all elements to the right of the removed element are shifted one position to the left to fill the gap.
32+
- Reduce Size: The size of the ArrayList is decremented by one.
33+
*/
34+
35+
public class LearnArrayList {
36+
public static void main(String[] args) {
37+
/*
38+
ArrayList <Integer> list = new ArrayList<>(); // size = 0, capacity = 10
39+
40+
ArrayList <Integer> list2 = new ArrayList<>(1000); // size = 0, capacity = 1000
41+
42+
list.add(1); // 0
43+
list.add(5); // 1
44+
list.add(80); // 2
45+
46+
// remove the element at index 2
47+
list.remove(2);
48+
49+
// add 50 at index 2
50+
list.add(2, 50);
51+
52+
// replace the element at index 2 with 50
53+
list.set(2, 50);
54+
55+
System.out.println(list); // [1, 5, 50]
56+
57+
58+
System.out.println(list.get(2));
59+
System.out.println(list.size());
60+
61+
for(int i = 0; i < list.size(); i++) {
62+
System.out.println(list.get(i));
63+
}
64+
65+
for(int x: list) {
66+
System.out.println(x);
67+
}
68+
69+
System.out.println(list.contains(5));
70+
System.out.println(list.contains(50));
71+
*/
72+
73+
List <String> list = new ArrayList<>();
74+
System.out.println(list.getClass().getName()); // java.util.ArrayList
75+
76+
77+
// returns a fixed-size List backed by the specified array, can't add or remove elements from this list, but can modify existing elements
78+
List <String> list1 = Arrays.asList("Monday", "Tuesday");
79+
System.out.println(list1.getClass().getName()); // java.util.Arrays$ArrayList
80+
list1.set(1, "Wednesday"); // allowed
81+
// list1.add("Thursday"); // throws UnsupportedOperationException
82+
83+
84+
String[] array = {"Apple", "Banana", "Cherry"};
85+
List <String> list2 = Arrays.asList(array);
86+
System.out.println(list2.getClass().getName()); // java.util.Arrays$ArrayList
87+
88+
List <String> list4 = new ArrayList<>(list2); // creates a new ArrayList with the elements of list2, this list is modifiable
89+
list4.add("Mango");
90+
System.out.println(list4);
91+
92+
93+
List <Integer> list3 = List.of(1, 2, 3); // return unmodifiable list, can't add, remove or modify elements
94+
// list3.set(1, 33); // throws UnsupportedOperationException
95+
96+
97+
98+
List <Integer> list5 = new ArrayList<>();
99+
list5.add(1);
100+
list5.add(2);
101+
list5.add(3);
102+
list5.add(0, 0);
103+
104+
List <Integer> list6 = List.of(4, 5, 6, 7, 8, 9);
105+
106+
list5.addAll(list6);
107+
System.out.println(list5); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
108+
109+
110+
111+
112+
List <Integer> list7 = new ArrayList<>();
113+
list7.add(1);
114+
list7.add(2);
115+
list7.add(3);
116+
117+
// list7.remove(1); // removes the element at index 1, which is 2
118+
list7.remove(Integer.valueOf(1)); // removes the object 1, which is at index 0 after the previous removals
119+
System.out.println(list7); // [2, 3]
120+
121+
122+
Collections.sort(list7); // sorts the list in natural order
123+
list7.sort(null); // sorts the list in natural order, same as Collections.sort(list7)
124+
125+
126+
Object[] array1 = list7.toArray();
127+
Integer[] array2 = list7.toArray(new Integer[0]);
128+
129+
130+
List <String> fruits = new ArrayList<>();
131+
fruits.add("Apple");
132+
fruits.add("Apple");
133+
fruits.add("Banana");
134+
fruits.add("Cherry");
135+
fruits.add("Banana");
136+
fruits.add("Date");
137+
138+
fruits.remove("Apple"); // removes the object "Apple", only the first occurrence is removed
139+
140+
System.out.println(fruits); // [Apple, Banana, Cherry, Banana, Date]
141+
}
142+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package learnCollections;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collection;
6+
import java.util.Collections;
7+
import java.util.Comparator;
8+
import java.util.List;
9+
10+
class MyComparator implements Comparator<Integer> {
11+
12+
// if we want to get o1 first then o2, then we should return a -ve number
13+
// if we want to get o1 and o2 at the same position, then we should return 0
14+
// if we want to get o2 first then o1, then we should return a +ve number
15+
@Override
16+
public int compare(Integer o1, Integer o2) {
17+
return o1 - o2;
18+
}
19+
}
20+
21+
class StringLengthComparator implements Comparator<String> {
22+
23+
@Override
24+
public int compare(String o1, String o2) {
25+
return o1.length() - o2.length();
26+
}
27+
}
28+
29+
class Student {
30+
private String name;
31+
private double gpa;
32+
33+
public Student(String name, double gpa) {
34+
this.name = name;
35+
this.gpa = gpa;
36+
}
37+
38+
public String getName() {
39+
return name;
40+
}
41+
42+
public double getGpa() {
43+
return gpa;
44+
}
45+
}
46+
47+
public class LearnComparator {
48+
public static void main(String[] args) {
49+
List <Integer> list = new ArrayList<>();
50+
list.add(2);
51+
list.add(1);
52+
list.add(3);
53+
54+
// list.sort(new MyComparator());
55+
list.sort((a, b) -> a - b); // using lambda expression to sort the list in natural order
56+
System.out.println(list); // [1, 2, 3]
57+
58+
59+
List <String> words = Arrays.asList("banana", "apple", "date");
60+
// words.sort(new StringLengthComparator());
61+
words.sort((s1, s2) -> s1.length() - s2.length()); // using lambda expression to sort the list based on string length
62+
System.out.println(words); // [date, apple, banana]
63+
64+
65+
List <Student> students = new ArrayList<>();
66+
students.add(new Student("Charlie", 3.5));
67+
students.add(new Student("Bob", 3.7));
68+
students.add(new Student("Alice", 3.5));
69+
students.add(new Student("Akshit", 3.9));
70+
71+
Comparator<Student> comparator = Comparator.comparing(Student::getGpa).reversed().thenComparing(Student::getName);
72+
73+
students.sort((o1, o2) -> {
74+
if(o2.getGpa() - o1.getGpa() > 0) {
75+
return 1;
76+
}
77+
else if (o2.getGpa() - o1.getGpa() < 0) {
78+
return -1;
79+
}
80+
else {
81+
return o1.getName().compareTo(o2.getName());
82+
}
83+
});
84+
85+
// students.sort(comparator);
86+
87+
Collections.sort(students, comparator);
88+
89+
for(Student student : students) {
90+
System.out.println(student.getName() + ": " + student.getGpa());
91+
}
92+
93+
}
94+
}

0 commit comments

Comments
 (0)