At work, we use a plugin architecture to allow developers to integrate with our server, therefore providing a service-oriented solution (or micro-service solution). These plugins process reasonable amounts of data, and store results in collections. Often we are asked which collection works best - so let's discuss the options here...
HashMap
A HashMap stores items in key-value pairs. You then access the value by the key (instead of an array in which you access the value by an index).
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, String> capitalCities = new HashMap<String, String>();
capitalCities.put("Alabama", "Montgomery");
capitalCities.put("Alaska", "Juneau");
capitalCities.put("Arizona", "Phoenix");
capitalCities.put("Arkansas", "Little Rock");
capitalCities.put("California", "Sacramento");
capitalCities.put("Colorado", "Denver");
capitalCities.put("Connecticut", "Hartford");
capitalCities.put("Delaware", "Dover");
capitalCities.put("Florida", "Tallahassee");
capitalCities.put("Georgia", "Atlanta");
capitalCities.put("Hawaii", "Honolulu");
capitalCities.put("Idaho", "Boise");
capitalCities.put("Illinois", "Springfield");
capitalCities.put("Indiana", "Indianapolis");
capitalCities.put("Iowa", "Des Moines");
capitalCities.put("Kansas", "Topeka");
capitalCities.put("Kentucky", "Frankfort");
capitalCities.put("Louisiana", "Baton Rouge");
capitalCities.put("Maine", "Augusta");
capitalCities.put("Maryland", "Annapolis");
capitalCities.put("Massachusetts", "Boston");
capitalCities.put("Michigan", "Lansing");
capitalCities.put("Minnesota", "Saint Paul");
capitalCities.put("Mississippi", "Jackson");
capitalCities.put("Missouri", "Jefferson City");
capitalCities.put("Montana", "Helena");
capitalCities.put("Nebraska", "Lincoln");
capitalCities.put("Nevada", "Carson City");
capitalCities.put("New Hampshire", "Concord");
capitalCities.put("New Jersey", "Trenton");
capitalCities.put("New Mexico", "Santa Fe");
capitalCities.put("New York", "Albany");
capitalCities.put("North Carolina", "Raleigh");
capitalCities.put("North Dakota", "Bismarck");
capitalCities.put("Ohio", "Columbus");
capitalCities.put("Oklahoma", "Oklahoma City");
capitalCities.put("Oregon", "Salem");
capitalCities.put("Pennsylvania", "Harrisburg");
capitalCities.put("Puerto Rico", "San Juan");
capitalCities.put("Rhode Island", "Providence");
capitalCities.put("South Carolina", "Columbia");
capitalCities.put("South Dakota", "Pierre");
capitalCities.put("Tennessee", "Nashville");
capitalCities.put("Texas", "Austin");
capitalCities.put("Utah", "Salt Lake City");
capitalCities.put("Vermont", "Montpelier");
capitalCities.put("Virginia", "Richmond");
capitalCities.put("Washington", "Olympia");
capitalCities.put("West Virginia", "Charleston");
capitalCities.put("Wisconsin", "Madison");
capitalCities.put("Wyoming", "Cheyenne");
capitalCities.put("none", null);
System.out.println(capitalCities);
System.out.println(capitalCities.get("New York"));
System.out.println(capitalCities.get("none"));
capitalCities.remove("none");
System.out.println(capitalCities.get("none"));
}
}
Results:
Albany
null
You'll notice that in the results, the values are not stored in order.
Getting an object based on a correct key will return the associated value.
HashMap allows for null values.
When a key is not found, null is returned.
LinkedList
A LinkedList is a collection of objects of the same type. LinkedLists are the goto structure if you frequently need to add and remove items from the beginning or end (addFirst(), addLast(), removeFirst(), removeLast(), getFirst(), getLast()). It is best to use a LinkedList when you only use the list by looping through it - not trying to find random items.
import java.util.LinkedList;
public class Main {
// We put these fields here because we're not using a second file
String name;
String capital;
public String getName(){
return this.name;
}
public Main(String name, String capital){
this.name = name;
this.capital = capital;
}
public static void main(String[] args) {
LinkedList<Main> capitalCities = new LinkedList<Main>();
capitalCities.add(new Main("Alabama", "Montgomery"));
capitalCities.add(new Main("Alaska", "Juneau"));
capitalCities.add(new Main("Arizona", "Phoenix"));
capitalCities.add(new Main("Arkansas", "Little Rock"));
capitalCities.add(new Main("California", "Sacramento"));
capitalCities.add(new Main("Colorado", "Denver"));
capitalCities.add(new Main("Connecticut", "Hartford"));
capitalCities.add(new Main("Delaware", "Dover"));
capitalCities.add(new Main("Florida", "Tallahassee"));
capitalCities.add(new Main("Georgia", "Atlanta"));
capitalCities.add(new Main("Hawaii", "Honolulu"));
capitalCities.add(new Main("Idaho", "Boise"));
capitalCities.add(new Main("Illinois", "Springfield"));
capitalCities.add(new Main("Indiana", "Indianapolis"));
capitalCities.add(new Main("Iowa", "Des Moines"));
capitalCities.add(new Main("Kansas", "Topeka"));
capitalCities.add(new Main("Kentucky", "Frankfort"));
capitalCities.add(new Main("Louisiana", "Baton Rouge"));
capitalCities.add(new Main("Maine", "Augusta"));
capitalCities.add(new Main("Maryland", "Annapolis"));
capitalCities.add(new Main("Massachusetts", "Boston"));
capitalCities.add(new Main("Michigan", "Lansing"));
capitalCities.add(new Main("Minnesota", "Saint Paul"));
capitalCities.add(new Main("Mississippi", "Jackson"));
capitalCities.add(new Main("Missouri", "Jefferson City"));
capitalCities.add(new Main("Montana", "Helena"));
capitalCities.add(new Main("Nebraska", "Lincoln"));
capitalCities.add(new Main("Nevada", "Carson City"));
capitalCities.add(new Main("New Hampshire", "Concord"));
capitalCities.add(new Main("New Jersey", "Trenton"));
capitalCities.add(new Main("New Mexico", "Santa Fe"));
capitalCities.add(new Main("New York", "Albany"));
capitalCities.add(new Main("North Carolina", "Raleigh"));
capitalCities.add(new Main("North Dakota", "Bismarck"));
capitalCities.add(new Main("Ohio", "Columbus"));
capitalCities.add(new Main("Oklahoma", "Oklahoma City"));
capitalCities.add(new Main("Oregon", "Salem"));
capitalCities.add(new Main("Pennsylvania", "Harrisburg"));
capitalCities.add(new Main("Puerto Rico", "San Juan"));
capitalCities.add(new Main("Rhode Island", "Providence"));
capitalCities.add(new Main("South Carolina", "Columbia"));
capitalCities.add(new Main("South Dakota", "Pierre"));
capitalCities.add(new Main("Tennessee", "Nashville"));
capitalCities.add(new Main("Texas", "Austin"));
capitalCities.add(new Main("Utah", "Salt Lake City"));
capitalCities.add(new Main("Vermont", "Montpelier"));
capitalCities.add(new Main("Virginia", "Richmond"));
capitalCities.add(new Main("Washington", "Olympia"));
capitalCities.add(new Main("West Virginia", "Charleston"));
capitalCities.add(new Main("Wisconsin", "Madison"));
capitalCities.add(new Main("Wyoming", "Cheyenne"));
System.out.println(capitalCities);
System.out.println(capitalCities.getFirst());
System.out.println(capitalCities.getFirst().getName());
}
}
Results:
Main@54bedef2
Alabama
You'll notice that in the results, the values are in order, but the contents are not readable through println.
The same is true for a single object, in which the contents are also unreadable by println.
Getting the contents of the item can be done by manual inspection.
Queue
A LinkedList is a type of Queue. Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Whatever the ordering used, the head of the queue is that element which would be removed by a call to remove() or poll(). In a FIFO queue, all new elements are inserted at the tail of the queue.
import java.util.Queue;
import java.util.LinkedList;
public class Main {
String name;
String capital;
public String getName(){
return this.name;
}
public Main(String name, String capital){
this.name = name;
this.capital = capital;
}
public static void main(String[] args) {
Queue<Main> capitalCities = new LinkedList<Main>();
capitalCities.add(new Main("Alabama", "Montgomery"));
capitalCities.add(new Main("Alaska", "Juneau"));
capitalCities.add(new Main("Arizona", "Phoenix"));
capitalCities.add(new Main("Arkansas", "Little Rock"));
capitalCities.add(new Main("California", "Sacramento"));
capitalCities.add(new Main("Colorado", "Denver"));
capitalCities.add(new Main("Connecticut", "Hartford"));
capitalCities.add(new Main("Delaware", "Dover"));
capitalCities.add(new Main("Florida", "Tallahassee"));
capitalCities.add(new Main("Georgia", "Atlanta"));
capitalCities.add(new Main("Hawaii", "Honolulu"));
capitalCities.add(new Main("Idaho", "Boise"));
capitalCities.add(new Main("Illinois", "Springfield"));
capitalCities.add(new Main("Indiana", "Indianapolis"));
capitalCities.add(new Main("Iowa", "Des Moines"));
capitalCities.add(new Main("Kansas", "Topeka"));
capitalCities.add(new Main("Kentucky", "Frankfort"));
capitalCities.add(new Main("Louisiana", "Baton Rouge"));
capitalCities.add(new Main("Maine", "Augusta"));
capitalCities.add(new Main("Maryland", "Annapolis"));
capitalCities.add(new Main("Massachusetts", "Boston"));
capitalCities.add(new Main("Michigan", "Lansing"));
capitalCities.add(new Main("Minnesota", "Saint Paul"));
capitalCities.add(new Main("Mississippi", "Jackson"));
capitalCities.add(new Main("Missouri", "Jefferson City"));
capitalCities.add(new Main("Montana", "Helena"));
capitalCities.add(new Main("Nebraska", "Lincoln"));
capitalCities.add(new Main("Nevada", "Carson City"));
capitalCities.add(new Main("New Hampshire", "Concord"));
capitalCities.add(new Main("New Jersey", "Trenton"));
capitalCities.add(new Main("New Mexico", "Santa Fe"));
capitalCities.add(new Main("New York", "Albany"));
capitalCities.add(new Main("North Carolina", "Raleigh"));
capitalCities.add(new Main("North Dakota", "Bismarck"));
capitalCities.add(new Main("Ohio", "Columbus"));
capitalCities.add(new Main("Oklahoma", "Oklahoma City"));
capitalCities.add(new Main("Oregon", "Salem"));
capitalCities.add(new Main("Pennsylvania", "Harrisburg"));
capitalCities.add(new Main("Rhode Island", "Providence"));
capitalCities.add(new Main("South Carolina", "Columbia"));
capitalCities.add(new Main("South Dakota", "Pierre"));
capitalCities.add(new Main("Tennessee", "Nashville"));
capitalCities.add(new Main("Texas", "Austin"));
capitalCities.add(new Main("Utah", "Salt Lake City"));
capitalCities.add(new Main("Vermont", "Montpelier"));
capitalCities.add(new Main("Virginia", "Richmond"));
capitalCities.add(new Main("Washington", "Olympia"));
capitalCities.add(new Main("West Virginia", "Charleston"));
capitalCities.add(new Main("Wisconsin", "Madison"));
capitalCities.add(new Main("Wyoming", "Cheyenne"));
System.out.println(capitalCities);
System.out.println(capitalCities.peek());
System.out.println(capitalCities.peek().getName());
System.out.println(capitalCities.remove().getName());
System.out.println(capitalCities.remove().getName());
}
}
Results:
Main@54bedef2
Alabama
Alabama
Alaska
You'll notice that in the results, the values are in order, but the contents are not readable through println.
The same is true for a single object, in which the contents are also unreadable by println.
Getting the contents of the item can be done by manual inspection.
Peeking does not remove an element.
Removing will remove an element (the first one added).
Stack
Opposite to a Queue, the Stack class represents a last-in-first-out (LIFO) stack of objects. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack. Recent implementations include a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.
import java.util.Stack;
public class Main {
String name;
String capital;
public String getName(){
return this.name;
}
public Main(String name, String capital){
this.name = name;
this.capital = capital;
}
public static void main(String[] args) {
Stack<Main> capitalCities = new Stack<Main>();
capitalCities.push(new Main("Alabama", "Montgomery"));
capitalCities.push(new Main("Alaska", "Juneau"));
capitalCities.push(new Main("Arizona", "Phoenix"));
capitalCities.push(new Main("Arkansas", "Little Rock"));
capitalCities.push(new Main("California", "Sacramento"));
capitalCities.push(new Main("Colorado", "Denver"));
capitalCities.push(new Main("Connecticut", "Hartford"));
capitalCities.push(new Main("Delaware", "Dover"));
capitalCities.push(new Main("Florida", "Tallahassee"));
capitalCities.push(new Main("Georgia", "Atlanta"));
capitalCities.push(new Main("Hawaii", "Honolulu"));
capitalCities.push(new Main("Idaho", "Boise"));
capitalCities.push(new Main("Illinois", "Springfield"));
capitalCities.push(new Main("Indiana", "Indianapolis"));
capitalCities.push(new Main("Iowa", "Des Moines"));
capitalCities.push(new Main("Kansas", "Topeka"));
capitalCities.push(new Main("Kentucky", "Frankfort"));
capitalCities.push(new Main("Louisiana", "Baton Rouge"));
capitalCities.push(new Main("Maine", "Augusta"));
capitalCities.push(new Main("Maryland", "Annapolis"));
capitalCities.push(new Main("Massachusetts", "Boston"));
capitalCities.push(new Main("Michigan", "Lansing"));
capitalCities.push(new Main("Minnesota", "Saint Paul"));
capitalCities.push(new Main("Mississippi", "Jackson"));
capitalCities.push(new Main("Missouri", "Jefferson City"));
capitalCities.push(new Main("Montana", "Helena"));
capitalCities.push(new Main("Nebraska", "Lincoln"));
capitalCities.push(new Main("Nevada", "Carson City"));
capitalCities.push(new Main("New Hampshire", "Concord"));
capitalCities.push(new Main("New Jersey", "Trenton"));
capitalCities.push(new Main("New Mexico", "Santa Fe"));
capitalCities.push(new Main("New York", "Albany"));
capitalCities.push(new Main("North Carolina", "Raleigh"));
capitalCities.push(new Main("North Dakota", "Bismarck"));
capitalCities.push(new Main("Ohio", "Columbus"));
capitalCities.push(new Main("Oklahoma", "Oklahoma City"));
capitalCities.push(new Main("Oregon", "Salem"));
capitalCities.push(new Main("Pennsylvania", "Harrisburg"));
capitalCities.push(new Main("Rhode Island", "Providence"));
capitalCities.push(new Main("South Carolina", "Columbia"));
capitalCities.push(new Main("South Dakota", "Pierre"));
capitalCities.push(new Main("Tennessee", "Nashville"));
capitalCities.push(new Main("Texas", "Austin"));
capitalCities.push(new Main("Utah", "Salt Lake City"));
capitalCities.push(new Main("Vermont", "Montpelier"));
capitalCities.push(new Main("Virginia", "Richmond"));
capitalCities.push(new Main("Washington", "Olympia"));
capitalCities.push(new Main("West Virginia", "Charleston"));
capitalCities.push(new Main("Wisconsin", "Madison"));
capitalCities.push(new Main("Wyoming", "Cheyenne"));
System.out.println(capitalCities);
System.out.println(capitalCities.peek());
System.out.println(capitalCities.peek().getName());
System.out.println(capitalCities.pop().getName());
System.out.println(capitalCities.pop().getName());
}
}
Results:
Main@3b07d329
Wyoming
Wyoming
Wisconsin
You'll notice that in the results, the values are in order, but the contents are not readable through println.
The same is true for a single object, in which the contents are also unreadable by println.
Getting the contents of the item can be done by manual inspection.
Peeking does not remove an element.
Popping will remove an element (the last one added).