Skip to content

Commit b1cd5ce

Browse files
author
vvedenin
committed
Add new info to Collections overview
1 parent c54fb7b commit b1cd5ce

File tree

1 file changed

+154
-10
lines changed
  • helloworlds/1.6-usefull-libraries/collections

1 file changed

+154
-10
lines changed

helloworlds/1.6-usefull-libraries/collections/readme.md

Lines changed: 154 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@
4444

4545
| Key's order | Value's order | Duplicate | Analog key | Analog value | Guava | Apache | Eclipse (GS) Collections | JDK |
4646
| ---------- | ------------- | ----------| -----------| ----------|-------| -------| -------------------------|-----|
47-
| not defined | Insertion-order | yes | [HashMap](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html) | [ArrayList](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html) | `ArrayListMultimap` | `MultiValueMap` | `FastListMultimap` | `HashMap<K, ArrayList<V>>` |
48-
| not defined | not defined | no | [HashMap](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html) | [HashSet](https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html) | `HashMultimap` | `MultiValueMap. multiValueMap( new HashMap<K, Set>(), HashSet.class);` | `UnifiedSetMultimap` | `HashMap<K, HashSet<V>>` |
49-
| not defined | sorted | no | [HashMap](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html) | [TreeSet](https://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html) | `Multimaps. newMultimap( HashMap, Supplier <TreeSet>)` | `MultiValueMap. multiValueMap( new HashMap<K, Set>(), TreeSet.class)` | `TreeSortedSet- Multimap` | `HashMap<K, TreeSet<V>>` |
50-
| Insertion-order | Insertion-order | yes | [LinkedHashMap](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html) | [ArrayList](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html) | LinkedList- Multimap | MultiValueMap. multiValueMap(new LinkedHashMap<K, List>(), ArrayList.class); | |LinkedHashMap< K, ArrayList<V>> |
51-
| Insertion-order | Insertion-order | no | [LinkedHashMap](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html) | [LinkedHashSet](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashSet.html) | `LinkedHash- Multimap` | `MultiValueMap. multiValueMap(new LinkedHashMap<K, Set>(), LinkedHashSet.class)` | |`LinkedHashMap<K, LinkedHashSet<V>>` |
52-
| sorted | sorted | no | [TreeMap](https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html) | [TreeSet](https://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html) | `TreeMultima` | `MultiValueMap. multiValueMap( new TreeMap<K, Set>(),TreeSet.class)` | |`TreeMap<K, TreeSet<V>>` |
47+
| not defined | Insertion-order | yes | [HashMap](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html) | [ArrayList](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html) | [ArrayListMultimap](http://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/ArrayListMultimap.html) | `MultiValueMap` | `FastListMultimap` | `HashMap<K, ArrayList<V>>` |
48+
| not defined | not defined | no | [HashMap](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html) | [HashSet](https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html) | [HashMultimap](http://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/HashMultimap.html) | `MultiValueMap. multiValueMap( new HashMap<K, Set>(), HashSet.class);` | `UnifiedSetMultimap` | `HashMap<K, HashSet<V>>` |
49+
| not defined | sorted | no | [HashMap](https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html) | [TreeSet](https://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html) | `Multimaps. newMultimap( HashMap, Supplier <TreeSet>)` | `MultiValueMap.multiValueMap( new HashMap<K, Set>(), TreeSet.class)` | `TreeSortedSet- Multimap` | `HashMap<K, TreeSet<V>>` |
50+
| Insertion-order | Insertion-order | yes | [LinkedHashMap](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html) | [ArrayList](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html) | [LinkedListMultimap](http://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/LinkedListMultimap.html) | MultiValueMap. multiValueMap(new LinkedHashMap<K, List>(), ArrayList.class); | |LinkedHashMap< K, ArrayList<V>> |
51+
| Insertion-order | Insertion-order | no | [LinkedHashMap](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html) | [LinkedHashSet](https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashSet.html) | [LinkedHashMultimap](http://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/LinkedHashMultimap.html) | `MultiValueMap. multiValueMap(new LinkedHashMap<K, Set>(), LinkedHashSet.class)` | |`LinkedHashMap<K, LinkedHashSet<V>>` |
52+
| sorted | sorted | no | [TreeMap](https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html) | [TreeSet](https://docs.oracle.com/javase/8/docs/api/java/util/TreeSet.html) | [TreeMultimap](http://google.github.io/guava/releases/snapshot/api/docs/com/google/common/collect/TreeMultimap.html) | `MultiValueMap. multiValueMap( new TreeMap<K, Set>(),TreeSet.class)` | |`TreeMap<K, TreeSet<V>>` |
5353

5454

5555
##### **Examples using Multimap**
@@ -86,6 +86,7 @@
8686

8787
#### 3.1 Create collections
8888

89+
###### 3.1.1 Create List
8990
| Description | JDK | guava | gs-collections |
9091
| ------------- | -------------| -------------| -------------|
9192
| Create empty list | `new ArrayList<>`() | `Lists.newArrayList()` | `FastList.newList()` |
@@ -98,7 +99,68 @@
9899
| Create list using factory | - | - | `FastList.newWithNValues(10, () -> "1")` |
99100

100101
**Examples:**
101-
*In progress*
102+
103+
System.out.println("createArrayList start");
104+
// Create empty list
105+
List<String> emptyGuava = Lists.newArrayList(); // using guava
106+
List<String> emptyJDK = new ArrayList<>(); // using JDK
107+
MutableList<String> emptyGS = FastList.newList(); // using gs
108+
109+
// Create list with 100 element
110+
List < String > exactly100 = Lists.newArrayListWithCapacity(100); // using guava
111+
List<String> exactly100JDK = new ArrayList<>(100); // using JDK
112+
MutableList<String> empty100GS = FastList.newList(100); // using gs
113+
114+
// Create list with about 100 element
115+
List<String> approx100 = Lists.newArrayListWithExpectedSize(100); // using guava
116+
List<String> approx100JDK = new ArrayList<>(115); // using JDK
117+
MutableList<String> approx100GS = FastList.newList(115); // using gs
118+
119+
// Create list with some elements
120+
List<String> withElements = Lists.newArrayList("alpha", "beta", "gamma"); // using guava
121+
List<String> withElementsJDK = Arrays.asList("alpha", "beta", "gamma"); // using JDK
122+
MutableList<String> withElementsGS = FastList.newListWith("alpha", "beta", "gamma"); // using gs
123+
124+
System.out.println(withElements);
125+
System.out.println(withElementsJDK);
126+
System.out.println(withElementsGS);
127+
128+
// Create list from any Iterable interface (any collection)
129+
Collection<String> collection = new HashSet<>(3);
130+
collection.add("1");
131+
collection.add("2");
132+
collection.add("3");
133+
134+
List<String> fromIterable = Lists.newArrayList(collection); // using guava
135+
List<String> fromIterableJDK = new ArrayList<>(collection); // using JDK
136+
MutableList<String> fromIterableGS = FastList.newList(collection); // using gs
137+
138+
System.out.println(fromIterable);
139+
System.out.println(fromIterableJDK);
140+
System.out.println(fromIterableGS);
141+
/* Attention: JDK create list only from Collection, but guava and gs can create list from Iterable and Collection */
142+
143+
// Create list from any Iterator
144+
Iterator<String> iterator = collection.iterator();
145+
List<String> fromIterator = Lists.newArrayList(iterator); // using guava
146+
System.out.println(fromIterator);
147+
148+
// Create list from any array
149+
String[] array = {"4", "5", "6"};
150+
List<String> fromArray = Lists.newArrayList(array); // using guava
151+
List<String> fromArrayJDK = Arrays.asList(array); // using JDK
152+
MutableList<String> fromArrayGS = FastList.newListWith(array); // using gs
153+
System.out.println(fromArray);
154+
System.out.println(fromArrayJDK);
155+
System.out.println(fromArrayGS);
156+
157+
// Create list using fabric
158+
MutableList<String> fromFabricGS = FastList.newWithNValues(10, () -> String.valueOf(Math.random())); // using gs
159+
System.out.println(fromFabricGS);
160+
161+
System.out.println("createArrayList end");
162+
163+
###### 3.1.2 Create Set
102164

103165
| Description | JDK | guava | gs-collections |
104166
| ------------- | -------------| -------------| -------------|
@@ -110,7 +172,59 @@
110172
| Create set from Array | `new HashSet<>(Arrays.asList(array))` | `Sets.newHashSet(array)` | `UnifiedSet.newSetWith(array)` |
111173

112174
**Examples:**
113-
*In progress*
175+
176+
System.out.println("createHashSet start");
177+
// Create empty set
178+
Set<String> emptyGuava = Sets.newHashSet(); // using guava
179+
Set<String> emptyJDK = new HashSet<>(); // using JDK
180+
Set<String> emptyGS = UnifiedSet.newSet(); // using gs
181+
182+
// Create set with 100 element
183+
Set<String> approx100 = Sets.newHashSetWithExpectedSize(100); // using guava
184+
Set<String> approx100JDK = new HashSet<>(130); // using JDK
185+
Set<String> approx100GS = UnifiedSet.newSet(130); // using gs
186+
187+
// Create set from some elements
188+
Set<String> withElements = Sets.newHashSet("alpha", "beta", "gamma"); // using guava
189+
Set<String> withElementsJDK = new HashSet<>(Arrays.asList("alpha", "beta", "gamma")); // using JDK
190+
Set<String> withElementsGS = UnifiedSet.newSetWith("alpha", "beta", "gamma"); // using gs
191+
192+
System.out.println(withElements);
193+
System.out.println(withElementsJDK);
194+
System.out.println(withElementsGS);
195+
196+
// Create set from any Iterable interface (any collection)
197+
Collection<String> collection = new ArrayList<>(3);
198+
collection.add("1");
199+
collection.add("2");
200+
collection.add("3");
201+
202+
Set<String> fromIterable = Sets.newHashSet(collection); // using guava
203+
Set<String> fromIterableJDK = new HashSet<>(collection); // using JDK
204+
Set<String> fromIterableGS = UnifiedSet.newSet(collection); // using gs
205+
206+
System.out.println(fromIterable);
207+
System.out.println(fromIterableJDK);
208+
System.out.println(fromIterableGS);
209+
/* Attention: JDK create set only from Collection, but guava and gs can create set from Iterable and Collection */
210+
211+
// Create set from any Iterator
212+
Iterator<String> iterator = collection.iterator();
213+
Set<String> fromIterator = Sets.newHashSet(iterator); // using guava
214+
System.out.println(fromIterator);
215+
216+
// Create set from any array
217+
String[] array = {"4", "5", "6"};
218+
Set<String> fromArray = Sets.newHashSet(array); // using guava
219+
Set<String> fromArrayJDK = new HashSet<>(Arrays.asList(array)); // using JDK
220+
Set<String> fromArrayGS = UnifiedSet.newSetWith(array); // using gs
221+
System.out.println(fromArray);
222+
System.out.println(fromArrayJDK);
223+
System.out.println(fromArrayGS);
224+
225+
System.out.println("createHashSet end");
226+
227+
###### 3.1.2 Create Map
114228

115229
| Description | JDK | guava | gs-collections |
116230
| ------------- | -------------| -------------| -------------|
@@ -120,9 +234,39 @@
120234
| Create map from keys | - | - | `UnifiedMap.newWithKeysValues("1", "a", "2", "b")` |
121235

122236
**Examples:**
123-
*In progress*
124237

125-
1. [CreateCollectionTest](https://github.com/Vedenin/useful-java-links/blob/master/helloworlds/1.6-usefull-libraries/collections/gs-eclipse/src/CreateCollectionTest.java)
238+
System.out.println("createHashMap start");
239+
// Create empty map
240+
Map<String, String> emptyGuava = Maps.newHashMap(); // using guava
241+
Map<String, String> emptyJDK = new HashMap<>(); // using JDK
242+
Map<String, String> emptyGS = UnifiedMap.newMap(); // using gs
243+
244+
// Create map with about 100 element
245+
Map<String, String> approx100 = Maps.newHashMapWithExpectedSize(100); // using guava
246+
Map<String, String> approx100JDK = new HashMap<>(130); // using JDK
247+
Map<String, String> approx100GS = UnifiedMap.newMap(130); // using gs
248+
249+
// Create map from another map
250+
Map<String, String> map = new HashMap<>(3);
251+
map.put("k1","v1");
252+
map.put("k2","v2");
253+
Map<String, String> withMap = Maps.newHashMap(map); // using guava
254+
Map<String, String> withMapJDK = new HashMap<>(map); // using JDK
255+
Map<String, String> withMapGS = UnifiedMap.newMap(map); // using gs
256+
257+
System.out.println(withMap);
258+
System.out.println(withMapJDK);
259+
System.out.println(withMapGS);
260+
261+
// Create map from keys
262+
Map<String, String> withKeys = UnifiedMap.newWithKeysValues("1", "a", "2", "b");
263+
System.out.println(withKeys);
264+
265+
System.out.println("createHashMap end");
266+
267+
More examples: [CreateCollectionTest](https://github.com/Vedenin/useful-java-links/blob/master/helloworlds/1.6-usefull-libraries/collections/gs-eclipse/src/CreateCollectionTest.java)
268+
269+
126270
1. [CollectionCompare](https://github.com/Vedenin/useful-java-links/blob/master/helloworlds/1.6-usefull-libraries/collections/apache-commons/src/CollectionCompareTests.java)
127271
1. [CollectionSearch](https://github.com/Vedenin/useful-java-links/blob/master/helloworlds/1.6-usefull-libraries/collections/apache-commons/src/CollectionSearchTests.java)
128272
1. [JavaTransform](https://github.com/Vedenin/useful-java-links/blob/master/helloworlds/1.6-usefull-libraries/collections/apache-commons/src/JavaTransformTest.java)

0 commit comments

Comments
 (0)