Ordered collections in java

Collections in Java

The 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 a few of them: 1. Laptop, Mouse, HeadPhones, earPhones, Mouse (computer’s & animal too), Book, Notepad, pen, the 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 things right in the system.

Few important Interfaces are :

Few important classes are :

  • ArrayList
  • Vector
  • LinkedList
  • PriorityQueue
  • HashSet
  • LinkedHashSet
  • TreeSet

java-collection-architecture

Outline of the Collections Framework

  • Collection — A group of objects. No assumptions are made about the order of the collection (if any) or whether it can contain duplicate elements.
  • Set — No duplicate elements permitted, May or may not be ordered. Extends the Collection interface.
  • List — Ordered collection, also known as a sequence. Duplicates are generally permitted. Allows index-based access. Extends the Collection interface.
  • Queue — A Queue is a First In First Out (FIFO) data structure. It models a queue in real life. It follows the real-life Queue that we might have seen in front of a movie theater, a shopping mall, a metro, or a bus.
  • Deque — A double-ended queue, supporting element insertion and removal at both ends. Extends the Queue interface.
  • Map — Map is not a collection but a form based on the Set and List interfaces. Here all the values will be stored with a respective key. The keys cannot be duplicated but values can have duplicates. Pairing the key and values are also called Mapping
  • SortedSet — A set whose elements are automatically sorted, either in their natural ordering (see the Comparable interface) or by a Comparator object provided when a SortedSetinstance is created. Extends the Set interface.
  • SortedMap — A map whose mappings are automatically sorted by key, either using the natural ordering of the keys or by a comparator provided when a SortedMap instance is created. Extends the Map interface.N
  • BlockingQueue — A Queue with operations that wait for the queue to become nonempty when retrieving an element and that wait for space to become available in the queue when storing an element. (This interface is part of the java.util.concurrent package.
  • BlockingDeque — A Deque with operations that wait for the deque to become non-empty when retrieving an element and wait for space to become available in the deque when storing an element.
    Extends both the Deque and BlockingQueue interfaces. (This interface is part of the java.util.concurrent package.)
  • HashSet — Hash table implementation of the Set interface. The best all-around implementation of the Set interface.
  • TreeSet — Red-black tree implementation of the NavigableSet interface.
  • LinkedHashSet — Hash table and linked list implementation of the Set interface. An insertion-ordered Set implementation that runs nearly as fast as HashSet.
  • ArrayList — Resizable-array implementation of the List interface (an unsynchronized Vector). The best all-around implementation of the List interface.
  • ArrayDeque — Efficient, resizable-array implementation of the Deque interface.
  • LinkedList — Doubly-linked list implementation of the List interface. Provides better performance than the ArrayList implementation if elements are frequently inserted or deleted within the list. Also implements the Deque interface. When accessed through the Queue interface, LinkedList acts as a FIFO queue.
  • PriorityQueue — Heap implementation of an unbounded priority queue.
  • HashMap — Hash table implementation of the Map interface (an unsynchronized Hashtable that supports null keys and values). The best all-around implementation of the Map interface.
  • TreeMap — Red-black tree implementation of the NavigableMap interface.
  • LinkedHashMap — Hash table and linked list implementation of the Map interface. An insertion-ordered Map implementation that runs nearly as fast as HashMap.
Читайте также:  Fix border size css
Iterators :

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.

Frequently Asked Questions

are java collections thread-safe?

All collection classes except Vector and Hashtable in java.util package is not thread-safe. The only two legacy collections are thread-safe:

WHY? Here’s the reason : Synchronization can be very expensive! You know, Vector and Hashtable are the two collections that 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 that always take time to monitor, and that reduces performance.

That’s why the new collections provide no concurrency control at all to provide maximum performance in single-threaded applications.

The collection which is non-Thread Safe : ArrayList, LinkedList, HashSet, LinkedHashset and TreeSet in Collection Interface and HashMap, LinkedHashMap, and Treemap

are java collections ordered?

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.

are java arrays are collections?

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 structures (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.

Difference between Arrays and Collections?

There are 5 (could be more) differences between Array and Collection as given below :

  • Arrays are fixed in size, whereas some Collections are grow-able in nature.
  • Arrays store homogeneous data. Collections store both homogeneous as well as heterogeneous data.
  • In Arrays, there are no underlining data structures, whereas Collections have underlining data structures.
  • Arrays are recommended for performance, whereas Collections are not.
  • Arrays use more memory space as compared to Collections.
java collections are immutable?

Immutable : Elements cannot be added or removed. Calling any mutator method will always cause UnsupportedOperationException to be thrown

The important characteristic of the collection is to grow dynamically as the values are coming in. So We can say Collections are not Immutable

can java collection contain null?

Collections can contain null values but not all collections will allow null. For example, Set and Vector will allow null value

how to import java collections?

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
what java collection to use?

decide-which-collection-use

Below flow chart will guide you or help you to decide which collection to use.

which java collections are synchronized?
which java collection is fastest?

Every collection type is suitable for a particular scenario. There is no fastest or best collection.

  • If you need fast access to elements using the index, ArrayList is your answer.
  • If you need fast access to elements using a key, use HashMap.
  • If you need fast add and removal of elements, use LinkedList (but it has a very poor index access performance).
which java collection allows duplicate keys?

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 java forbids it. Java hates to have such a situation.

Unambiguous situation — You cannot decide which to choose when both options are correct.

which java collection is sorted?

Tree set and TreeMap are sorted collections in java.

Источник

Оцените статью