Java Data Structures — The Building Blocks
4 min readJun 5, 2020
Here are the most commonly used data structures in Java, each grouped under its corresponding interface.
List Interface
ArrayList
- Definition: It uses a dynamic array to store elements, allows duplicates and also stores different data types. It can increase in size as required.
Example:
import java.util.*;class ArrayListExample {public static void main(String[] args) {ArrayList<String> alist=new ArrayList<String>();alist.add("Steve");
alist.add("Tim");//displaying elements
System.out.println(alist);//Adding "Steve" at the fourth position
alist.add(2, "Tom");//displaying elements
System.out.println(alist);// loop and display
for(String str:alist) {
System.out.println(str);
}/*
Outputs:
[Steve, Tim]
[Steve, Tim, Tom]
Steve
Tim
Tom
*/
}
}
LinkedList
- Definition: It is much like an array structure which stores elements in linked memory locations, allows duplicates and also stores different data types
- Has two subtypes: Singly LinkedList and Doubly LinkedList
- Single LinkedList are lists which are linked only in one direction, whereas Double LinkedList are lists which are linked in bi-directionally.
Example:
import java.util.*;public class LinkedListExample {public static void main(String[] args) {LinkedList<String> list = new LinkedList<String>();//Adding elements to the Linked list
list.add("Steve");
list.add("Carl");
list.add("Raj");
list.add("Negan");
list.add("Rick");//Removing First element
//Same as list.remove(0);
list.removeFirst();//Removing Last element
list.removeLast();//Iterating LinkedList
Iterator<String> iterator=list.iterator();while(iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
/* Output:
Carl Raj Negan
*/}
}
Stack
- Definition: It is a data structure as the name sounds, where elements are stored in a stack structure and follow the LIFO(Last In First Out) method for storing/retrieving.
Example:
import java.util.*;public class StackExample {public static void main(String[] args) {Stack<Integer> stk = new Stack<Integer>();//Different ways of adding elements to a stack
stk.add(45);
stk.push(89);
stk.push(98);// Printing the stack
System.out.println(stk);
// Output: [45, 89, 98]//Removes the last element inserted
stk.pop();
System.out.println(stk);
// Output: [45, 89]}
}
Set Interface
HashSet
- Definition: It uses a hash table which uses hashing mechanism for storage of elements, and it doesn’t store duplicate values.
Example:
import java.util.*;public class HashSetExample{public static void main(String args[]){//Creating HashSet and adding elements
HashSet<String> set=new HashSet<String>();set.add("Tom");
set.add("Dick");
set.add("Harry");
set.add("Tom");
set.add(""); // Trying to add a null value//Traversing elements
Iterator<String> itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}/* Output:
Tom
Dick
Harry
*/}
}
LinkedHashSet
- Definition: It is a HashTable and LinkedList implementation of a Set interface, it doesn’t store duplicate values.
Example:
import java.util.*;public class LinkedHashSetExample {public static void main(String[] args) {LinkedHashSet<String> set=new LinkedHashSet<String>();set.add("Hey");
set.add("How");
set.add("Are");
set.add("You");
set.add("Hey");//Traversing elements
Iterator<String> itr=set.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
/* Output:
Hey
How
Are
You
*/}
}
SortedSet Interface
TreeSet
- Definition: It uses a tree for storing elements, and it doesn’t allow duplicate values or null values. It always stores elements in ascending order.
Example:
import java.util.*;public class SortedSetExample {public static void main(String[] args) {// Create the sorted set
SortedSet set = new TreeSet();// Add elements to the set in non-alphabetical order
set.add("Banana");
set.add("Cat");
set.add("Apple");// Iterating over the elements in the set
Iterator it = set.iterator();while (it.hasNext()) {
// Get element
Object element = it.next();
System.out.println(element.toString());
}
// Output in alphabetical order
/* Output:
Apple
Banana
Cat
*/}
}
Map Interface
HashMap
- Definition: It stores elements in a key-value pair format where keys should be unique but values don’t have to be unique.
Example:
import java.util.*;public class HashMapExample {public static void main(String[] args) {//Creating HashMap
HashMap<Integer,String> map=new HashMap<Integer,String>();//Put elements in Map
map.put(1,"Make");
map.put(2,"America");
map.put(3,"Great");
map.put(4,"Again");//Iterating through the map using Map.Entry
for(Map.Entry m : map.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
/* Output:
1 Make
2 America
3 Great
4 Again
*/}
}
Map.Entry Interface
- Definition: The Map.Entry interface enables to work with a Map entry object and has the entrySet() method defined.
Example:
import java.util.*;public class HashTableExample {public static void main(String[] args) {//Creating HashTable
Hashtable<Integer,String> hm=new Hashtable<Integer,String>();//Put elements in HashTable
hm.put(100,"Tracy");
hm.put(102,"Abe");
hm.put(101,"Alex");
hm.put(103,"Ian");
hm.put(104, "Val");//Iterating through the map using Map.Entry
for(Map.Entry m:hm.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
/* Output:
104 Val
103 Ian
102 Abe
101 Alex
100 Tracy
*/}
}
SortedMap Interface
TreeMap
- Definition: It is again an implementation of the Map interface which ensures that its elements are stored in an ascending key order.
import java.util.*;public class SortedMapExample {public static void main(String[] args) {// Create a tree map
TreeMap tm = new TreeMap();// Put elements to the map in any alphabetical order
tm.put("Tom", new Integer(68));
tm.put("Myu", new Integer(79));
tm.put("Hua", new Integer(94));
tm.put("Ren", new Integer(98));
tm.put("Qazi", new Integer(56));// Get a set of the entries
Set set = tm.entrySet();// Display elements using Iterator
Iterator i = set.iterator();
while(i.hasNext()) {
Map.Entry me = (Map.Entry)i.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
// In the output, keys are in ascending order
/* Output:
Hua: 94
Myu: 79
Qazi: 56
Ren: 98
Tom: 68
*/}
}
Conclusion
The above list of interfaces and class implementations is not a complete list but only the most used ones. Hope it helps in your learning journey of Data Structures in JAVA!