Skip to content

Commit b406554

Browse files
authored
Collection Framework
1 parent 625323f commit b406554

14 files changed

Lines changed: 443 additions & 0 deletions

LearnArrayDequeue.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.*;
2+
public class LearnArrayDequeue {
3+
public static void main(String args[])
4+
{
5+
ArrayDeque<Integer> adq = new ArrayDeque<>();
6+
7+
adq.offer(10);
8+
adq.offerLast(20);
9+
adq.offerFirst(5);
10+
adq.offerLast(50);
11+
System.out.println(adq);
12+
13+
System.out.println(adq.peek());
14+
System.out.println(adq.peekFirst());
15+
System.out.println(adq.peekLast());
16+
17+
System.out.println(adq.poll());
18+
System.out.println(adq);
19+
System.out.println(adq.pollFirst());
20+
System.out.println(adq.pollLast());
21+
22+
}
23+
}

LearnArrayListObject.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*;
2+
public class LearnArrayListObject
3+
{
4+
public static void main(String[] args) {
5+
Student s1 = new Student(111,"Pranav",20);
6+
Student s2 = new Student(112,"Tejas",21);
7+
Student s3 = new Student(113,"Psrp",20);
8+
List<Student> st = new ArrayList<Student>();
9+
10+
st.add(s1);
11+
st.add(s2);
12+
st.add(s3);
13+
14+
//System.out.println(st);------>it prints addresses
15+
16+
// Iterator itr=st.iterator();
17+
// while(itr.hasNext()){
18+
// Student s=(Student)itr.next();
19+
// System.out.println(s.rollno+" "+s.name+" "+s.age);
20+
// }
21+
22+
//easy method---->
23+
for (Student s : st) {
24+
System.out.println(s.rollno + " " + s.name + " " + s.age);
25+
}
26+
27+
System.out.println(st);//after using toString in student class it shows correct output
28+
}
29+
}

LearnArraylistOperations.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.util.*;
2+
public class LearnArraylistOperations
3+
{
4+
public static void main(String args[]){
5+
List<Integer> list = new ArrayList<>();
6+
list.add(10);
7+
list.add(20);
8+
list.add(30);
9+
list.add(40);
10+
System.out.println(list);
11+
12+
list.add(1,15);
13+
System.out.println(list);
14+
15+
List<Integer> newlist = new ArrayList<>();
16+
newlist.add(50);
17+
newlist.add(60);
18+
19+
list.addAll(newlist);
20+
System.out.println(list);
21+
22+
newlist.clear();
23+
System.out.println(newlist);
24+
25+
System.out.println(list.get(1));
26+
// System.out.println(newlist.get(1));
27+
28+
list.remove(1);
29+
list.remove(Integer.valueOf(60));
30+
31+
list.set(2,25);
32+
System.out.println(list);
33+
34+
System.out.println(list.contains(25));
35+
System.out.println(list.contains(30));
36+
list.add(85);
37+
list.add(60);
38+
list.add(77);
39+
list.add(52);
40+
41+
for(int i = 0;i < list.size();i++){
42+
System.out.println("Element is "+ list.get(i));
43+
}
44+
45+
System.out.println();
46+
47+
for(Integer element:list){
48+
System.out.println("for each element "+ element);
49+
}
50+
51+
System.out.println();
52+
53+
Iterator<Integer> it = list.iterator();
54+
while(it.hasNext()){
55+
System.out.println("Iterator element "+ it.next());
56+
}
57+
58+
Collections.sort(list);
59+
60+
System.out.println(list);
61+
}
62+
}

LearnArraysClass.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.*;
2+
public class LearnArraysClass {
3+
public static void main(String[] args) {
4+
int[] numbers = {1,3,5,7,9,11,13,15};
5+
6+
int index = Arrays.binarySearch(numbers,5);
7+
System.out.println(index);
8+
int index1 = Arrays.binarySearch(numbers,6);
9+
System.out.println(index1);
10+
11+
int[] numbers1 = {1,31,55,17,91,14,48,73};
12+
Arrays.sort(numbers1);
13+
//System.out.println(numbers1);--------->shows address, no correct o/p
14+
System.out.println(Arrays.toString(numbers1));
15+
for(Integer num:numbers1)
16+
{
17+
System.out.println(num);
18+
}
19+
// Arrays.fill(numbers,25);
20+
// System.out.println(Arrays.toString(numbers));
21+
22+
23+
24+
}
25+
}

