Essential Java.util Classes

shabbir's Avatar author of Essential Java.util Classes
Java.util package contains the collections framework, legacy collection classes, event model, date and time facilities, and miscellaneous utility classes..
Java.util package contains the collections framework, legacy collection classes, event model, date and time facilities, and miscellaneous utility classes.

Java.util.ArrayList Class



The java.util.ArrayList class provides resizable-array and implements the List interface.
  • It implements all optional list operations and it also permits all elements, including null.
  • It provides methods to manipulate the size of the array that is used internally to store the list.
Class declaration
Code:
public class ArrayList<E>
   extends AbstractList<E>
      implements List<E>, RandomAccess, Cloneable, Serializable
Here <E> represents an Element. For example, if you're building an array list of Integers then you'd initialize it as
ArrayList<Integer> list = new ArrayList<Integer>();

Class constructors
  1. ArrayList() - Empty list is created having an initial capacity sufficient to hold 10 elements.
  2. ArrayList(Collection<? extends E> c) - List containing the elements of the specified collection is created.
  3. ArrayList(int initialCapacity) - Empty list is created with an initial capacity.
Class methods
  1. boolean add(E e) - Specified element is appended to the end of this list.
  2. void add(int index, E element) - Specified element is inserted at the specified position in this list.
  3. boolean addAll(int index, Collection<? extends E> c) - Elements are inserted in the specified collection into this list, starting at the specified position.
  4. void clear() - This method removes elements from this list.
  5. boolean contains(Object o) - This method returns true if this list contains the specified element.
  6. E get(int index) - Element is returned at the specified position in this list.
  7. int indexOf(Object o) - This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contains the element.
  8. boolean isEmpty() - This method returns true if this list contains no elements.
  9. int lastIndexOf(Object o) - Index of the last occurrence of the specified element is returned in this list, or -1 if this list does not contain the element.
  10. E remove(int index) - Element is removed at the specified position in this list using this method.
  11. boolean remove(Object o) - First occurrence of the specified element is removed from this list, if it is present.
  12. protected void removeRange(int fromIndex, int toIndex) - All elements of list are removed whose index is between fromIndex(inclusive) and toIndex(exclusive).
  13. E set(int index, E element) - Element is replaced at the specified position in this list with the specified element.
  14. int size() - Returns the number of elements in this list.
Example
Code:
import java.util.ArrayList;
class ArrayListDemo {
	public static void main (String dt[]) {
		//  creates a new ArrayList
		ArrayList al=new ArrayList();
		//   add elements to the list
		al.add(14);
		al.add(27);
		al.add(27.25);
		al.add('M');
		al.add("Sanju");
		//   traverse the elements of the list
		for (int i=0;i<al.size();i++) {
			System.out.println(al.get(i));
		}
	}
}
Output



Java.util.Calendar Class



The java.util.calendar class is an abstract class that provides methods for converting a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.
  • This class also provides additional fields and methods for implementing a concrete calendar system outside the package.
  • Calendar defines the range of values returned by certain calendar fields.
Class declaration
Code:
public abstract class Calendar
   extends Object
      implements Serializable, Cloneable, Comparable<Calendar>
Class constructors
  1. protected Calendar() - Calendar object is constructed with the default time zone and locale.
  2. protected Calendar(TimeZone zone, Locale aLocale) - Calendar is constructed with the specified time zone and locale.
