-
Notifications
You must be signed in to change notification settings - Fork 68
/
chapter30-data_structure.jsh
31 lines (21 loc) · 1.18 KB
/
chapter30-data_structure.jsh
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
// To starts, run jshell --enable-preview which is a program able to interpret Java syntax
// then cut and paste the following lines to see how it works
// To exit jshell type /exit
// In Java, there are already plenty of data structures already available
// there are grouped under the name the collection API.
// Lists are not the only data structure in Java, you also have set, queue and map
// - a set is set where you can not store the same object twice
// (object are the same is equals() return true)
// - a queue add or remove object at the head or at the tail of the queue
// (so a stack is a queue, a FIFO is a queue, etc)
// - a map is a dictionary that associate a key (which is unique) to a value
// so to create an unmodifiable set, using the static method of()
var authors = Set.of("J.R.R. Tolkien", "Philip K. Dick", "George R.R. Martin");
System.out.println(authors);
// elements inside a set are organized in a way that make `contains` fast
System.out.println(authors.contains("Philip K. Dick"));
// there are 3 modifiable sets
// - HashSet
// - LinkedHashSet, as fast as set
// - TreeSet, elements are sorted
// a set has no order by default, apart if you create a LinkedHashSet