Skip to content

Commit 1e461e6

Browse files
committed
Adds some more examples to readme file.
1 parent 64b148b commit 1e461e6

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,47 @@ methods in the `JImmutables` utility class:
7979
import static org.javimmutable.collections.util.JImmutables.*;
8080
````
8181

82+
Factory Methods
83+
---
84+
85+
The `JImmutables` class has static factory methods to make it easy to create new instances. Here are various ways to
86+
create the same basic list. Similar factory methods exist for the other collections.
87+
88+
````
89+
List<String> sourceList = Arrays.asList("these", "are", "some", "strings");
90+
JImmutableList<String> empty = list();
91+
JImmutableList<String> aList = empty
92+
.insert("these")
93+
.insert("are")
94+
.insert("some")
95+
.insert("strings");
96+
JImmutableList<String> literal = list("these", "are", "some", "strings");
97+
JImmutableList<String> fromJavaList = list(sourceList);
98+
JImmutableList<String> fromBuilder = JImmutables.<String>listBuilder()
99+
.add("these")
100+
.add("are")
101+
.add("some", "strings")
102+
.build();
103+
assertThat(aList).isEqualTo(literal);
104+
assertThat(fromJavaList).isEqualTo(literal);
105+
assertThat(fromBuilder).isEqualTo(literal);
106+
````
107+
108+
Iterators
109+
---
110+
111+
The collections are all `Iterable` so they can be used in standard `for` loops.
112+
113+
````
114+
int eWordCount = 0;
115+
for (String word : fromBuilder) {
116+
if (word.contains("e")) {
117+
eWordCount += 1;
118+
}
119+
}
120+
assertThat(eWordCount).isEqualTo(3);
121+
````
122+
82123
Streams and Collectors
83124
---
84125

src/test/java/org/javimmutable/collections/ReadmeTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
import org.javimmutable.collections.util.JImmutables;
3939
import org.junit.Test;
4040

41+
import java.util.Arrays;
4142
import java.util.ConcurrentModificationException;
43+
import java.util.List;
4244
import java.util.Map;
4345
import java.util.stream.Collectors;
4446
import java.util.stream.IntStream;
@@ -52,6 +54,32 @@ public class ReadmeTest
5254
@Test
5355
public void creation()
5456
{
57+
List<String> sourceList = Arrays.asList("these", "are", "some", "strings");
58+
JImmutableList<String> empty = list();
59+
JImmutableList<String> aList = empty
60+
.insert("these")
61+
.insert("are")
62+
.insert("some")
63+
.insert("strings");
64+
JImmutableList<String> literal = list("these", "are", "some", "strings");
65+
JImmutableList<String> fromJavaList = list(sourceList);
66+
JImmutableList<String> fromBuilder = JImmutables.<String>listBuilder()
67+
.add("these")
68+
.add("are")
69+
.add("some", "strings")
70+
.build();
71+
assertThat(aList).isEqualTo(literal);
72+
assertThat(fromJavaList).isEqualTo(literal);
73+
assertThat(fromBuilder).isEqualTo(literal);
74+
75+
int eWordCount = 0;
76+
for (String word : fromBuilder) {
77+
if (word.contains("e")) {
78+
eWordCount += 1;
79+
}
80+
}
81+
assertThat(eWordCount).isEqualTo(3);
82+
5583
// use a stream to build a map of numbers to lists of the number's factors
5684
JImmutableMap<Integer, JImmutableList<Integer>> factorMap =
5785
IntStream.range(2, 100).boxed()

0 commit comments

Comments
 (0)