Java Collections Framework
Java Collections Framework
Java Collections Framework
import java.util.*;
ad.add("Banana");
ad.add("Pineapple");
ad.add("StrawBerry");
ad.offerFirst("Apple");
ad.offerLast("Kiwi");
ad.pollFirst();
System.out.println("Deque Elements: " + ad);
ad.pollLast();
System.out.println("Deque Elements: " + ad);
}
}
import java.util.*;
hs.add(1001);
hs.add(2001);
hs.add(3001);
hs.remove(3001);
for(int i : hs)
System.out.print(i + " ");
import java.util.*;
ts.add(24);
ts.add(66);
ts.add(12);
ts.add(15);
ts.add(30);
ts.add(81);
ts.add(92);
ts.remove(12);
System.out.println("Highest element: " + ts.pollLast());
System.out.println("Lowest element: " + ts.pollFirst());
import java.util.*;
hm.put(1,"AIML");
hm.put(2,"CSE");
hm.put(3,"ECE");
hm.put(4,"EEE");
System.out.println("Map Elements: ");
for(Map.Entry m : hm.entrySet())
System.out.println(m.getKey() + ":" + m.getValue());
hm.putIfAbsent(4,"IT");
System.out.println("Map Elements: ");
for(Map.Entry m : hm.entrySet())
System.out.println(m.getKey() + ":" + m.getValue());
import java.util.*;
tm.put(7301,"Anil");
tm.put(7304,"Pranay");
tm.put(7303,"Ayesha");
tm.put(7302,"Asfiya");
tm.put(501,"Ramu");
tm.remove(501);
System.out.println("Map after removal: " + tm);
import java.util.*;
al.add(1004);
al.add(26);
al.add(794);
al.add(120);
al.add(66);
al.add(576);
al.add(1122);
Collections.sort(al);
System.out.println("Sorted list: " + al);
Collections.shuffle(al);
System.out.println("Shuffled List: " + al);
Collections.shuffle(al);
System.out.println("Shuffled List: " + al);
Collections.reverse(al);
System.out.println("Reverse Elements: " + al);
Arrays.sort(arr);
System.out.println("Element 35 is found at index: " +
Arrays.binarySearch(arr,35)) ;
Arrays.sort(arr);
System.out.println("Sorted array: " + Arrays.toString(arr));
}
}
import java.util.*;
ht.put(1,"Chiru");
ht.put(2,"Nagi");
ht.put(3,"Venky");
ht.put(4,"Srikanth");
ht.remove(4);
System.out.println("Hashtable elements: " + ht);
Enumeration e = ht.keys();
System.out.println("Hashtable traversing using Enumeration interface:
");
while(e.hasMoreElements()) {
int k = (int) e.nextElement();
String v = (String)ht.get(k);
System.out.println(k + " : " + v);
}
}
}
import java.util.*;
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
import java.util.*;
bits1.or(bits2);
System.out.println("OR: " + bits1);
bits1.xor(bits2);
System.out.println("XOR: " + bits1);
import java.util.*;
import java.util.*;
class CalendarClassDemo1 {
public static void main(String[] args) {
String months[] =
{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int year;
System.out.print("Date:");
System.out.print(months[gcal.get(Calendar.MONTH)]);
System.out.print(" " + gcal.get(Calendar.DATE));
System.out.println(year = gcal.get(Calendar.YEAR));
System.out.print("Time:");
System.out.print(gcal.get(Calendar.HOUR) + ":");
System.out.print(gcal.get(Calendar.MINUTE) + ":");
System.out.println(gcal.get(Calendar.SECOND));
if(gcal.isLeapYear(year)) {
System.out.println("The current year is a leap year");
}
else {
System.out.println("The current year is not a leap year");
}
}
}
import java.util.*;
import java.util.*;
al.add(10);
al.add(20);
al.add(30);
System.out.println();
boolean flag = al.contains(100);
System.out.println("100 is Present: " + flag);
System.out.print("List = ");
for(int i : al)
System.out.print(i + " ");
}
}
import java.util.*;
class LinkedListDemo {
public static void main(String[] args) {
LinkedList<String> ll = new LinkedList<>();
ll.add("Vinayak");
ll.add("PraBOSS");
ll.add("Salaar");
ll.add("Kalki");
ll.removeFirst();
System.out.println("List after removing first element: " + ll);
ll.addLast("Meher");
Iterator<String> itr = ll.iterator();
System.out.print("List after modification: ");
while(itr.hasNext()) {
System.out.print(itr.next() + " ");
}
ll.add(1,"Rebel");
itr = ll.iterator();
System.out.print("\nModified List: ");
while(itr.hasNext()) {
System.out.print(itr.next() + " ");
}
ll.removeLast();
itr = ll.iterator();
System.out.print("\nList after removal: ");
while(itr.hasNext()) {
System.out.print(itr.next() + " ");
}
}
}
import java.util.*;
class VectorDemo {
public static void main(String[] args) {
Vector<String> vtr = new Vector<>();
vtr.addElement("JAVA");
vtr.addElement("CPP");
vtr.addElement("PHP");
vtr.remove("PHP");
vtr.add("SWIFT");
import java.util.*;
class StackExample {
public static void main(String[] args) {
Stack<String> s = new Stack<>();
s.push("Rajamouli");
s.push("PrashanthNeel");
s.push("Shankar");
s.push("MeherRamesh");