You are on page 1of 22

Contents:-

1. Collection classes in java


- Java Collection class
- Java Collection Framework
- JCF Hierarchy
- Data structures

2. Enumeration
- Enumeration and uses
- Methods in enum

3. Iteration
- Java iteration and uses

4. String tokenizer
- String tokenizer and its methods
5. Gregorian calendar
- Methods of Gregorian calendar

6. Time zone and currency


- Methods of currency
- Time zone class

What is a Java Collection?


A Java Collection is a predefined architecture
capable of storing a group of elements and behaving
like a single unit such as an object or a group.

What is the Java Collection Framework?


Java Collection Framework offers the capability to
Java Collection to represent a group of elements in
classes and Interfaces.
Java Collection Framework enables the user to
perform various data manipulation operations like
storing data, searching, sorting, insertion, deletion,
and updating of data on the group of elements.
JCF Hierarchy
1.ArrayList:

ArrayList is a class implemented using a list


interface, in that provides the functionality of a
dynamic array where the size of the array is not
fixed.

Syntax:

ArrayList<type> var_name = new ArrayList<type>();


2. Vector

Vector is a Part of the collection class that


implements a dynamic array that can grow or shrink
its size as required.

Syntax:

public class Vector<E> extends AbstractList<E>


implements List<E>, RandomAccess, Cloneable,
Serializable
3. Stack

Stack is a part of Java collection class that


models and implements a Stack data structure.
It is based on the basic principle of last-in-first-
out(LIFO) .

Syntax:
public class Stack<E> extends Vector<E>

4. LinkedList
LinkedList class is an implementation of the
LinkedList data structure. It can store the elements
that are not stored in contiguous locations and every
element is a separate object with a different data
part and different address part.

Syntax:
LinkedList name = new LinkedList();
5. HashSet
HashSet is implemented using the Hashtable data
structure. It offers constant time performance for the
performing operations like add, remove, contains,
and size.

Syntax:
public class HashSet<E> extends
AbstractSet<E> implements Set<E>,
Cloneable, Serializable

6.PriorityQueue
The PriorityQueue is based on the priority heap.
The elements of the priority queue are ordered
according to the natural ordering, or by a
Comparator provided at queue construction
time, depending on which constructor is used.
Syntax:
public class PriorityQueue<E> extends
AbstractQueue<E> implements Serializable

7.ArrayDeque

The ArrayDeque class in Java is an


implementation of the Deque interface that uses
a resizable array to store its elements. The
ArrayDeque class provides constant-time
performance for inserting and removing
elements from both ends.

Syntax:

public class ArrayDeque<E> extends


AbstractCollection<E> implements
Deque<E>, Cloneable,Serializable
ENUMERATION:
A Java enumeration is a class type. Although we
don’t need to instantiate an enum using new, it has
the same capabilities as other classes.

This fact makes Java enumeration a very powerful


tool.

Just like classes, you can give them constructors,


add instance variables and methods, and even
implement interfaces.

1. Declaration outside the class


A simple enum example where enum is declared
outside any class (Note enum keyword instead
of class keyword)
Source code:-

enum Color {
RED,
GREEN,
BLUE;
}

public class Test {


public static void main(String[] args)
{
Color c1 = Color.RED;
System.out.println(c1);
}
}
2. Declaration inside a class

Source code:-

public class Test {


enum Color {
RED,
GREEN,
BLUE;
}

public static void main(String[] args)


{
Color c1 = Color.RED;
System.out.println(c1);
}
}
JAVA ITERATOR:
An Iterator is an object that can be used to loop
through collections, like ArrayList and HashSet.
It is called an "iterator" because "iterating" is the
technical term for looping.

To use an Iterator, you must import it from the


java.util package.

Looping Through a Collection:


To loop through a collection, use the hasNext()
and next() methods of the Iterator:

Source code:-