LearnCollectionClass.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.*;
2+
public class LearnCollectionClass
3+
{
4+
public static void main(String[] args) {
5+
List<Integer>list = new ArrayList<>();
6+
list.add(25);
7+
list.add(7);
8+
list.add(13);
9+
list.add(42);
10+
list.add(34);
11+
list.add(71);
12+
13+
System.out.println(list);
14+
Collections.sort(list);
15+
System.out.println(list);
16+
17+
System.out.println(Collections.min(list));
18+
System.out.println(Collections.max(list));
19+
20+
Collections.sort(list,Comparator.reverseOrder());
21+
System.out.println(list);
22+
23+
System.out.println(Collections.frequency(list,25));
24+
25+
Student s1 = new Student(111,"Pranav",20);
26+
Student s3 = new Student(113,"Psrp",20);
27+
Student s2 = new Student(112,"Tejas",21);
28+
Student s4 = new Student(102,"PP",21);
29+
30+
List<Student> st = new ArrayList<Student>();
31+
32+
st.add(s4);
33+
st.add(s2);
34+
st.add(s3);
35+
st.add(s1);
36+
37+
System.out.println(st);
38+
Collections.sort(st);
39+
System.out.println(st);
40+
Collections.sort(st,((o1, o2) -> o1.name.compareTo(o2.name)));
41+
System.out.println(st);
42+
}
43+
}

LearnHashMap.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import javax.swing.text.html.parser.Entity;
2+
import java.util.*;
3+
public class LearnHashMap
4+
{
5+
public static void main(String[] args) {
6+
Map<String,Integer> numbers = new HashMap<>();
7+
numbers.put("one",1);
8+
numbers.put("five",5);
9+
numbers.put("two",2);
10+
numbers.put("three",3);
11+
12+
//numbers.put("two",22); ------> override the old value
13+
//numbers.putIfAbsent("two",22);
14+
15+
System.out.println(numbers);
16+
for(Map.Entry<String,Integer> e: numbers.entrySet()){
17+
System.out.println(e);
18+
}
19+
// for(Map.Entry<String,Integer> e: numbers.entrySet()){
20+
// System.out.println(e.getKey());
21+
// System.out.println(e.getValue());
22+
// }
23+
for(String key: numbers.keySet()){
24+
System.out.println(key);
25+
}
26+
System.out.println(numbers.containsKey("one"));
27+
28+
numbers.replace("one",1,11);
29+
System.out.println(numbers);
30+
}
31+
}

LearnHashset.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.*;
2+
public class LearnHashset {
3+
public static void main(String args[]){
4+
Set<Integer> set = new HashSet<>();
5+
6+
set.add(10);
7+
set.add(7);
8+
set.add(25);
9+
set.add(52);
10+
set.add(44);
11+
12+
System.out.println(set);
13+
set.remove(52);
14+
System.out.println(set);
15+
16+
System.out.println(set.contains(25));
17+
System.out.println(set.isEmpty());
18+
19+
set.add(10);
20+
System.out.println(set);
21+
22+
set.remove(22);
23+
System.out.println(set);
24+
25+
Set<Integer> set1 = new HashSet<>();
26+
set1.add(101);
27+
set1.add(150);
28+
set1.add(122);
29+
30+
set.addAll(set1);
31+
System.out.println(set);
32+
33+
set.removeAll(set1);
34+
System.out.println(set);
35+
}
36+
}
37+
38+
39+

LearnHashsetObject.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.*;
2+
3+
public class LearnHashsetObject
4+
{
5+
public static void main(String[] args) {
6+
Set<Student>st = new HashSet<Student>();
7+
Student s1 = new Student(111,"Pranav",20);
8+
Student s2 = new Student(112,"Tejas",21);
9+
Student s3 = new Student(113,"Psrp",20);
10+
Student s4 = new Student(113,"PR",21);
11+
12+
st.add(s1);
13+
st.add(s2);
14+
st.add(s3);
15+
st.add(s4);
16+
17+
//System.out.println(st);
18+
19+
for(Student s:st)
20+
{
21+
System.out.println(s.name + " " + s.rollno + " " + s.age);
22+
}
23+
}
24+
}

LearnLinkedHashset.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.*;
2+
public class LearnLinkedHashset {
3+
public static void main(String args[]){
4+
Set<Integer> set = new LinkedHashSet<>();
5+
6+
set.add(10);
7+
set.add(7);
8+
set.add(25);
9+
set.add(52);
10+
set.add(44);
11+
12+
System.out.println(set);
13+
set.remove(52);
14+
System.out.println(set);
15+
16+
System.out.println(set.contains(25));
17+
System.out.println(set.isEmpty());
18+
19+
set.add(10);
20+
System.out.println(set);
21+
}
22+
}
23+
24+
25+

LearnLinkedListQueue.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.*;
2+
public class LearnLinkedListQueue {
3+
public static void main(String args[]){
4+
Queue<Integer> que = new LinkedList<>();
5+
6+
que.add(10);
7+
que.add(20);
8+
que.add(30); //add throws exception when error
9+
System.out.println(que);
10+
11+
que.offer(40);
12+
que.offer(50);// use offer while adding element
13+
System.out.println(que);
14+
15+
System.out.println(que.remove()); //throws exception
16+
System.out.println(que.poll());
17+
System.out.println(que.element()); //throws exception
18+
System.out.println(que.peek());
19+
System.out.println(que.isEmpty());
20+
}
21+
}

0 commit comments

Comments
 (0)