Difference Between HashSet LinkedHashSet and TreeSet

HashSet, LinkedHashSet and TreeSet are all implementations of the Set interface.In this post lets see Difference Between HashSet LinkedHashSet and TreeSet. 1. Implementation All these 3 implement the Set interface. HashSet is backed by a hash table (actually a HashMap instance). LinkedHashSet

Read More

TreeSet Example in Java

TreeSet is a NavigableSet implementation based on a TreeMap. Sorting in the TreeSet makes it different from the HashSet. In TreeSet the elements are sorted ascending using their nartural sorting order. You can change the sorting by providing a Comparator while constructing

Read More

LinkedHashSet Example in Java

LinkedHashSet extends the HashSet and is the implementation of Hash Table and Linked List. LinkedHashSet maintains a doubly-linked list through all of its elements. This helps in maintaining the insertion-order of the elements. Since LinkedHashSet extends the HashSet, they have all the

Read More

Sorting TreeMap Using Comparator

TreeMap are sorted naturally using their Keys. If you have your objects as keys then Sorting TreeMap using Comparator is a good way to decide the map order. Lets see an example on how to use Comparator in a TreeMap 1. User

Read More

WeakHashMap example in Java

WeakHashMap is a HashTable based implementation of the Map Interface. The keys of the WeakHashMap have weak reference and so an entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use. Each key object in

Read More

LinkedHashMap example in Java

LinkedHashMap is part of the java.util package and it extends the HashMap, so it has all the properties of HashMap. LinkedHashMap maintains the order of insertion of the elements, hence when we iterate a LinkedHashMap we get the elements in the order

Read More

TreeMap example in Java

TreeMap is a part of the java.util package and implements the Map interface. TreeMap stores the Key-Value pairs in a sorted order of the Keys. TreeMap is a Red-Black tree based NavigableMap implementation. Red-Black Tree – Ref- wiki A red–black tree is

Read More

Sorting of HashSet in Java Examples

HashSet are unsorted collections. Sorting of HashSet can be done in multiple ways. In this post we will see examples on sorting of HashSet using ArrayList and TreeSet. Example: Sorting of HashSet in Java [crayon-5997b208dcad8354661743/] Output [crayon-5997b208dcae2348531491/] Reference HashSet API

Read More

Difference between ArrayList and LinkedList

In this post we will see Difference between ArrayList and LinkedList in java along with examples. Implementation a. ArrayList implements the List Interface , LinkedList implements the List and Deque Interface b. ArrayList uses a dyanmic array to store elements, whereas LinkedList

Read More

LinkedList push and pop methods

Adding and removing element in LinkedList can also be done by push and pop methods. 1. [crayon-5997b208dd2da103038747-i/] – Adds an element into the List as the first element 2. [crayon-5997b208dd2e3098413594-i/] – Removes the first element of the list. Lets see some examples

Read More