Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Monday, 26 June 2017

Collections Class In Java

Java Collections Class

Collections class in java

Collections is a class in java which is available in java.util package. Collections class inherit(extends) only Object class in java.

In java Collections class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by specified collection and a few others odds and ends.

The methods of collections's class all throw a NullPointerException if the collections or class objects provided to them are null.

Collections class is a member of Java Collection Framework.


Declaration of Collections Class

public class Collections extends java.lang.Object


Fields

Following are fields for java.util.Collections class :

static List EMPTY_LIST : The empty list(immutable)

static Map EMPTY_MAP : The empty map(immutable)

static Set EMPTY_SET : The empty set(immutable)


Methods of Collections Class in Java

Take a look of some methods of Collections class.

1) static <T> boolean addAll(Collections<? super T> c, T... elements)

This method adds all of the specified elements to the specified collection.

2) static <T> Queue<T> asLifoQueue(Deque<T> deque)

This method returns a view of a Deque as a Last-in-first-out(Lifo) queue.

3) static <T> int binarySearch(List<? extends Comparable<?  super T>> list, T key)

This method searches the specified list for the specified object using the binary search algorithm.

4) static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c)

This method searches the specified list for the specified object using the binary search algorithm.

5) static <E> Collection<E> checkedCollection(Collection<E> c, Class<E> type)

This method returns a dynamically typesafe view of the specified collection.

6) static <E> List<E> checkedList(List<E> list, Class<E> type)

This method returns a dynamically typesafe view of the specified list.



7) static <K, V> Map<K, V> checkedMap(Map<K, V> m, Class<K> keyType, Class<V> valueType)

This method returns a dynamically typesafe view of the specified map.

8) static <K, V> NavigableMap<K, V> checkedNavigableMap(NavigableMap<K, V> m, Class<K> keyType, Class<V> valueType)

This method returns a dynamically typesafe view of the specified navigable map.

9) static <E> Set<E> checkedSet(Set<E> s, Class<E> type)

This method returns a dynamically typesafe view of the specified set.

10) static <T> void copy(List<? super T> dest, List<? extends T> src)

This method copies all of the elements from one list into another.

11) static <T> Enumeration<T> emptyEnumeration()

This method returns an enumeration that has no elements.

12) static <T> Iterator<T> emptyIterator()

This method returns an iterator that has no elements.


Java Collections Class Example

This is simple example of collections class.

import java.util.*;
public class CollectionsExample
{
public static void main(String args[])
{
List<String> list = new ArrayList<String>();
list.add("hindi");
list.add("english");
list.add("math");
System.out.println("before adding collection values "+list);

//using collections's method
Collections.addAll(list, "science", "drawing");
System.out.println("after adding elements collection values"+list);
}


output : before adding collection values [hindi, english, math]
        after adding elements collection values [hindi, english, math, science, drawing]


Java Collections Class Example 1

In this example, we will use max() and min() methods of collections class.

import java.util.*;
public class CollectionsExample2
{
public static void main(String args[])
{
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(96);
al.add(150);
al.add(1);
al.add(2);
al.add(33);
al.add(50);
System.out.println("maximum value"+Collections.max(al));
System.out.println("minimum value"+Collections.min(al));
}
}

output : maximum value 150
              minimum value 1

Share:

1 comment:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate