Collection (Data Structure)
- List
- ArrayList
- LinkedList
- Stack
- Queue
- Set
- Map
List
ArrayList
// declared and initialized
ArrayList<Integer> intList = new ArrayList<Integer>();
// adding elements to the list: [1, 2, 3]
intList.add(1);
intList.add(2);
intList.add(3);
// Printing: retriving by indexing
System.out.println(intList.get(0)); // output: 1
System.out.println(intList.get(1)); // output: 2
System.out.println(intList.get(2)); // output: 3
// Printing: the whole list to string
System.out.println(intList.toString()); // output: [1,2,3]
// Getting the size of the list
int size = intList.size();
// reseting value at a specific index: set(index, value)
intList.set(1, 10);
// remove list at a specific index: remove(index)
intList.remove(1);
// empties out the whole list
intList.clear();
LinkedList
// declared and initialized
LinkedList<Integer> linkedList = new LinkedList<>();
// adding elements to the list: 1 <- 2 <- 3
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
// Printing: retriving by indexing
System.out.println(linkedList.get(0)); // output: 1
System.out.println(linkedList.get(1)); // output: 2
System.out.println(linkedList.get(2)); // output: 3
// Printing: the whole list to string
// output: [1, 2, 3]
System.out.println(linkedList.toString());
System.out.println(linkedList); // implicitly call toString()
// inserting value at a specific index: add(index, value)
// 1 <- 2 <- 4 <- 3
linkedList.add(2, 4);
// reseting value at a specific index: set(index, value)
// 1 <- 10 <- 4 <- 3
linkedList.set(1, 10);
// Getting the size of the list
int size = linkedList.size();
// remove list at a specific index: remove(index)
// 1 <- 4 <- 3
linkedList.remove(1);
// empties out the whole list
linkedList.clear();
Stack
// declared and initialized
Stack<Integer> intStack = new Stack<Integer>();
// adding elements to the stack: (3,2,1)
intStack.push(1);
intStack.push(2);
intStack.push(3);
// pop(): remove & return the top most element
System.out.println(intStack.pop());
// now remains: (2,1)
// peek(): get the top most element
System.out.println(intStack.peek()); // output: 2
// size(): get the size of the stack
System.out.println(intStack.size()); // output: 2
Queue
import java.util.Queue;
// declared and initialized
Queue<Integer> intQueue = new LinkedList<>();
// adding elements to the queue: (1,2,3)
intQueue.add(1);
intQueue.add(2);
intQueue.add(3);
// poll(): remove & return the top first-in-queue element
System.out.println(intQueue.poll()); // 1
// peek(): get the first-in-queue element
System.out.println(intQueue.peek());
// size(): get the size of the queue
System.out.println(intQueue.size());
Set
// declared and initialized
Set<Integer> intSet = new HashSet<Integer>();
// add(): add values to the set
intSet.add(1);
intSet.add(2);
intSet.add(3);
intSet.add(3); // add repetitive value
intSet.add(3); // add repetitive value
// output: 1 2 3
for (Integer value : intSet) {
System.out.print(value + ' ');
}
// size()
int size = intSet.size();
// contains(): checking if the set contains a certain value
System.out.println(intSet.contains(2)); // output: true
System.out.println(intSet.contains(4)); // output: false
// remove(): remove a certain value (NOT via indexing, but by specifying value)
intSet.remove(3);
Map
import java.util.HashMap;
import java.util.Map;
// declared and initialized
Map<String, Integer> intMap = new HashMap<>();
// put(key, value): add keys and values.
intMap.put("one", 1);
intMap.put("two", 2);
intMap.put("three", 3);
intMap.put("three", 33); // add repetitive key
intMap.put("three", 333); // add repetitive key
// keySet(): call all keys in the map
for (String key : intMap.keySet()) {
System.out.println(key + ' ');
}
// output: one two three
// values(): call all values in the map
for (Integer key : intMap.values()) {
System.out.println(key);
}
// output: 1 2 333
// NOTE: the latest key, value pair added overwrote the previous!
// get(): gets the value corresponding to a certain key
System.out.println(intMap.get("three")); // output: 333
// size(): geting the size of the HashMap
int size = intMap.size();