Collection Framework in Java

Collection Framework in Java,The Java collections framework (JCF) is a set of classes and interfaces that implement commonly reusable collection data structures.

Java Collections API Methods

A small confusion comes to a novice at this juncture between collection and collections. Both are very different. Collection is an interface and Collections is a class, both from java.util package. Collection is the root interface from which almost all data structures are derived, commonly known as collection framework. Collections class contains many static methods …

Java Collections API Methods Read More »

interface Comparator

Comparator interface is from java.util package like other data structures. Some data structures TreeSet and TreeMap sort the elements in natural (ascending) order by default. The Comparator object is capable to change this order, the way customization requires. Programmer can get a precise control over ordering. In Java, we come across a list of objects …

interface Comparator Read More »

Comparable Example Java

Before going through this program, it is advised to go through the interface Comparable tutorial. In the following Employee program, compareTo() method of Comparable interface is used to sort the employees by their ages. To use the compareTo() method, Employee class implements Comparable interface. Example code on Comparable Example import java.util.*; class Employee implements Comparable …

Comparable Example Java Read More »

Comparable vs Comparator

At the outset, both are used for ordering the elements of a data structure in the way the programmer desires. Just they differ in their methods of implementation and usage with objects in comparison. These two interfaces are best suitable for ordering the objects depending on their fields. For example, student objects can be ordered …

Comparable vs Comparator Read More »

TreeMap add Sort Compare Java

Two programs are given on TreeMap with the following operations. TreeMap General: The general operations like object creation, addition and deletion of key/value pairs, knowing the existence of a key or value or elements and size etc. are performed. TreeMap Special: The special operations like sorting the elements on key basis and value basis, adding …

TreeMap add Sort Compare Java Read More »

TreeMap Example Java

Two programs are given on TreeMap with the following operations. TreeMap General: The general operations like object creation, addition and deletion of key/value pairs, knowing the existence of a key or value or elements and size etc. are performed. TreeMap Special: The special operations like sorting the elements on key basis and value basis, adding …

TreeMap Example Java Read More »

TreeMap Tutorial Java

TreeMap is derived from SortedMap interface and attains the property of printing the elements in ascending order. With Comparator, the order can be changed. The Comparator should be supplied at the time of TreeMap object creation itself. Methods of TreeMap are not synchronized and thereby not thread-safe. Using in multithreaded environment is not advisable. A …

TreeMap Tutorial Java Read More »

interface SortedMap Tutorial

As you can observe in the collections framework hierarchy, the Map interface comes with another sub interface SortedMap. The SortedMap can make use of all the methods of Map interface and also it adds its own (the list is given hereunder). The extra advantage (the original one is unique keys, a feature derived from its …

interface SortedMap Tutorial Read More »

HashMap Sort Get Java

Three programs are given on HashMap with different functionalities used very often in coding. HashMap General: In this program, API and the general operations like adding, retrieving and deleting elements, size and checking the existence of elements are done. HashMap Iteration: The iteration styles of elements, adding elements of one HashMap to another and cloning …

HashMap Sort Get Java Read More »

HashMap Iteration Add Get Clone Java

Three programs are given on HashMap with different functionalities used very often in coding. HashMap General: In this program, API and the general operations like adding, retrieving and deleting elements, size and checking the existence of elements are done. HashMap Iteration: The iteration styles of elements, adding elements of one HashMap to another and cloning …

HashMap Iteration Add Get Clone Java Read More »

HashMap Example Java

Three programs are given on HashMap with different functionalities used very often in coding. HashMap General: In this program, API and the general operations like adding, retrieving and deleting elements, size and checking the existence of elements are done. HashMap Iteration: The iteration styles of elements, adding elements of one HashMap to another and cloning …

HashMap Example Java Read More »

Map interface Methods Java

After knowing what Map is and its position in the collections hierarchy diagram, let us explore the methods. These methods can be used straight way by its derived data structures like TreeMap and HashMap. Following are the important Map interface Methods boolean containsKey(Object key1): Used to check the availability of a specific key, key1, in …

Map interface Methods Java Read More »

TreeSet Java Add Print Clone Elements

Two Programs exists on TreeSet Java. TreeSet General: General operations like printing the elements in natural order (the default behavior of SortedSet, the super class of TreeSet), extracting a part of a set, creating generics TreeSet Java etc. are performed. TreeSet Special: Operations like cloning the TreeSet, iterating elements with Iterator and the methods like …

TreeSet Java Add Print Clone Elements Read More »

TreeSet Example Add, Print, Extract Elements

Two Programs exists on TreeSet. TreeSet General: General operations like printing the elements in natural order (the default behavior of SortedSet, the super class of TreeSet), extracting a part of a set, creating generics TreeSet etc. are performed. TreeSet Special: Operations like cloning the TreeSet, iterating elements with Iterator and the methods like retainAll(), addAll(), …

TreeSet Example Add, Print, Extract Elements Read More »

TreeSet Tutorial Constructors Methods

TreeSet is derived from SortedSet interface. We know earlier, SortedSet is derived from Set and Set is derived from Collection. A complete hierarchy is available at "Java Collections Interfaces Hierarchy". TreeSet is very powerful as it can make use of all the methods of Collection (like iterator() and add()); stores only unique elements, a feature …

TreeSet Tutorial Constructors Methods Read More »

SortedSet interface Java

SortedSet interface is derived from Set interface. Set interface is very unique in the whole collections hierarchy. All the derived classes of Set interface like HashSet and TreeSet does not allow duplicate elements, a unique characteristic of all collections classes. The SortedSet interface adds one more feature of returning the elements in ascending order (known …

SortedSet interface Java Read More »

HashSet Example addAll containAll retainAll Iterator

Two programs are given on HashSet with different functionality. HashSet General: In this program, the general methods like contains, equals, add, size, remove, toArray and hashCode usage is illustrated. Elements are printed with foreach loop. HashSet Operations: In this program, Iterator for printing the elements of HashSet is used. The methods like addAll(), clear(), retainAll(), …

HashSet Example addAll containAll retainAll Iterator Read More »