Skip to content

Commit

Permalink
Java Collection framework
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaspundpal committed Jan 8, 2023
1 parent e721c31 commit b61c271
Show file tree
Hide file tree
Showing 14 changed files with 443 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Collection Framework/LearnArrayDequeue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.*;
public class LearnArrayDequeue {
public static void main(String args[])
{
ArrayDeque<Integer> adq = new ArrayDeque<>();

adq.offer(10);
adq.offerLast(20);
adq.offerFirst(5);
adq.offerLast(50);
System.out.println(adq);

System.out.println(adq.peek());
System.out.println(adq.peekFirst());
System.out.println(adq.peekLast());

System.out.println(adq.poll());
System.out.println(adq);
System.out.println(adq.pollFirst());
System.out.println(adq.pollLast());

}
}
29 changes: 29 additions & 0 deletions Collection Framework/LearnArrayListObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.*;
public class LearnArrayListObject
{
public static void main(String[] args) {
Student s1 = new Student(111,"Pranav",20);
Student s2 = new Student(112,"Tejas",21);
Student s3 = new Student(113,"Psrp",20);
List<Student> st = new ArrayList<Student>();

st.add(s1);
st.add(s2);
st.add(s3);

//System.out.println(st);------>it prints addresses

// Iterator itr=st.iterator();
// while(itr.hasNext()){
// Student s=(Student)itr.next();
// System.out.println(s.rollno+" "+s.name+" "+s.age);
// }

//easy method---->
for (Student s : st) {
System.out.println(s.rollno + " " + s.name + " " + s.age);
}

System.out.println(st);//after using toString in student class it shows correct output
}
}
62 changes: 62 additions & 0 deletions Collection Framework/LearnArraylistOperations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import java.util.*;
public class LearnArraylistOperations
{
public static void main(String args[]){
List<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
list.add(40);
System.out.println(list);

list.add(1,15);
System.out.println(list);

List<Integer> newlist = new ArrayList<>();
newlist.add(50);
newlist.add(60);

list.addAll(newlist);
System.out.println(list);

newlist.clear();
System.out.println(newlist);

System.out.println(list.get(1));
// System.out.println(newlist.get(1));

list.remove(1);
list.remove(Integer.valueOf(60));

list.set(2,25);
System.out.println(list);

System.out.println(list.contains(25));
System.out.println(list.contains(30));
list.add(85);
list.add(60);
list.add(77);
list.add(52);

for(int i = 0;i < list.size();i++){
System.out.println("Element is "+ list.get(i));
}

System.out.println();

for(Integer element:list){
System.out.println("for each element "+ element);
}

System.out.println();

Iterator<Integer> it = list.iterator();
while(it.hasNext()){
System.out.println("Iterator element "+ it.next());
}

Collections.sort(list);

System.out.println(list);
}
}
25 changes: 25 additions & 0 deletions Collection Framework/LearnArraysClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.*;
public class LearnArraysClass {
public static void main(String[] args) {
int[] numbers = {1,3,5,7,9,11,13,15};

int index = Arrays.binarySearch(numbers,5);
System.out.println(index);
int index1 = Arrays.binarySearch(numbers,6);
System.out.println(index1);

int[] numbers1 = {1,31,55,17,91,14,48,73};
Arrays.sort(numbers1);
//System.out.println(numbers1);--------->shows address, no correct o/p
System.out.println(Arrays.toString(numbers1));
for(Integer num:numbers1)
{
System.out.println(num);
}
// Arrays.fill(numbers,25);
// System.out.println(Arrays.toString(numbers));



}
}
43 changes: 43 additions & 0 deletions Collection Framework/LearnCollectionClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.util.*;
public class LearnCollectionClass
{
public static void main(String[] args) {
List<Integer>list = new ArrayList<>();
list.add(25);
list.add(7);
list.add(13);
list.add(42);
list.add(34);
list.add(71);

System.out.println(list);
Collections.sort(list);
System.out.println(list);

System.out.println(Collections.min(list));
System.out.println(Collections.max(list));

Collections.sort(list,Comparator.reverseOrder());
System.out.println(list);

System.out.println(Collections.frequency(list,25));

Student s1 = new Student(111,"Pranav",20);
Student s3 = new Student(113,"Psrp",20);
Student s2 = new Student(112,"Tejas",21);
Student s4 = new Student(102,"PP",21);

List<Student> st = new ArrayList<Student>();

st.add(s4);
st.add(s2);
st.add(s3);
st.add(s1);

System.out.println(st);
Collections.sort(st);
System.out.println(st);
Collections.sort(st,((o1, o2) -> o1.name.compareTo(o2.name)));
System.out.println(st);
}
}
31 changes: 31 additions & 0 deletions Collection Framework/LearnHashMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import javax.swing.text.html.parser.Entity;
import java.util.*;
public class LearnHashMap
{
public static void main(String[] args) {
Map<String,Integer> numbers = new HashMap<>();
numbers.put("one",1);
numbers.put("five",5);
numbers.put("two",2);
numbers.put("three",3);

//numbers.put("two",22); ------> override the old value
//numbers.putIfAbsent("two",22);

System.out.println(numbers);
for(Map.Entry<String,Integer> e: numbers.entrySet()){
System.out.println(e);
}
// for(Map.Entry<String,Integer> e: numbers.entrySet()){
// System.out.println(e.getKey());
// System.out.println(e.getValue());
// }
for(String key: numbers.keySet()){
System.out.println(key);
}
System.out.println(numbers.containsKey("one"));

numbers.replace("one",1,11);
System.out.println(numbers);
}
}
39 changes: 39 additions & 0 deletions Collection Framework/LearnHashset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.*;
public class LearnHashset {
public static void main(String args[]){
Set<Integer> set = new HashSet<>();

set.add(10);
set.add(7);
set.add(25);
set.add(52);
set.add(44);

System.out.println(set);
set.remove(52);
System.out.println(set);

System.out.println(set.contains(25));
System.out.println(set.isEmpty());

set.add(10);
System.out.println(set);

set.remove(22);
System.out.println(set);

Set<Integer> set1 = new HashSet<>();
set1.add(101);
set1.add(150);
set1.add(122);

set.addAll(set1);
System.out.println(set);

set.removeAll(set1);
System.out.println(set);
}
}



