Posts

Showing posts with the label Collection Framework

Program to demonstrate "HashMap" class in Java's Collection Framework

The  Map  interface maps unique keys to values. A key is an object that you use to retrieve a value at a later date. Given a key and a value, you can store the value in a Map object. After the value is stored, you can retrieve it by using its key. It stores value in pairs i.e key and value we can store data using put( ) method and fetch specific data using get( ) method we can print all data using entrySet( ) method. It has following methods: void clear( ) :  Removes all key/value pairs from the invoking map. boolean containsKey(Object k) :  Returns true if the invoking map contains k as a key. Otherwise, returns false. boolean containsValue(Object v) :  Returns true if the map contains v as a value. Otherwise, returns false.

Program to demonstrate "LinkedList" class in Java's Collection Framework

The  LinkedList  class extends  AbstractSequentialList  and implements the  List  interface. It provides a linked-list data structure. Following are the constructors supported by the LinkedList class. LinkedList( ) This constructor builds an empty linked list. LinkedList(Collection c) This constructor builds a linked list that is initialized with the elements of the collection c. LinkedList defines following methods: void add(int index, Object element) :  Inserts the specified element at the specified position index in the list. boolean add(Object o) :  Appends the specified element to the end of the list.

Program to demonstrate "Queue" class in Java's Collection Framework

The  Queue  interface order elements in a FIFO (first-in-first-out) manner. The  PriorityQueue  class has following methods: boolean add(E e) :  Inserts the specified element into the priority queue. void clear() :  Removes all of the elements from the priority queue. boolean contains(Object o) :  Returns true if the queue contains the specified element. boolean remove(Object o) :  Removes a single instance of the specified element from the queue, if it is present. int size() :  Returns the number of elements in the collection. PROGRAM import java.util.PriorityQueue; import java.util.Queue; class QueueDemo { public static void main(String[] args) { Queue<String> p=new PriorityQueue<String>(); p.add("Apple"); p.add("PineApple"); p.add("Orange"); p.add("Banana"); p.add("Raddish");

Program to demonstrate "Stack" class in Java's Collection Framework

The  Stack  class implements a LIFO (Last In First Out) stack of elements. For example, stack of coins. When you add a new element, it gets stacked on top of the others. When you pull an element off the stack, it removes the element at top. Hence it is called as LIFO (Last In First Out) or FILO (First In Last Out). Stack  is a subclass of Vector that implements a standard last-in, first-out stack. Stack only defines the default constructor, which creates an empty stack. Stack includes all the methods defined by Vector and adds several of its own. Stack class has following methods: boolean empty( ) :  Tests if this stack is empty. Returns true if the stack is empty, and returns false if the stack contains elements. Object peek( ) :  Returns the element on the top of the stack, but does not remove it.

Program to show date and time using only Date class methods

The  java.util.Date  class represents date and time in java. It provides constructors and methods to deal with date and time in java. The java.util.Date class implements Serializable, Cloneable and Comparable  interface. The constructors of java.util.Date class are given below: Date( ) :  Creates a date object representing current date and time. Date(long milliseconds) :  Creates a date object for the given milliseconds since January 1, 1970, 00:00:00 GMT. Some of the methods of Data class are: long getTime( ) :  Returns the time represented by this date object.

Program to demonstrate use of ArrayList class

Arraylist  is a class which implements  List  interface. It is widely used because of the functionality and flexibility it offers. Java ArrayList class uses a dynamic array for storing the elements. The issue with arrays is that they are of fixed length so if it is full we cannot add any more elements to it, likewise if there are number of elements gets removed from it the memory consumption would be the same as it doesn’t shrink. On the other  ArrayList  can dynamically grow and shrink as per the need. Apart from these benefits ArrayList class enables us to use predefined methods of it which makes our task easy. This program shows some of the methods of  ArrayList  class. I'm going to add String elements so I made it of string type as given below: ArrayList<String> al = new ArrayList<String>( );