Java Collections Framework
Collections Framework in Java
The Java collections framework is a set of classes and interfaces that implement commonly reusable collection data structures.
Although referred to as a framework, it works in a manner of a library. The collections framework provides both interfaces that define various collections and classes that implement them.
A collection sometimes called a container is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data.
A collection is the same as the intuitive, mathematical concept of a set. A set is just a group of unique items, meaning that the group contains no duplicates. Java Collections Framework provides a set of interfaces and classes for storing and manipulating groups of data as a single unit, a collection.
The framework provides a convenient API to many of the abstract data types maps, sets, lists, trees, arrays, hashtables, and other collections. Because of their object-oriented design, the Java classes in the Collections Framework encapsulate both the data structures and the algorithms associated with these abstractions. With the Java Collections Framework the programmer easily define higher level data abstractions, such as stacks, queues, and thread safe collections.
You should be able to understand the Collections Framework more easily. The Collections Framework is made up of a set of interfaces for working with groups of objects. The different interfaces describe the different types of groups. For the most part, once you understand the interfaces, you understand the framework. While you always need to create specific implementations of the interfaces, access to the actual collection should be restricted to the use of the interface methods, thus allowing you to change the underlying data structure, without altering the rest of your code. The following diagrams shows the framework interface hierarchy.
Collections in Java
The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects.
Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.
Java Collection means a single unit of objects. Java Collection framework provides many interfaces Set, List, Queue, Deque and classes ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet.
What is Collection in Java ?
A Collection represents a single unit of objects, i.e., a group.
What is a framework in Java ?
Framework represents a set of classes and interfaces. It provides ready made architecture.
What Is a Collections Framework ?
A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following:
- Interfaces : These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy.
- Implementations : These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.
- Algorithms : These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface. In essence, algorithms are reusable functionality.
How Many Types of Collections in Java ?
There are three generic types of collections in Java.
- Ordered Lists
- Dictionaries or Maps
What is the use of Java collections ?
The Java collections framework gives the programmer access to prepackaged data structures as well as to algorithms for manipulating them. A collection is an object that can hold references to other objects. The collection interfaces declare the operations that can be performed on each type of collection.
What is the difference between Java collection and Java collections ?
Collections is an utility class present in java.util package to define several utility method like sorting, searching for collection object. Collections is a class which has some static methods and that method returns the collection. Collection is an interface, rather than root interface in collection hierarchy.
What are collection APIs give me an example ?
The Collection API is a set of classes and interfaces that support operation on collections of objects.
- Example of Classes : HashSet, HashMap, ArrayList, LinkedList, TreeSet and TreeMap.
- Example of Interfaces : Collection, Set, List and Map.
What is the hierarchy of collections framework in Java ?
The hierarchy of the entire collection framework consists of four core interface such as Collection, List, Set, Map, and two specialized interfaces named SortedSet and SortedMap for sorting. All the interfaces and classes for the collection framework are located in java.util package.
Why do we need Java collections ?
Collections are used almost in every programming language and when Java arrived, it also came with Collection classes. Collections are used in situations where data is dynamic. Collections allow adding an element, deleting an element and host of other operations. There are a number of Collections in Java allowing to choose the right Collection for the right context. You can play with data structure and algorithms.
Benefits of Java Collections Framework
The Java Collections Framework provides the following benefits :
- Reduces programming effort : By providing useful data structures and algorithms, the Collections Framework frees you to concentrate on the important parts of your program rather than on the low-level "plumbing" required to make it work. By facilitating interoperability among unrelated APIs, the Java Collections Framework frees you from writing adapter objects or conversion code to connect APIs.
- Increases program speed and quality : The Collections Framework provides high-performance, high-quality implementations of useful data structures and algorithms. The various implementations of each interface are interchangeable, so programs can be easily tuned by switching collection implementations. Because you're freed from the drudgery of writing your own data structures, you'll have more time to devote to improving programs' quality and performance.
- Allows interoperability among unrelated APIs : The collection interfaces are the vernacular by which APIs pass collections back and forth. If my network administration API furnishes a collection of node names and if your GUI toolkit expects a collection of column headings, APIs will interoperate seamlessly, even though they were written independently.
- Reduces effort to learn and to use new APIs : Many APIs naturally take collections on input and furnish them as output. In the past, each such API had a small sub-API devoted to manipulating its collections. There was little consistency among these ad hoc collections sub-APIs, so you had to learn each one from scratch, and it was easy to make mistakes when using them. With the advent of standard collection interfaces, the problem went away.
- Reduces effort to design new APIs : This is the flip side of the previous advantage. Designers and implementers don't have to reinvent the wheel each time they create an API that relies on collections; instead, they can use standard collection interfaces.
- Fosters software reuse : New data structures that conform to the standard collection interfaces are by nature reusable. The same goes for new algorithms that operate on objects that implement these interfaces.
Which collections are thread safe in Java ?
ArrayList, LinkedList, HashSet, LinkedHashset and TreeSet in Collection Interface and HashMap, LinkedHashMap and Treemap are all non-synchronized. All collection classes (except Vector and Hashtable) in the java.util package are not thread-safe. The only two legacy collections are thread-safe: Vector and Hashtable.
What is a set in Java collections ?
A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited. Set also adds a stronger contract on the behavior of the equals and hashCode operations, allowing Set instances to be compared meaningfully even if their implementation types differ. Two Set instances are equal if they contain the same elements.
What is the advantage of generic collection ?
Code that uses generics has many benefits over non-generic code: Stronger type checks at compile time. A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find.
What is collection class in Java ?
Java collection class is used exclusively with static methods that operate on or return collections. It inherits Object class. The important points about Java Collections class are: Java Collection class supports the polymorphic algorithms that operate on collections.
What are the Java Collections Framework Classes and Interfaces ?
|Java Collections Framework Classes|
|Sl.No.||Collections Framework Class||Description|
|1||AbstractCollection||The AbstractCollection class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface.|
|2||AbstractList||The AbstractList class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "random access" data store (such as an array).|
|3||AbstractMap||The AbstractMap class provides a skeletal implementation of the Map interface, to minimize the effort required to implement this interface.|
|4||AbstractQueue||The AbstractQueue class provides skeletal implementations of some Queue operations.|
|5||AbstractSequentialList||The AbstractSequentialList class provides a skeletal implementation of the List interface to minimize the effort required to implement this interface backed by a "sequential access" data store (such as a linked list).|
|6||AbstractSet||The AbstractSet class provides a skeletal implementation of the Set interface to minimize the effort required to implement this interface..|
|7||ArrayDeque||The ArrayDeque class Resizable-array implementation of the Deque interface.|
|8||ArrayList||The ArrayList class Resizable-array implementation of the List interface.|
|9||Arrays||The Arrays class contains various methods for manipulating arrays (such as sorting and searching).|
|10||Collections||The Collections class consists exclusively of static methods that operate on or return collections.|
|11||EnumMap||The EnumMap class A specialized Map implementation for use with enum type keys.|
|12||EnumSet||The EnumSet class A specialized Set implementation for use with enum types.|
|13||HashMap||The HashMap class Hash table based implementation of the Map interface.|
|14||HashSet||The HashSet class implements the Set interface, backed by a hash table (actually a HashMap instance).|
|15||HashTable||The HashTable class implements a hash table, which maps keys to values.|
|16||IdentityHashMap||The IdentityHashMap class implements the Map interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values).|
|17||LinkedHashMap||The LinkedHashMap class Hash table and linked list implementation of the Map interface, with predictable iteration order.|
|18||LinkedHashSet||The LinkedHashSet class Hash table and linked list implementation of the Set interface, with predictable iteration order.|
|19||LinkedList||The LinkedList class Doubly-linked list implementation of the List and Deque interfaces.|
|20||PriorityQueue||The PriorityQueue class An unbounded priority queue based on a priority heap.|
|21||TreeMap||The TreeMap class A Red-Black tree based NavigableMap implementation.|
|22||TreeSet||The TreeSet class A NavigableSet implementation based on a TreeMap.|
|23||Vector||The Vector class implements a growable array of objects.|
|24||WeakHashMap||The WeakHashMap class Hash table based implementation of the Map interface, with weak keys.|
Java Collections Framework Interfaces