24 changes: 24 additions & 0 deletions Collection Framework/LearnHashsetObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.*;

public class LearnHashsetObject
{
public static void main(String[] args) {
Set<Student>st = new HashSet<Student>();
Student s1 = new Student(111,"Pranav",20);
Student s2 = new Student(112,"Tejas",21);
Student s3 = new Student(113,"Psrp",20);
Student s4 = new Student(113,"PR",21);

st.add(s1);
st.add(s2);
st.add(s3);
st.add(s4);

//System.out.println(st);

for(Student s:st)
{
System.out.println(s.name + " " + s.rollno + " " + s.age);
}
}
}
25 changes: 25 additions & 0 deletions Collection Framework/LearnLinkedHashset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.*;
public class LearnLinkedHashset {
public static void main(String args[]){
Set<Integer> set = new LinkedHashSet<>();

set.add(10);
set.add(7);
set.add(25);
set.add(52);
set.add(44);

System.out.println(set);
set.remove(52);
System.out.println(set);

System.out.println(set.contains(25));
System.out.println(set.isEmpty());

set.add(10);
System.out.println(set);
}
}



21 changes: 21 additions & 0 deletions Collection Framework/LearnLinkedListQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import java.util.*;
public class LearnLinkedListQueue {
public static void main(String args[]){
Queue<Integer> que = new LinkedList<>();

que.add(10);
que.add(20);
que.add(30); //add throws exception when error
System.out.println(que);

que.offer(40);
que.offer(50);// use offer while adding element
System.out.println(que);

System.out.println(que.remove()); //throws exception
System.out.println(que.poll());
System.out.println(que.element()); //throws exception
System.out.println(que.peek());
System.out.println(que.isEmpty());
}
}
38 changes: 38 additions & 0 deletions Collection Framework/LearnPriorityQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.*;
public class LearnPriorityQueue {
public static void main(String args[]){
Queue<Integer> pq = new PriorityQueue<>();
pq.offer(40);
pq.offer(20);
pq.offer(50);
pq.offer(30);
pq.offer(10);

System.out.println(pq);
System.out.println(pq.peek());
pq.poll();
System.out.println(pq);

Queue<String> pqs = new PriorityQueue<>();
pqs.offer("Tejas");
pqs.offer("Ketan");
pqs.offer("Prathmesh");
pqs.offer("Abhi");
pqs.offer("Aabhi");

System.out.println(pqs);

Queue<Integer> pqr = new PriorityQueue<>(Comparator.reverseOrder());
pqr.offer(40);
pqr.offer(20);
pqr.offer(50);
pqr.offer(30);
pqr.offer(10);

System.out.println(pqr);
System.out.println(pqr.peek());
pqr.poll();
System.out.println(pqr);

}
}
25 changes: 25 additions & 0 deletions Collection Framework/LearnStackCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.*;
public class LearnStackCollection {
public static void main(String args[])
{
Stack<String> students = new Stack<>();
students.push("Tejas");
students.push("Sharvari");
students.push("Jayashri");

System.out.println(students);

students.pop();
System.out.println(students);

System.out.println(students.isEmpty());
System.out.println(students.peek());

System.out.println(students.empty());

Iterator<String> it = students.iterator();
while(it.hasNext()){
System.out.println("stack elements : "+it.next());
}
}
}
Loading

0 comments on commit b61c271

Please sign in to comment.