In the past Java HashSet example, we learnt about the Java HashSet and its Set implementation and how it keeps the uniqueness. Now, in this example, we will learn about the Buckets and how it aids quicker retrieval and search. These buckets improve the performance of search action and this example will research how these bucketing concepts work with HashSet Class and hashCode override of its stored object.
Before we learn about Java hashSet class, we will learn about the Set interface. Because the HashSet class implements Java Collection’s Set interface. The Set Interface ensures the implementing class to hold uniqueness in the collection. Means, it does not allow duplicate items. In Java Collection framework, the below classes implement the Set interface:
There were other classes as well. But here we listed frequently used ones. In this example, we will explore about the HashSet collection class.
Like Iterator, the Java ListIterator also a special reference over the collection of objects. In the past example on Java Iterator, we saw that it can only remove the collection items. But, the ListIterator can remove as well as add items to the collection while iteration is in progress. Unlike the Iterator, the ListIterator can iterate the collection in both forward and backward directions. So, the ListIterator is called a Bi-Directional Cursor.
In this example, we will use the ArrayList of Fruits and learn how ListIterator works. Let us start.
The Java Iterator is a reference over a collection object. It can be ArrayList, LinkedList anything which implements the basic Collection Interface. With the iterator we can get all the items in the collection one by one. We can iterate over the collection via a loop or via a for-each construct. But, using these, we cannot adjust the structure while the iteration is in-progress. Whereas with Java Iterator, we can remove items while the iteration is going on.
Java Iterator is a Forward-Only Cursor. This means one can iterate the collection items only in forward direction. Standard Java Iterator does not allow the reverse way of iteration. Also, note the Standard Java Iterator does not allow adding the items even though it allows removal. In this example, we will learn about the Iterator using an ArrayList collection object.
The Java Stack operates on ‘Last In First Out (LIFO)’ principle. In the past example, we used the LinkedList as Queue which operated on ‘First In First Out’. Let us say, the Hotel server is keeping the coffee cups on top of each one. When he serves the coffee to the tables, he will take out the cup from the top. This means, the person will take out cup which he placed last. This shows the LIFO principle. The same way Stack works in Java.
Java Collection does not provide Stack Interface. We can use the Deque interface which adds element in the front (or we can say Top) and removes element from the front using special methods of stack. In this example, we are going to use our Linked List of Products as stack.