Collections framework is nothing but a backpack, guess what are the things you can store in a backpack. Probably you must have guessed everything that we can store in a backpack, but let list few of them: 1. Laptop, Mouse, HeadPhones, earPhones, Mouse (computer's & animal too), Book, Notepad, pen, pencil you can write so on..
The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects in Java.
With Java Collections, we can achieve all the operations that you perform on data such as searching, sorting, insertion, manipulation, and deletion.
Java Collection framework provides different interfaces and classes to get the things right in the system.
Few important Interfaces are :
Few important classes are :
Iterators are used to navigate through the elements present in the collection, the collection could be any type.
Iterator - In addition to the functionality of the Enumeration interface, enables the user to remove elements from the backing collection with well-defined, useful semantics.
ListIterator - Iterator for use with lists. In addition to the functionality of the Iterator interface, supports bidirectional iteration, element replacement, element insertion, and index retrieval.
All collection classes except Vector and Hashtable in the
WHY? Here’s the reason : Synchronization can be very expensive! You know, Vector and Hashtable are the two collections exist early in Java history, and they are designed for thread-safe from the start if you have a chance to look at their source code, you will see their methods are all synchronized!.
However, they quickly expose poor performance in multi-threaded programs. As you may know, synchronization requires locks which always take time to monitor, and that reduces the performance.
That’s why the new collections provide no concurrency control at all to provide maximum performance in single-threaded applications.
The collection which are non-Thread Safe : ArrayList, LinkedList, HashSet, LinkedHashset and TreeSet in Collection Interface and HashMap, LinkedHashMap and Treemap
An ordered collection maintains the order of the elements based on the sequence you put stuff into/remove them from the collection.
List is an ordered collection: each element has an index, which forms an ordering of the elements, but not usually related to any property of the elements themselves.
No, Arrays are not collections.
Arrays are simple constructs with linear storage of fixed size and therefore they can only store a given number of elements. Arrays are built into the core of Java language and the array-related Java syntax is very easy and straightforward, for example, the nth element of the array can be obtained as array[n-1].
Collections are more sophisticated and flexible. First of all, they are resizable: you can add any number of elements to a collection. A collection will automatically handle the deletion of an element from any position.
There are several types of collections with different internal storage structure (linear, list, hash set, tree, etc.) and you can choose a collection type best matching your problem so that your most frequent operations will be convenient and efficient.
There are 5 (could be more) differences between Array and Collection as given below :
The important characteristic of the collection is to grow dynamically as the values are coming in. So We can say Collections are not Immutable
Collections can contain null values but not all collections will allow null. For example, Set and Vector will allow null value
You can import the collection a specific one or all of the collections, Collections omes as part of java.util package.
import java.util.ArrayList; // for specificimport java.util.*; // for all
Below flow cart will guide you or help you to decide which collection to use.
Every collection type is suitable for a particular scenario. There is no fastest or best collection.
You cannot keep duplicate keys in Java Collection because only Map and related classes will allow you to store the Key and pairs.
For a Moment let's consider Map allows you to store duplicates, Now you have two keys as "Jedi"
For the first Jedi, you have assigned values as "Anakin Skywalker". For the second Jedi, you have assigned values as Yoda.
Now you are wishing to retrieve "Yoda" but are you will receive the same because both are the same keys. NO right, because java might think like you trying to read "Anakin Skywalker".
This situation is Unambiguous called an unambiguous situation, because which java forbids it. Java hates to have such a situation.
Unambiguous situation - You cannot decide which to choose when both options are correct.
Tree set and TreeMap are sorted collections in java.