Class methods
  1. abstract void add(int field, int amount) - Specified amount of time is added to the given calendar field, based on the calendar's rules.
  2. int compareTo(Calendar anotherCalendar) - Time values represented by two Calendar objects are compared by this method.
  3. boolean equals(Object obj) - This Calendar is compared to the specified Object.
  4. int getFirstDayOfWeek() - This method is used to get the first day of the week.
  5. static Calendar getInstance() - This method is used to get calendar using the default time zone and locale.
  6. static Calendar getInstance(Locale aLocale) - This method is used to get calendar using the default time zone and specified locale.
  7. static Calendar getInstance(TimeZone zone) - This method is used to get a calendar using the specified time zone and default locale.
  8. static Calendar getInstance(TimeZone zone, Locale aLocale) - This method is used to get calendar with the specified time zone.
  9. Date getTime() - Date object is returned representing this Calendar's time value.
  10. long getTimeInMillis() - Calendar's time value is returned in milliseconds.
  11. TimeZone getTimeZone() - This method is used to get the time zone.
  12. void set(int field, int value) - Given calendar field is set to the given value.
  13. void set(int year, int month, int date) - Calendar fields YEAR, MONTH, and DAY_OF_MONTH are set using this method.
  14. void set(int year, int month, int date, int hourOfDay, int minute) - Calendar fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, and MINUTE are set using this method.
  15. void setTime(Date date) - Calendar's time with the given Date is being set by this method.
  16. void setTimeInMillis(long millis) - Calendar's current time from the given long value is being set by this method.
  17. String toString() - String representation of this calendar is returned.
Example
Code:
import java.util.Calendar;
class CalenderDemo
{
	public static void main(String arr[])
	{
		Calendar cal=Calendar.getInstance();
		int y=cal.get(Calendar.YEAR);
		int x=cal.get(Calendar.MONTH);		
		int z=cal.get(Calendar.DAY_OF_MONTH);
		int r=cal.get(Calendar.DAY_OF_WEEK);
		int m=cal.get(Calendar.DAY_OF_YEAR);
		
		System.out.println("Current Year="+y);
		System.out.println("Current Month="+(x+1));
		System.out.println("Current Day of Month="+z);
		System.out.println("Current Day of Week="+r);
		System.out.println("Current Day of Year="+m);
	}	
}
Output



Java.util.HashMap Class



The java.util.HashMap class is the Hash table based implementation of the Map interface.
  • Hash map is used to store collection in the form of key and value pair.
  • This class permits null values and the null key.
Class declaration
Code:
public class HashMap<K,V>
   extends AbstractMap<K,V>
       implements Map<K,V>, Cloneable, Serializable
Parameters
java.util.HashMap class have following parameters:
  • K -- This is the type of keys maintained by this map.
  • V -- This is the type of mapped values.
Class constructors
  • HashMap() - Empty HashMap is constructed with the default initial capacity (16) and the default load factor (0.75).
  • HashMap(Collection<? extends E> c)
    Empty HashMap is constructed with the specified initial capacity and the default load factor (0.75).
  • HashMap(Map<? extends K,? extends V> m)
    New HashMap is created with the same mappings as the specified Map.
Class methods
  1. void clear() - All of the mappings from this map are removed by this method.
  2. boolean containsKey(Object key) - If this map contains a mapping for the specified key this method returns true.
  3. boolean containsValue(Object value) - If this map maps one or more keys to the specified value this method will return true.
  4. V get(Object key) - Value to which the specified key is mapped is returned, or null if this map contains no mapping for the key.
  5. boolean isEmpty() - This method returns true if this map contains no key-value mapping.
  6. Set<K> keySet() - A Set view of the keys is returned contained in this map.
  7. V put(K key, V value) - Specified value is associated with the specified key in this map.
  8. void putAll(Map<? extends K,? extends V> m) - Mappings from the specified map are copied to this map.
  9. V remove(Object key) - Mapping for the specified key is removed from this map if present.
  10. int size() - Number of key-value is returned mappings in this map.
  11. Collection<V> values() - Collection view of the values is returned contained in this map.
Example
Code:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
class HashMapDemo
{
	public static void main (String dt[])
	{
		HashMap<Integer,String> hm=new HashMap<Integer,String>();
		hm.put(101,"Aman");
		hm.put(103,"Ajay");
		hm.put(102,"Vishal");
		hm.put(103,"Mohit");
		hm.put(104,"Sanju");
		hm.put(105,"Nikhil");
		System.out.println(hm);
		Set s=hm.keySet();
		Iterator itr=s.iterator();
		while(itr.hasNext())
		{
			int k=(Integer)itr.next();
			String v=hm.get(k);
			System.out.println(k+"="+v);
		}
	}
}
Output