Skip to content

Commit

Permalink
Add README section for stream API (gstamatelat#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatelat committed Dec 29, 2017
1 parent 921e4ea commit df090d6
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ rs.feed("collection", 1)
System.out.println(rs.sample());
```

Unweighted random sampling using the Java 8 stream API.
```java
RandomSamplingCollector<Integer> collector = WatermanSampling.collector(5, new Random());
Collection<Integer> sample = IntStream.range(0, 20).boxed().collect(collector);
System.out.println(sample);
```

Weighted random sampling using the Java 8 stream API.
```java
WeightedRandomSamplingCollector<String> collector = ChaoSampling.weightedCollector(2, new Random());
Map<String, Double> map = new HashMap<>();
map.put("collection", 1.0);
map.put("algorithms", 2.0);
map.put("java", 2.0);
map.put("random", 3.0);
map.put("sampling", 4.0);
map.put("reservoir", 5.0);
Collection<String> sample = map.entrySet().stream().collect(collector);
System.out.println(sample);
```

## Algorithms

| Class | Algorithm | Space | Weighted |
Expand Down

0 comments on commit df090d6

Please sign in to comment.