5
5
// In Java, there are already plenty of data structures already available
6
6
// there are grouped under the name the collection API.
7
7
8
- // to create a simple list
9
- var list = List .of (1 , 2 , 3 );
10
-
11
- // a list is an indexed data structure that stores object in the order of insertions
12
- // get() access to an element given an index
13
- var firstElement = list .get (0 );
14
- var lastElement = list .get (list .size () - 1 );
15
-
16
- // contains return true if a value is contained in the list
17
- System .out .println (list .contains (4 ));
18
-
19
- // indexOf returns the first index of the element in the list
20
- System .out .println (list .indexOf (2 ));
21
-
22
- // to loop over the elements of a list, we have a special syntax using the keyword 'for'
23
- var countries = List .of ('UK' , 'US' , 'France' );
24
- for (var country : countries ) {
25
- System .out .println (country );
26
- }
27
-
28
- // you can also loop over the elements using a method forEach
29
- // if you don't understand this one, don't panic, we will see it later
30
- countries .forEach (country -> System .out .println (country ));
31
-
32
-
33
- // a list also defines the method equals() and toString(), so
34
- // you can print a list or test if two list are equals
35
- System .out .println (countries );
36
- System .out .println (list .equals (countries ));
37
-
38
-
39
- // in Java, depending on how you create a collection it can be changed
40
- // after creation or not. Implementation that allow mutation after creation
41
- // are called modifiable
42
-
43
- // by example, the list above (created with the static method of()) is not modifiable
44
- //countries.set(0, 'Poland') // throws an UnsupportedOperationException
45
-
46
- // To create a modifiable list, we use an ArrayList, created using the operator 'new'
47
- // here because there is no element in the list, the compiler has no way to know
48
- // the type of the elements so we have to provide it in between angle brackets ('<' and '>')
49
- var modifiableCountries = new ArrayList <String >();
50
-
51
- // To add elements in a list, we have the method add()
52
- modifiableCountries .add ("UK" );
53
- modifiableCountries .add ("US" );
54
- modifiableCountries .add ("France" );
55
- modifiableCountries .add ("Poland" );
56
-
57
- // to remove an element, we have the method remove()
58
- modifiableCountries .remove ("UK" );
59
-
60
- // an unmodifiable list or a modifiable list have the same set of methods,
61
- // so you can loop over the modiable list the same way
62
- for (var country : modifiableCountries ) {
63
- System .out .println (country );
64
- }
65
-
66
- // You can create a modifiable list from an unmodifiable one using new ArrayList
67
- // with the unmodifiable list as argument
68
- // In that case you don't have to specify the type of the elements
69
- // the compiler already knows the type of list hence the <> (diamond)
70
- var modifiableList = new ArrayList <>(list );
71
- System .out .println (modifiableList );
72
-
73
-
74
8
// Lists are not the only data structure in Java, you also have set, queue and map
75
9
// - a set is set where you can not store the same object twice
76
10
// (object are the same is equals() return true)
@@ -82,8 +16,8 @@ System.out.println(modifiableList);
82
16
var authors = Set .of ("J.R.R. Tolkien" , "Philip K. Dick" , "George R.R. Martin" );
83
17
System .out .println (authors );
84
18
85
- // elements inside a set are organized in a way that make contains fast
86
- System .out .println (authors .contains ("" Philip K . Dick " " ));
19
+ // elements inside a set are organized in a way that make ` contains` fast
20
+ System .out .println (authors .contains ("Philip K. Dick" ));
87
21
88
22
// there are 3 modifiable sets
89
23
// - HashSet
0 commit comments