forked from cirosantilli/java-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionCheat.java
More file actions
153 lines (117 loc) · 4.56 KB
/
CollectionCheat.java
File metadata and controls
153 lines (117 loc) · 4.56 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
# Collection
Important interface that has methods like `add`, `remove`, `equals` and `size`.
http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
Superinterface: `Iterable`.
Notable subinterfaces: `List`, `Set`.
Does not specify if ordered or unordered.
Array does not implement `Collection`.
Collection seems to be a well known non-Java specific computer science term:
http://en.wikipedia.org/wiki/Collection_%28abstract_data_type%29
# Collections vs arrays
Arrays are a Java feature like primitives,
that exists only to allow writing faster code
by mapping directly to hardware representations.
The tradeoff is that the array API is uglier and less flexible.
If you are not super concerned about speed, use collections by default.
http://stackoverflow.com/questions/6100148/collection-interface-vs-arrays
*/
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collection;
import java.util.TreeSet;
public class CollectionCheat {
public static void main(String[] args) {
/*
# add Collection
Ensures that the collection contains the element.
Since collections can be either ordered or not,
this does not necessarily insert at the beginning or end.
Subinterfaces may specify that. E.g., `List` is ordered,
and List#add` always adds to the end of the list.
*/
{
/*
Return value: true if the collection was changed, false otherwise.
E.g. for List it is always true because duplicates are allowed.
But for Set it can be false since duplicates are not allowed.
*/
{
Collection<Integer> c = new TreeSet<Integer>();
assert c.add(1);
assert !c.add(1);
}
}
/*
# Initialize collection
http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line
There is no simple one liner to initialize a collection with given values.
The best way is likely to `add` them one by one.
A slightly less verbose possibility is the "double brace initialization idiom".
*/
{
/*
# Double brace initialization idiom
Creates an anonymous inner class,
and adds an instance initializer to it.
Downside: for classes that implement serializable,
you need to add an explicit serialVersionUID or you will get a warning.
TODO why?
The main cause of this idiom is that `add`
does not return the modified collection itself, so you cannot chain
`add().add()` and pass the result to a method invocation.
Even C++ is more convenient on this point, with its initializer lists.
*/
{
ArrayList<Integer> l = new ArrayList<Integer>() {
private static final long serialVersionUID = 1L;
{
add(0);
add(1);
}
};
ArrayList<Integer> l2 = new ArrayList<>();
l2.add(0);
l2.add(1);
assert l.equals(l2);
/*
If are fine with an immutable collections, see
`Collections` `singletonXXX()`, `emptyXXX` families and `nCopies.
*/
/*
`Arrays.asList()` is a possibility
is you want multiple different elements and but are fine with fixed size.
*/
}
}
/*
# toArray
Creates a new array.
*/
{
Collection<Integer> l = new ArrayList<>();
l.add(0);
l.add(1);
l.add(2);
assert Arrays.equals(l.toArray(), new Integer[]{0, 1, 2});
}
/*
# equals Collection
Does not specify anything about the semantics besides Object's contract.
Inheriting interfaces however do.
Notably, `List` and `Set` say that their comparison
must be based on the `equals` of the contained elements.
*/
/*
# isEmpty
*/
{
Collection<Integer> c = new ArrayList<Integer>();
assert c.isEmpty();
c.add(1);
assert !c.isEmpty();
c.remove(1);
assert c.isEmpty();
}
}
}