java-stream

Retrieve fixed number of entries around an entry in a map sorted by values

匆匆过客 提交于 2019-12-05 16:56:45
The POJO viz. Entry.java represents an entry in the leaderboard. Position is the position in the leaderboard, 1 being the user with the highest score public class Entry { private String uid; private int score; private int position; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + score; result = prime * result + ((uid == null) ? 0 : uid.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; if (!(obj instanceof Entry)

Collect stream with grouping, counting and filtering operations

别说谁变了你拦得住时间么 提交于 2019-12-05 16:55:47
问题 I'm trying to collect stream throwing away rarely used items like in this example: import java.util.*; import java.util.function.Function; import static java.util.stream.Collectors.*; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; import org.junit.Test; @Test public void shouldFilterCommonlyUsedWords() { // given List<String> allWords = Arrays.asList( "call", "feel", "call", "very", "call", "very", "feel", "very", "any"); // when

Map<String,List<String>> to Pair<String,String>

微笑、不失礼 提交于 2019-12-05 16:54:28
Using Java 8 Stream API how can I flat a Map to Pair list where left pair value is the map key? Example: If given map was 1 => {1, 2, 3} 2 => {2, 4} Then desired output is the stream of five pairs: (1,1) , (1,2) , (1,3) , (2,2) , (2,4) List<Pair<String, String>> result = map.entrySet() .stream() .flatMap( entry -> entry.getValue() .stream() .map(string -> new Pair<>(entry.getKey(), string))) .collect(Collectors.toList()); 来源: https://stackoverflow.com/questions/32189147/mapstring-liststring-to-pairstring-string

Multiple “match” checks in one stream

半城伤御伤魂 提交于 2019-12-05 16:38:46
问题 Is it possible to check if an array (or collection) contains element 5 and element other than 5. In one stream returning boolean result instead of using two streams: int[] ints = new int[]{1, 2, 3, 4, 5}; boolean hasFive = IntStream.of(ints).anyMatch(num -> num == 5); boolean hasNonFive = IntStream.of(ints).anyMatch(num -> num != 5); boolean result = hasFive && hasNonFive; 回答1: Here's two solutions involving my StreamEx library. The core feature I'm using here is the concept of short

Concatenate the String values of all Maps in a List

二次信任 提交于 2019-12-05 16:24:59
I am trying to adapt Lambda features, however few struggles here and there. List<Map<String, String>> list = new LinkedList<>(); Map<String, String> map = new HashMap<>(); map.put("data1", "12345"); map.put("data2", "45678"); list.add(map); I just want to print the values in comma separated format like 12345,45678 So here goes my trial list.stream().map(Map::values).collect(Collectors.toList()) //Collectors.joining(",") and the output is [[12345,45678]] . It means, there's a list and inside list it's creating the comma separated value at 0 index. I do understand why it's doing though. But I

Java 8 streams: conditional Collector

丶灬走出姿态 提交于 2019-12-05 16:23:30
I want to use Java 8 streams to convert a List of String values to a single String. A List of values like "A", "B" should return a String like "Values: 'A', 'B' added". This works fine, however I want to change the Pre- and Postfix depending on the amount of values. For example, if I have a List of only "A" I want the resulting String to be "Value 'A' added". import java.util.stream.Collectors; import java.util.ArrayList; import java.util.List; public class HelloWorld { public static void main(String[] args) { List<String> values = new ArrayList<>(); values.add("A"); values.add("B"); values

How to convert a Java 8 Stream into a two dimensional array?

北慕城南 提交于 2019-12-05 16:09:20
I’m trying to convert a map based Stream into a two-dimensional array. I have figured out how to store it in a one dimensional array. Here is working code snippet: Float[] floatArray = map.entrySet() .stream() .map(key -> key.getKey().getPrice()) .toArray(size -> new Float[size]); When I execute the above code, I get my Float array populated as expected. Now I need to extend this to a two-dimensional array where I need to store the result in first dimension of a 2d array along these lines: Float[][1] floatArray = map.entrySet() .stream() .map(key -> key.getKey().getPrice()) .toArray(size ->

How to divide 1 completablefuture to many completablefuture in stream?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 15:44:33
For example I have such methods: public CompletableFuture<Page> getPage(int i) { ... } public CompletableFuture<Document> getDocument(int i) { ... } public CompletableFuture<Void> parseLinks(Document doc) { ... } And my flow: List<CompletableFuture> list = IntStream .range(0, 10) .mapToObj(i -> getPage(i)) // I want method like this: .thenApplyAndSplit(CompletableFuture<Page> page -> { List<CompletableFuture<Document>> docs = page.getDocsId() .stream() .map(i -> getDocument(i)) .collect(Collectors.toList()); return docs; }) .map(CompletableFuture<Document> future -> { return future.thenApply

How to get n first values from an Iterator in Java 8?

。_饼干妹妹 提交于 2019-12-05 15:06:20
I have sorted a HashMap using Sort a Map<Key, Value> by values (Java) to that I have a LinkedHashMap , i.e. an Iterable which garantees iteration order. Now, I'd like to retrieve a java.util.List of the first n entries of the map with a one-liner, if possible with a Java 8 Collection Stream API-technique. I found how can i get two consecutive values from Iterator which explains that there's a possibility to do that with an array, but that's not elegant, differs from my intention to get a List (although that can be transformed, but it's an unnecessary step) and requires an extra method. Stream

Java 9 Collectors.flatMapping rewritten in Java 8

风格不统一 提交于 2019-12-05 14:49:50
问题 I got in touch with a new feature since java-9 called Collectors.flatMapping that takes place as a downstream of grouping or partitioning. Such as (example taken from here): List<List<Integer>> list = Arrays.asList( Arrays.asList(1, 2, 3, 4, 5, 6), Arrays.asList(7, 8, 9, 10)); Map<Integer, List<Integer>> map =list.stream() .collect(Collectors.groupingBy( Collection::size, Collectors.flatMapping( l -> l.stream().filter(i -> i % 2 == 0), Collectors.toList()))); {4=[8, 10], 6=[2, 4, 6]} This is