├── .idea
├── vcs.xml
├── .gitignore
├── misc.xml
└── modules.xml
├── src
└── collections
│ ├── AddJobToQueue.java
│ ├── StudentAgeComparator.java
│ ├── client.java
│ ├── client1.java
│ ├── Student.java
│ └── Client2.java
├── .gitignore
└── Masterclass_Collections_25_july.iml
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Editor-based HTTP Client requests
5 | /httpRequests/
6 | # Datasource local storage ignored files
7 | /dataSources/
8 | /dataSources.local.xml
9 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/src/collections/AddJobToQueue.java:
--------------------------------------------------------------------------------
1 | package collections;
2 |
3 | import java.util.Queue;
4 | import java.util.concurrent.Callable;
5 |
6 | public class AddJobToQueue implements Callable {
7 | Queue queue;
8 | int x;
9 | AddJobToQueue(Queue queue , int x){
10 | this.queue = queue;
11 | }
12 | @Override
13 | public Integer call() {
14 | queue.add(x);
15 | return x;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ### IntelliJ IDEA ###
2 | out/
3 | !**/src/main/**/out/
4 | !**/src/test/**/out/
5 |
6 | ### Eclipse ###
7 | .apt_generated
8 | .classpath
9 | .factorypath
10 | .project
11 | .settings
12 | .springBeans
13 | .sts4-cache
14 | bin/
15 | !**/src/main/**/bin/
16 | !**/src/test/**/bin/
17 |
18 | ### NetBeans ###
19 | /nbproject/private/
20 | /nbbuild/
21 | /dist/
22 | /nbdist/
23 | /.nb-gradle/
24 |
25 | ### VS Code ###
26 | .vscode/
27 |
28 | ### Mac OS ###
29 | .DS_Store
--------------------------------------------------------------------------------
/Masterclass_Collections_25_july.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/collections/StudentAgeComparator.java:
--------------------------------------------------------------------------------
1 | package collections;
2 |
3 | import java.util.Comparator;
4 |
5 | // sort according to desc order of age
6 | public class StudentAgeComparator implements Comparator {
7 | @Override
8 | public int compare(Student o1, Student o2) {
9 | // o1 vs o2 , First arg vs Second arg
10 | // First to win : return -1
11 | // Second to win : return 1
12 | // return 0
13 | if(o1.getAge() < o2.getAge()){
14 | // o2 wins because desc order
15 | return 1;
16 | } else if (o1.getAge() > o2.getAge()){
17 | return -1;
18 | }
19 | return 0;
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/src/collections/client.java:
--------------------------------------------------------------------------------
1 | package collections;
2 |
3 | import java.util.*;
4 | import java.util.concurrent.*;
5 |
6 | public class client {
7 | // private static Map concurrentHashMap = new ConcurrentHashMap<>();
8 | public static void main(String[] args) throws InterruptedException {
9 | Queue queue = new ConcurrentLinkedQueue<>();
10 |
11 | ExecutorService ex = Executors.newCachedThreadPool();
12 | for(int i = 0; i < 10000; i++){
13 | AddJobToQueue x1 = new AddJobToQueue(queue , i);
14 | // behind the scenes use threads
15 | ex.submit(x1);
16 | }
17 | ex.shutdown();
18 | boolean check = ex.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
19 |
20 | System.out.println(queue.size());
21 |
22 | // Runnable task = () -> {
23 | // for (int i = 0; i < 1000; i++) {
24 | // concurrentHashMap.put(Thread.currentThread().getName() + i, i);
25 | // }
26 | // };
27 | //
28 | // Thread thread1 = new Thread(task);
29 | // Thread thread2 = new Thread(task);
30 | //
31 | // thread1.start();
32 | // thread2.start();
33 | //
34 | // thread1.join();
35 | // thread2.join();
36 | //
37 | // System.out.println("Size of ConcurrentHashMap: " + concurrentHashMap.size());
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/collections/client1.java:
--------------------------------------------------------------------------------
1 | package collections;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Collections;
5 | import java.util.List;
6 |
7 | public class client1 {
8 | public static void main(String[] args) {
9 |
10 | List list = new ArrayList<>();
11 | list.add(new Student(24 , 2022 , "Apr23"));
12 | list.add(new Student(25 , 2020 , "Apr24"));
13 | list.add(new Student(21 , 2024 , "Feb23"));
14 |
15 | // Right now we don't how to compare two student objects
16 | // we need to somehow tell how will two students are going to be compared.
17 | Collections.sort(list);
18 |
19 | // asc order of age using Lambda expressions
20 | Collections.sort(list , (o1 , o2) -> {
21 | if(o1.getAge() < o2.getAge()){
22 | return -1;
23 | } else if (o1.getAge() > o2.getAge()){
24 | return 1;
25 | }
26 | return 0;
27 | });
28 |
29 | for(Student student : list){
30 | System.out.println(student.getAge() + " " + student.getGradYear());
31 | }
32 |
33 | List list1 = new ArrayList<>();
34 | list1.add(24);
35 | list1.add(25);
36 | list1.add(21);
37 | list1.add(22);
38 |
39 | // We know how to compare two integers
40 | Collections.sort(list1);
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/collections/Student.java:
--------------------------------------------------------------------------------
1 | package collections;
2 |
3 | public class Student implements Comparable {
4 | private int age;
5 | private int gradYear;
6 | private String batch;
7 |
8 | public Student(int age, int gradYear, String batch) {
9 | this.age = age;
10 | this.gradYear = gradYear;
11 | this.batch = batch;
12 | }
13 |
14 | public int getAge() {
15 | return age;
16 | }
17 |
18 | public void setAge(int age) {
19 | this.age = age;
20 | }
21 |
22 | public int getGradYear() {
23 | return gradYear;
24 | }
25 |
26 | public void setGradYear(int gradYear) {
27 | this.gradYear = gradYear;
28 | }
29 |
30 | public String getBatch() {
31 | return batch;
32 | }
33 |
34 | public void setBatch(String batch) {
35 | this.batch = batch;
36 | }
37 |
38 | // this method is used to give the rules for natural sorting
39 | // We need to sort according to ascending order of grad Year
40 | @Override
41 | public int compareTo(Student other) {
42 | // "this" will be calling the compareTo
43 | // one object is calling the function : this
44 | // one object is passed in the function : other
45 | // this vs other
46 |
47 | // "this" to win (comes first) : return -1
48 | // "other" to win : return 1
49 | // equal : return 0
50 |
51 | if(this.gradYear < other.gradYear) {
52 | return 1;
53 | } else if(this.gradYear > other.gradYear) {
54 | return -1;
55 | }
56 | return 0;
57 | }
58 | }
59 |
60 | // 7 & 4 , asc : 4 will come first , desc : 7 will come first
61 |
62 | // classes and objects
63 | // getters and setters
64 | // interfaces
65 | // generics
66 |
67 | // Comparable helps you to give a natural sorting order to a custom class
68 | // For that you need to implement Comparable interface
69 | // give the definition to compareTo
70 |
71 | // what is the diff between Comparable and Comparator
72 |
73 |
74 | // You are not allowed to make any changes in the existing class
75 | // In this case comparator comes into picture
--------------------------------------------------------------------------------
/src/collections/Client2.java:
--------------------------------------------------------------------------------
1 | package collections;
2 |
3 | import java.util.*;
4 | import java.util.concurrent.CopyOnWriteArrayList;
5 |
6 | public class Client2 {
7 | public static void main(String[] args) {
8 |
9 | // Collection interface
10 |
11 | // List : Ordered collection : sequence
12 |
13 | // ArrayList , LinkedList , Vector , Stack
14 |
15 | // ArrayList : Resizable Array : uses a normal array
16 | // random access : O(1)
17 | List list = new ArrayList();
18 | // dynamic array is implemented behind the scenes
19 | // array size : 4
20 | // 1 2 3 4 : creates another new array of double size
21 | // copy the existing data over there
22 | // 1 2 3 4 _ _ _ _
23 | // 1 2 3 4 5 6 7 8 : size is doubled
24 | // 1 2 3 4 5 6 7 8 _ _ _ _ _ _ _ _
25 | // Amortized Time complexity : O(1) : Homework
26 | list.add(1);
27 | list.add(2);
28 | list.add(3);
29 | list.get(1);
30 | // doubly linked List
31 | List list2 = new LinkedList<>();
32 | // ArrayList and LinkedList are not thread safe
33 | // You might get errors in the case of multiple threads using the same data
34 |
35 | // vector and stack
36 | // thread safe
37 |
38 | List list3 = new Vector<>();
39 | // stack extends vector
40 | // LIFO operations
41 | // Also Thread safe
42 | // Stack DS is implemented using arrays and LL
43 | // Thread safety bring higher time complexity
44 | List list4 = new Stack<>();
45 |
46 | // read about this : Concurrent List : thread safe
47 | List list5 = new CopyOnWriteArrayList<>();
48 |
49 |
50 |
51 | // Queue
52 |
53 | // LinkedList : Doubly , Queue ? Yes
54 | // remove at end , insert at other end
55 |
56 | Queue queue = new LinkedList<>();
57 |
58 | Queue queue2 = new PriorityQueue<>((x , y) -> {
59 | if(x > y) {
60 | return -1;
61 | } else if (x < y) {
62 | return 1;
63 | }
64 | return 0;
65 | });
66 | // COMPARATOR
67 | queue2.add(7);
68 | queue2.add(2);
69 | queue2.add(5);
70 | queue2.add(3);
71 |
72 | while(!queue2.isEmpty()) {
73 | // peek method helps you to check the element at the front
74 | System.out.println(queue2.peek());
75 | queue2.poll();
76 |
77 | }
78 |
79 | // Set
80 | // random order
81 | Set set = new HashSet<>();
82 | // Insertion order : Doubly Linked List + HashMap
83 | Set set2 = new LinkedHashSet<>();
84 | // Sorted order : BBST : Balanced Binary Search tree
85 | // Red Black tree
86 | Set set3 = new TreeSet<>();
87 |
88 | // same diff is between diff type of Hashmap
89 |
90 | set.add(5);
91 | set.add(100);
92 | set.add(3);
93 | set.add(2);
94 | set.add(1);
95 |
96 | set2.add(5);
97 | set2.add(100);
98 | set2.add(3);
99 | set2.add(2);
100 | set2.add(1);
101 |
102 | set3.add(5);
103 | set3.add(100);
104 | set3.add(3);
105 | set3.add(2);
106 | set3.add(1);
107 |
108 | System.out.println(set);
109 | System.out.println(set2);
110 | System.out.println(set3);
111 |
112 |
113 | Map map = new HashMap<>();
114 | map.put("India" , 500);
115 | map.put("America", 600);
116 | map.put("Germany" , 200);
117 |
118 | if(map.containsKey("India")) {
119 | System.out.println(map.get("India"));
120 | }
121 | System.out.println(map.get("Germany"));
122 | }
123 | }
124 |
--------------------------------------------------------------------------------