import java.util.ArrayList;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {

// Make a collection
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");

// Get the iterator


Iterator<String> it = cars.iterator();

while(it.hasNext()) {

// Print the first item


System.out.println(it.next());
}
}
STRINGTOKENIZER:-
String tokenizer class in Java is used to break a string into
tokens. A StringTokenizer object internally maintains a current
position within the string to be tokenized.
The String Tokenizer class allows an application to break strings
into tokens. It implements the Enumeration interface. This class is
used for parsing data.
To use String Tokenizer class we have to specify an input string
and a string that contains delimiters. Delimiters are the characters
that separate tokens.
Each character in the delimiter string is considered a valid
delimiter. Default delimiters are whitespaces, new
line, space, and tab.
import java.util.*;
public class StrTkn
{
public static void main(String args[])
{
System.out.println("Using Constructor 1 - ");
StringTokenizer st1 = new StringTokenizer("How are
you", " ");
while (st1.hasMoreTokens())
System.out.println(st1.nextToken());
System.out.println("Using Constructor 2 - ");
StringTokenizer st2 = new StringTokenizer("JAVA :
Code : String", " :");
while (st2.hasMoreTokens())
System.out.println(st2.nextToken());
System.out.println("Using Constructor 3 - ");
StringTokenizer st3 = new StringTokenizer("JAVA :
Code : String", " :", true);
while (st3.hasMoreTokens())
System.out.println(st3.nextToken());
}
}
GREGORIAN CALENDAR :

Gregorian calendar is a concrete subclass of


Calendar and provides the most commonly
used standard calendar system.
The getInstance() method of Calendar class is
used to get the GregorianCalendar object
initialized with the current date and time in the
default locale and time zone. We can also use
new operator with GregorianCalendar class
constructor.

Syntax :

Calendar calendar = Calendar.getInstance();


or
Calendar calendar = new
GregorianCalendar();
Source code:-
import java.util.Calendar;
import java.util.GregorianCalendar;
class CalendarGFG {
public static void main(String[] args)
{
Calendar cal = Calendar.getInstance();
GregorianCalendar gcal = new
GregorianCalendar();
/* Displaying Current Date using
Calendar Class */
System.out.println("Calendar date: "+
cal.getTime());
/* Displaying Current Date using
GregorianCalendar Class */
System.out.print("Gregorian date: "+
gcal.getTime());
}}
TIME ZONE AND CURRENCY:
Java Currency getSymbol() Method
There are two types of getIsntance methods.
Java Currency getSymbol() Method
The getSymbol() is the method of Java Currency
class which is used to get the symbol of the given
currency for the default DISPLAY locale. For
example, the symbol for the US Dollar is "$".

Syntax
public String getSymbol()

Returns
The getSymbol() method is used to get the symbol
of invoking currency for the default locale.
Source code:-

import java.util.Currency;
public class CurrencyGetSymbolExample1 {
public static void main(String args[]) {
// Create a currency for USD
Currency cur = Currency.getInstance("USD");
// Get and print the symbol of the currency
String symbol = cur.getSymbol();
System.out.println("Currency symbol is: " +
symbol);
}
}
JAVA TIMEZONE CLASS
Java TimeZone class represents a time zone offset,
and also figures out daylight savings. It inherits the
Object class.
Java TimeZone class declaration:
public abstract class TimeZone extends Object
implements Serializable, Cloneable
Java TimeZone class Example:

Source code:-

getAvailableIDs()

import java.util.*;
public class TimeZoneExample1 {
public static void main( String args[] ){
String[] id = TimeZone.getAvailableIDs();
System.out.println("In TimeZone class available
Ids are: ");
for (int i=0; i<id.length; i++){
System.out.println(id[i]);
}
}
}
Java TimeZone class Example:

Source code:-

getDisplayName()
FileName: TimeZoneExample4.java

import java.util.*;
public class TimeZoneExample4 {
public static void main( String args[] ){
TimeZone zone = TimeZone.getDefault();
String name = zone.getDisplayName();
System.out.println("Display name for default time
zone: "+ name);
}
}

You might also like