You are on page 1of 11

JAVA COLLECTIONS FRAMEWORK

import java.util.*;

public class ArrayDequeDemo {


public static void main(String[] args) {
ArrayDeque<String> ad = new ArrayDeque<>();

ad.add("Banana");
ad.add("Pineapple");
ad.add("StrawBerry");
ad.offerFirst("Apple");
ad.offerLast("Kiwi");

System.out.println("Deque Elements: " + ad);

ad.pollFirst();
System.out.println("Deque Elements: " + ad);

ad.pollLast();
System.out.println("Deque Elements: " + ad);
}
}

import java.util.*;

public class HashSetDemo {


public static void main(String[] args) {
HashSet<Integer> hs = new HashSet<>();

hs.add(1001);
hs.add(2001);
hs.add(3001);

System.out.println("Hash set elements: " + hs);

hs.add(1001); //Duplicates not allowed

hs.remove(3001);

for(int i : hs)
System.out.print(i + " ");

System.out.println("\nThe HashCode of set is: " + hs.hashCode());


}
}

import java.util.*;

public class TreeSetDemo {


public static void main(String[] args) {
TreeSet<Integer> ts = new TreeSet<>();

ts.add(24);
ts.add(66);
ts.add(12);
ts.add(15);
ts.add(30);
ts.add(81);
ts.add(92);

System.out.println("Tree Set elements: " + ts);

ts.remove(12);
System.out.println("Highest element: " + ts.pollLast());
System.out.println("Lowest element: " + ts.pollFirst());

System.out.println("Modified List: " + ts);


System.out.print("Elements in descending order: ");
Iterator<Integer> itr = ts.descendingIterator();
while(itr.hasNext()){
System.out.print(itr.next() + " ");
}

System.out.print("\nSubset = " + ts.subSet(24,false,81,true));


}
}

import java.util.*;

public class HashMapDemo {


public static void main(String[] args) {
LinkedHashMap <Integer,String> hm = new LinkedHashMap<>();

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());

//Key based removal


hm.remove(4);
System.out.println("Map after removal: " + hm);

//Value based removal


hm.remove(3,"ECE");
System.out.println("Map after removal: " + hm);

System.out.println("Keys in the map: " + hm.keySet());


}
}

import java.util.*;

public class TreeMapDemo {


public static void main(String args[]) {
TreeMap<Integer,String> tm = new TreeMap<>();

tm.put(7301,"Anil");
tm.put(7304,"Pranay");
tm.put(7303,"Ayesha");
tm.put(7302,"Asfiya");
tm.put(501,"Ramu");

System.out.println("Map Elements: ");


for(Map.Entry m : tm.entrySet())
System.out.println(m.getKey() + " -> " + m.getValue());

tm.remove(501);
System.out.println("Map after removal: " + tm);

System.out.println("Head Map: " + tm.headMap(7303,false));

System.out.println("Tail Map: " + tm.tailMap(7303,true));

System.out.println("Sub Map: " + tm.subMap(7301,7304));


System.out.println("Map in descending order: " + tm.descendingMap());
}
}

import java.util.*;

public class CollectionsDemo {


public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<>();

al.add(1004);
al.add(26);
al.add(794);
al.add(120);
al.add(66);
al.add(576);
al.add(1122);

System.out.println("ArrayList Elements: " + al);

Collections.sort(al);
System.out.println("Sorted list: " + al);

System.out.println("Element 576 found at index: " +


Collections.binarySearch(al,576));

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);

System.out.println("Minimum Element in the list: " +


Collections.min(al));
System.out.println("Maximum Element in the list: " +
Collections.max(al));
}
}
import java.util.*;

public class ArraysClassDemo {

public static void main(String[] args)


{
int arr[] = { 10, 20, 15, 22, 35 };

Arrays.sort(arr);
System.out.println("Element 35 is found at index: " +
Arrays.binarySearch(arr,35)) ;

int arr1[] = {10, 15, 22};


System.out.println("Comparing Arrays: " + Arrays.equals(arr,arr1));

System.out.println("The element mismatched index is: " +


Arrays.mismatch(arr,arr1));

Arrays.sort(arr);
System.out.println("Sorted array: " + Arrays.toString(arr));
}
}

import java.util.*;

public class HashtableDemo {


public static void main(String[] args) {
Hashtable<Integer,String> ht = new Hashtable<>();

ht.put(1,"Chiru");
ht.put(2,"Nagi");
ht.put(3,"Venky");
ht.put(4,"Srikanth");

System.out.println("Hashtable elements: " + ht);

ht.remove(4);
System.out.println("Hashtable elements: " + ht);

System.out.println("Hashcode of the table is: " + ht.hashCode());

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.*;

public class StringTokenizerDemo {

public static void main(String args[]) {


StringTokenizer st = new StringTokenizer("Welcome to Java
Programming"," ");

System.out.println("Total no of tokens: " + st.countTokens());

while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}

import java.util.*;

public class BitSetDemo {


public static void main(String[] args) {
BitSet bits1 = new BitSet(8);
BitSet bits2 = new BitSet(8);

// Set some bits in the first BitSet


bits1.set(1);
bits1.set(3);
bits1.set(5);

// Set some bits in the second BitSet


bits2.set(2);
bits2.set(4);
bits2.set(6);

System.out.println("BitSet 1: " + bits1);


System.out.println("BitSet 2: " + bits2);
// Perform logical operations
bits1.and(bits2);
System.out.println("AND: " + bits1);

bits1.or(bits2);
System.out.println("OR: " + bits1);

bits1.xor(bits2);
System.out.println("XOR: " + bits1);

// Check if specific bits are set


System.out.println("Bit at index 3 is set: " + bits1.get(3));

// Count the number of set bits


System.out.println("Number of set bits: " + bits2.cardinality());
}
}

import java.util.*;

public class DateClassDemo {


public static void main(String[] args) {
Date d = new Date();

System.out.println("Today's Date: " + d.toString());


}
}

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;

GregorianCalendar gcal = new GregorianCalendar();

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.*;

public class RandomClassDemo {


public static void main(String[] args) {
Random r = new Random();

// Generate a random integer


System.out.println("Random Numbers: ");
for(int i=1;i<=20;i++) {
int rn = r.nextInt(100);
System.out.println(rn);
}
}
}

import java.util.*;

public class ArrayListEx1 {


public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<>();

al.add(10);
al.add(20);
al.add(30);

System.out.println("List = " + al);


System.out.println(al.get(2));
al.set(0,9);
System.out.print("List = ");
for(int i : al)
System.out.print(i + " ");

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");

System.out.println("Linked List elements: " + ll);

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");

System.out.print("Vector Elements: ");


for(String s : vtr)
System.out.print(s + " ");

vtr.remove("PHP");
vtr.add("SWIFT");

System.out.print("\nVector Elements: ");


for(String s : vtr)
System.out.print(s + " ");

System.out.println("\nSize of the vector = " + vtr.capacity());


}
}

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");

Iterator<String> itr = s.iterator();


System.out.print("Stack Elements: ");
while(itr.hasNext()) {
System.out.print(itr.next() + " ");
}

String poppedElement = s.pop();


System.out.print("\nRemoved ELement = " + poppedElement);
itr = s.iterator();
System.out.print("\nStack Elements: ");
while(itr.hasNext()) {
System.out.print(itr.next() + " ");
}

String topElement = s.peek();


System.out.println("\nTop Element of the stack: " + topElement);
}
}

You might also like