0% found this document useful (0 votes)
84 views16 pages

Collections Class:-: Utility Classes Examples

This document provides examples of using various utility classes in Java, including Collections, Arrays, Vector, Stack, Hashtable, Properties, StringTokenizer, and BitSet. The Collections class examples demonstrate sorting, shuffling, rotating, and searching lists. The Arrays class examples show converting arrays to lists, sorting arrays, and searching arrays. Examples are also given for Vector, Stack, Hashtable, Properties, StringTokenizer, and BitSet classes.

Uploaded by

Koteswara Rao G
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views16 pages

Collections Class:-: Utility Classes Examples

This document provides examples of using various utility classes in Java, including Collections, Arrays, Vector, Stack, Hashtable, Properties, StringTokenizer, and BitSet. The Collections class examples demonstrate sorting, shuffling, rotating, and searching lists. The Arrays class examples show converting arrays to lists, sorting arrays, and searching arrays. Examples are also given for Vector, Stack, Hashtable, Properties, StringTokenizer, and BitSet classes.

Uploaded by

Koteswara Rao G
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

1 | U ti l i t y C l a s s e s E x a m p l e s

Collections class:-

import java.util.*;

class CollectionsDemo {

public static void main(String[] args) {

ArrayList al = new ArrayList();

al.add(30);

al.add(40);

al.add(10);

al.add(30);

al.add(20);

System.out.println("Initial Contents of List : \n"+al);

Collections.rotate(al,2); // used rotate contents of list by 2 indices.

System.out.println("List contents after rotating elements by 2 : \n"+al);

Collections.sort(al); // sorts al in ascending order.

System.out.println("Sorting list elements in Ascending order : \n"+al);

Collections.shuffle(al); // shuffles the elements of al.

System.out.println("After Shuffling the list contents : \n"+al);

Comparator comp = Collections.reverseOrder(); // Obtain a reverse order

// comparator.

Collections.sort(al,comp); // sorts al using specified comparator.

System.out.println("Sorting list elements in Descending order : \n"+al);

int i = Collections.binarySearch(al,20,comp); // search’s element 20 in list al

// which uses comparator comp.

if(i<0)
2 | U ti l i t y C l a s s e s E x a m p l e s

System.out.println("Element Not Found");

else

System.out.println("Element 20 Found at location : "+i);

Collections.swap(al,1,3); // exchanges elements of index 1 & 3 in al.

System.out.println("List contents After swapping values at 1 and 3 : \n"+al);

System.out.println("Max Element in list is : "+Collections.max(al));

System.out.println("Min Element in list is : "+Collections.min(al));

System.out.println("Frequency of element 30 is : "+Collections.frequency(al,30));

Output :-

Arrays class:-

import java.util.*;

class ArraysDemo {

public static void main(String[] args) {

String[] a = {"CSE","MECH","CIVIL","ECE"};
3 | U ti l i t y C l a s s e s E x a m p l e s

List l = Arrays.asList(a); // Converting array to list.

System.out.println("Contents of List are :\n "+l);

Arrays.sort(a); // Sorting array.

System.out.println("The array contents after sorting :");

for(String s:a)

System.out.print(s+" ");

System.out.println();

// Performing binary search on array "a" which is sorted in Ascending order.

int x = Arrays.binarySearch(a,"CSE");

if(x<0)

System.out.println("Element not found");

else

System.out.println("Element found at location"+x);

String[] b = Arrays.copyOf(a,a.length); // Copying array.

System.out.println("The array b contents are :");

for(String s:b)

System.out.print(s+" ");

System.out.println();

System.out.println("Array a and b are equal : "+Arrays.equals(a,b));

Output :-
4 | U ti l i t y C l a s s e s E x a m p l e s

Vector class:-

import java.util.*;

class VectorDemo {

public static void main(String[] args) {

Vector v = new Vector();

System.out.println("Initial Size is : "+v.size());

System.out.println("Initial Capacity is : "+v.capacity());

v.addElement("B");

v.addElement("D");

v.addElement("A");

v.addElement("C");

v.addElement("E");

v.addElement("G");

System.out.println("After adding elements \n Vector Size is : "+v.size());

System.out.println("Vector Capacity is : "+v.capacity());

v.addElement("H");

v.addElement("F");

v.addElement("J");

v.addElement("I");
5 | U ti l i t y C l a s s e s E x a m p l e s

v.addElement("K");

v.addElement("L");

System.out.println("After Adding More Elements");

System.out.println("Vector Size is : "+v.size());

System.out.println("Vector Capacity is : "+v.capacity());

Enumeration e = v.elements();

while(e.hasMoreElements())

System.out.println((String)e.nextElement());

System.out.println("Vector contains \"E\" : "+v.contains("E"));

Output :-

Stack class:-
6 | U ti l i t y C l a s s e s E x a m p l e s

import java.util.*;

class StackDemo {

public static void main(String[] args) {

Stack s = new Stack();

System.out.println("Stack contents are : "+s);

System.out.println("Initial Size is : "+s.size());

System.out.println("Initial Capacity is : "+s.capacity());

s.push("A");

s.push("D");

s.push("C");

s.push("B");

System.out.println("After Adding Elements");

System.out.println("Stack contents are : "+s);

System.out.println("Popped value from stack is :"+s.pop());

Output :-

Hashtable class:-

import java.util.*;
7 | U ti l i t y C l a s s e s E x a m p l e s

class HashtableDemo {

public static void main(String[] args) {

Hashtable ht = new Hashtable();

ht.put("abc",3434.34);

ht.put("lmn",5656.56);

ht.put("pqr",7878.78);

ht.put("xyz",9191.91);

Set s = ht.keySet();

Iterator itr = s.iterator();

while(itr.hasNext())

String key = (String)itr.next();

System.out.println(key+" : "+ht.get(key));

Output :-

Properties class:-

import java.util.*;

import java.io.*;
8 | U ti l i t y C l a s s e s E x a m p l e s

class PropertiesDemo {

public static void main(String[] args) throws IOException {

Properties pt = new Properties();

FileInputStream fis = new FileInputStream("phonebook.dat");

pt.load(fis);

Set s = pt.stringPropertyNames();

TreeSet ts = new TreeSet(s);

for ( Object name: ts)

String number = pt.getProperty((String)name);

System.out.println(name+" : "+number);

pt.setProperty("Nader","9885098850"); // setting 2 new properties.

pt.setProperty("Ali","9849098490");

FileOutputStream fos = new FileOutputStream("phonebook.dat");

pt.store(fos,"Telephone Book"); // storing contents of properties object into file.

System.out.println();

fis.close();

fos.flush();

fos.close();

Output:
9 | U ti l i t y C l a s s e s E x a m p l e s

Phonebook.dat :-

Newly added
entries into file.

StringTokenizer class:-

import java.util.*;

class StringTokenizerDemo {

public static void main(String[] args) {

String str1 = "Branch:CSE, Course:BE, College:MJCET.";

StringTokenizer st1 = new StringTokenizer(str1);

System.out.println("String tokens using st1");

while (st1.hasMoreTokens())

System.out.println(st1.nextToken());

StringTokenizer st2 = new StringTokenizer(str1,":,");

System.out.println("String tokens using st2");

while (st2.hasMoreTokens())
10 | U ti l i t y C l a s s e s E x a m p l e s

System.out.println(st2.nextToken());

StringTokenizer st3 = new StringTokenizer(str1,":,",true);

System.out.println("String tokens using st3");

while (st3.hasMoreTokens())

System.out.println(st3.nextToken());

Output:-

BitSet class:-

import java.util.*;

class BitSetDemo {

public static void main(String[] args) {

BitSet bs1 = new BitSet(16);


11 | U ti l i t y C l a s s e s E x a m p l e s

BitSet bs2 = new BitSet(16);

for(int i=0;i<16;i++)

if(i%2==0)

bs1.set(i);

if(i%5==0)

bs2.set(i);

System.out.println("Bit Patterns in bs1 are : "+bs1);

System.out.println();

System.out.println("Bit Patterns in bs2 are : "+bs2);

System.out.println();

bs2.and(bs1);

System.out.println("After AND oper's, Bit Patterns in bs2 are : "+bs2);

bs2.xor(bs1);

System.out.println("After XOR oper's, Bit Patterns in bs2 are : "+bs2);

bs2.or(bs1);

System.out.println("After OR oper's, Bit Patterns in bs2 are : "+bs2);

bs2.clear(2);

System.out.println("After clearing index-2 bit, Bit Patterns in bs2 are : "+bs2);

bs2.clear();

System.out.println("After clearing all bits, Bit Patterns in bs2 are : "+bs2);

}
12 | U ti l i t y C l a s s e s E x a m p l e s

Output :-

Date class:-

import java.util.*;

class DateDemo {

public static void main(String[] args) {

Date d1 = new Date();

System.out.println("D1 Details ");

System.out.println(d1);

System.out.println("Time is : "+d1.getTime());

System.out.println("Hours is : "+d1.getHours());

System.out.println("Minutes is : "+d1.getMinutes());

System.out.println("Seconds is : "+d1.getSeconds());

System.out.println("Date is : "+d1.getDate());

Date d2 = new Date();

System.out.println("D2 Details ");

System.out.println(d2);

System.out.println("Time is : "+d2.getTime());

System.out.println("Hours is : "+d2.getHours());
13 | U ti l i t y C l a s s e s E x a m p l e s

System.out.println("Minutes is : "+d2.getMinutes());

System.out.println("Seconds is : "+d2.getSeconds());

System.out.println("Date is : "+d2.getDate());

System.out.println("d1 is an Earlier date :"+d1.before(d2));

System.out.println("d1 is a Later date :"+d1.after(d2));

d1.setHours(14);

d1.setMinutes(34);

d1.setSeconds(20);

d1.setDate(11);

System.out.println("d1 is an Earlier date :"+d1.before(d2));

System.out.println("d1 is a Later date :"+d1.after(d2));

Output :-
14 | U ti l i t y C l a s s e s E x a m p l e s

Calendar class:-

import java.util.*;

class CalendarDemo

public static void main(String[] args)

Calendar c = Calendar.getInstance();

System.out.println("Date is : ");

System.out.println("Month is : "+c.get(Calendar.MONTH));

System.out.println("Date is : "+c.get(Calendar.DATE));

System.out.println("Year is : "+c.get(Calendar.YEAR));

System.out.println("Time is : ");

System.out.println("Hour is : "+c.get(Calendar.HOUR));

System.out.println("Minute is : "+c.get(Calendar.MINUTE));

System.out.println("Second is : "+c.get(Calendar.SECOND));
15 | U ti l i t y C l a s s e s E x a m p l e s

c.set(Calendar.HOUR,11);

c.set(Calendar.MINUTE,32);

c.set(Calendar.SECOND,27);

System.out.println();

System.out.println("Modified Time in Calender instance : ");

System.out.println("Hour is : "+c.get(Calendar.HOUR));

System.out.println("Minute is : "+c.get(Calendar.MINUTE));

System.out.println("Second is : "+c.get(Calendar.SECOND));

Output :-

Timer and TimerTask class:-

import java.util.*;

class MyTimerTask extends TimerTask {

public void run() {

System.out.println("This code should be executed at some future time");


16 | U ti l i t y C l a s s e s E x a m p l e s

class TimerDemo {

public static void main(String[] args) {

MyTimerTask mtt = new MyTimerTask();

Timer t = new Timer();

t.schedule(mtt,1000,2000);

try

Thread.sleep(5000);

catch (InterruptedException e) { e.printStackTrace(); }

t.cancel();

Output :-

You might also like