@fernandob/

Data Structures

Java

Arrays, LinkedLists, Stacks, Queues, and Maps

fork
loading
Files
  • Main.java
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Map;
import java.util.Stack;
import java.util.Queue;

class Main {
	public static void main(String[] args) {

		// Array
		// Start with data type, String, int, bool, etc followed by []

		int counter = 1;
		int[] seasonKillCount = { 59, 130, 87};

		for (int killCount : seasonKillCount) {
			System.out.printf("Season %d kill count: %d %n", counter, killCount);
			counter++;
		}

		// ArrayList
		// Though slower than arrays, it adds dynamic functionality
		// import java.util.ArrayList;

		ArrayList<String> avengersActive = new ArrayList<>();
		avengersActive.add("Scarlet Witch");
		avengersActive.add("Spider-Man");

		for (String avenger : avengersActive) {
			System.out.printf("%s is alive %n", avenger);
		}

		// LinkedList
		// Every element has a pointer with the last element pointer to null
		// Unlike Arrays or ArrayList, be of any type interchangable, int + String, bool, etc
		// import java.util.LinkedList;

		LinkedList LinkedListExample = new LinkedList();

		LinkedListExample.add("London");
		LinkedListExample.add(true);
		LinkedListExample.addFirst(3);

		ListIterator iterator = LinkedListExample.listIterator(LinkedListExample.size());
		while(iterator.hasPrevious()) {
			System.out.println(iterator.previous());
		}

		// Stack
		// A Stack uses a last "pushed", first "pop" order or LIFO (last in first out)
		// Think of plates, you dont grab the bottom plate
		// push, pop and peek methods to add, remove and get first copy
		// import java.util.Stack;

		Stack stackExample = new Stack();
		for( int i = 1; i <= 5 ; i++) {
			stackExample.push(i);
		}

		System.out.println(stackExample);
		System.out.println(stackExample.peek());

		// Queue
		// A linear list where items are added to one end and deleted from the other end
		// Think of a line at the movies, concert, etc, new people join the queue at the end
		// while the first people exit from the front
		// Basic Methods include add, peek, remove, and poll, with removes from top but 
		// returns null if empty instead of error
		// import java.util.Queue;

		Queue<Integer> pplWaitingForEndGame = new LinkedList<>();
		for(int i = 0; i <= 10; i++) {
			pplWaitingForEndGame.add(i);
		}

		System.out.println(pplWaitingForEndGame+" people waiting");


		// Map <K,V> with data type i.e String, Int
		// map data with key and value
		// Methods include get, put, remove,
		// import java.util.Map;
		// import java.util.HashMap;

		Map<String, String> avengers = new HashMap<>();

		avengers.put("Iron Man", "Tony Stark");
		avengers.put("Captain America", "Steve Rogers");
		avengers.put("Thor", "Thor Odinson");

		String strongestAvenger = avengers.get("Thor");
		System.out.println(avengers);
		System.out.println(strongestAvenger);
	
	}
}