Collections in Java – Types of Interface In Java
1. Objective
In our last tutorial, we discussed Command Line Arguments in Java. Now, in this Collections in Java tutorial, we are going to study: types of interface in Java, subtypes of collections in Java, and collection framework in Java. Moreover, we will discuss, set, Java list and map interface in Java. We will also cover subtypes of Java collections: stack, queue, and deque.
So, let’s start with Collections in Java.
2. What are Collections in Java?
- Java collections are set of Java classes that assist the objects to group them and manage them.
- The least complex technique to use the arrays but, as we know for some situations it is too complex.
- For instance, array can’t change the size and also the elements can’t be effectively embedded.
- Arrays file by integers number esteems and don’t permit indexing components by key (ex: string).
- To overcome these constraints specific classes utilize.
Do you know What is Java Garbage Collection Algorithm?
The three main interfaces in Java are –
- Set
- List
- Map
The subtypes of collections in Java are –
- Stack
- Queue
- Deque
3. Types of Interface in Java
a. Set Interface in Java
Set accumulation that doesn’t permit copies and furthermore doesn’t permit getting to components by index. Rather, it gives techniques that check if component or components exist.
- EnumSet – Enumset particular class to work with enum types.
- HashSet – HashSet keeps an unordered list of components (arrange is eccentric).
- LinkedHashSet – LinkedHashSet keeps requested rundown of components.
- TreeSet – Treeset makes sure that there are no duplicates.
- SortedSet – Sortedset provides ordering on its elements.
Let’s Discuss Array vs ArrayList in Java
b. Java List Interface
Java list is accumulation that permits copies and carries on, like arrays (file components by the whole number) yet is more adaptable. To start with component has list = 0, last one has record = length-1.
- ArrayList – Keeps an unordered list of components utilizing exhibit.
- LinkedList – Keeps requested list of components utilizing doubly-connected rundown.
- Vector – For the most part, the same as ArrayList, however, it is string safe.
c. Map Interface in Java
Java Map Interface is accumulation that permits copies and is like rundown with the exception of that record components by (key can be any protest) Map can be expected as an affiliated exhibit.
- HashMap – Keeps unordered rundown of list utilizing exhibit
- LinkedHashMap – Keeps requested list of components utilizing doubly-connected rundown.
- TreeMap – Keeps requested list of components utilizing RBT. Components are requested by regular request or by a custom comparator.
- Hashtable – Keeps an unordered list of components as HashMap, however, it is synchronized. This class is outdated.
- EnumMap – Keeps ordered collection and are maintained in natural order.
- Properties – It is subclass of HashTable. Provides methods to read and store data in properties file.
Follow this link to know What is Java HashMap?
4. Subtypes of Collections in Java
- Java Stack – Elements include (push) and remove (i.e. pop) from best of accumulation. This rule is called LIFO (Last In, First Out)
- Java Line – Elements include (push) and removed (pop) all together they include. This guideline is called FIFO (First In, First Out)
- Deque – Elements include and expel from the two sides. Name deque (articulated “Deck”) is an easy route for “double finished line”.
5. Collection Framework in Java
Collections in Java is a readymade architecture with an option to represent classes and objects.
On the other hand, Collection framework in Java represents a better and unified method to store objects and classes.
It has an algorithm, interfaces and implementations.
a. Methods
public boolean add(Object element) | To insert an element |
public boolean addAll(Collection c) | Insert a specified collection |
public boolean remove(Object element) | To delete |
public boolean removeAll(Collection c) | To delete all elements of invoking collection |
public boolean retainAll(Collection c) | Delete all elements of the specified collection |
public int size() | return total number of elements |
public void clear() | delete total number of elements |
public boolean contains(Object element) | Search and element |
public boolean containsAll(Collection c) | Search a specific collection |
public Iterator iterator() | To return an iterator |
public Object[] toArray() | To convert a collection to an array |
public boolean isEmpty() | Checks for an empty array |
public boolean equals(Object element) | Checks for a collection empty |
public int hashCode() | To checks for a match in a collection |
Let’s Explore JDBC Tutorial | Performing Database Operations in Java
b. Iterator Interface in Java
- The public boolean hasNext() – True if it has more value.
- public Object next() – Returns an element and shifts the cursor to the next element.
- public void remove() – Removes the last element that iterator returned.
This was all about Collections in Java Tutorial. Hope you like our explanation.
6. Conclusion
In this Java tutorial, we have learned about what is collections in Java. In addition, we studied Interface in java: Set, Java List and Map Interface in Java. We also covered Subtypes of Java Collections: Java Stack, Java Queue, and Deque Java. At last, we see Collection framework in Java: Methods and Iterator Interface in Java. Furthermore, if you have any query, feel free to ask in the comment section.
Related Topic- Java Extends vs Implements With Example Program