java-stream

List of Only One Single value of Filtered List Value

人走茶凉 提交于 2020-01-02 06:56:56
问题 I want to do divide and conquers to solve this question I have this class before shown in my question Generate List with Combination of Subset of List, Java I have a class named Competitor, with Type, Name and Power. public class Competitor { private final int type; private final String name; private final int power; public Competitor(int type, String name, int power) { this.type = type; this.name = name; this.power = power; } public int getType() { return type; } public String getName() {

Is Stream.findAny a short-circuit operation?

孤人 提交于 2020-01-02 06:46:12
问题 Consider this code Object found = collection.stream() .filter( s -> myPredicate1(s)) .filter( s -> myPredicate2(s)) .findAny() Will it process entire stream, and call both myPredicate1 and myPredicate2 for all elements of the collection? Or will as many predicates be called as are needed to actually find the value? 回答1: Yes it is, as the Stream.findAny() documentation states: This is a short-circuiting terminal operation. It's a common misconception that objects in stream are "pushed" towards

Creating Map composed of 2 Lists using stream().collect in Java

故事扮演 提交于 2020-01-02 02:11:27
问题 As for example, there are two lists: List<Double> list1 = Arrays.asList(1.0, 2.0); List<String> list2 = Arrays.asList("one_point_zero", "two_point_zero"); Using Stream, I want to create a map composed of these lists, where list1 is for keys and list2 is for values. To do it, I need to create an auxiliary list: List<Integer> list0 = Arrays.asList(0, 1); Here is the map: Map<Double, String> map2 = list0.stream() .collect(Collectors.toMap(list1::get, list2::get)); list0 is used in order list1:

Replacing traditional newForLoop with Java 8 Streams

五迷三道 提交于 2020-01-02 02:03:07
问题 So, finally making a relatively large jump from Java 6 to Java 8, I've read up a fair amount of Java 8 Streams API. Unfortunately, almost all the examples that have been asked are almost close to what I'm trying to figure out how to do, but not close enough. What I have is final List<Function<? super Double, Double>> myList = generateList(); final double myVal = calculate(10); private double calculate(double val) { for (Function<? super Double, Double> function : this.myList) { val +=

How to preserve newlines while reading a file using stream - java 8

做~自己de王妃 提交于 2020-01-02 01:20:11
问题 try (Stream<String> lines = Files.lines(targetFile)) { List<String> replacedContent = lines.map(line -> StringUtils.replaceEach(line,keys, values)) .parallel() .collect(Collectors.toList()); Files.write(targetFile, replacedContent); } I'm trying to replace multiple text patterns in each line of the file. But I'm observing that "\r\n"(byte equivalent 10 and 13) is being replaced with just "\r"(just 10) and my comparison tests are failing. I want to preserve the newlines as they are in the

How to get a custom type instead of Integer when using Collectors.summingInt?

我与影子孤独终老i 提交于 2020-01-02 01:05:14
问题 I am currently creating a Map<String, Map<LocalDate, Integer>> like this, where the Integer represents seconds: Map<String, Map<LocalDate, Integer>> map = stream.collect(Collectors.groupingBy( x -> x.getProject(), Collectors.groupingBy( x -> x.getDate(), Collectors.summingInt(t -> t.getDuration().toSecondOfDay()) ) )); How could I instead create a Map<String, Map<LocalDate, Duration>> ? 回答1: To change that Integer from Collectors.summingInt to a Duration , you simply need to replace that

Why do we have to cast the List returned by Collectors.toList() to List<Integer> even though the elements of the Stream are already mapped to Integer? [duplicate]

左心房为你撑大大i 提交于 2020-01-01 19:35:08
问题 This question already has answers here : What is a raw type and why shouldn't we use it? (15 answers) Why does the Streams API need a hint for generic type in this case? (1 answer) Why does javac complain about generics unrelated to the class' type arguments? [duplicate] (1 answer) Closed 2 years ago . I'm mapping a raw Stream to a Stream<Integer> and then collect the elements to a List<Integer> . Why do I have to cast the result of collect(Collectors.toList()) to List<Integer> if my mapper -

How do you invert Map<v1, Set<v2>> to Map<v2, Set<v1>> in Java with stream()

╄→гoц情女王★ 提交于 2020-01-01 19:11:58
问题 I have a Map object Map<t1, Set<t2>> , and I want to go into the set and turn t2 in the sets into the keys of the new map. The original key t1 will be the new value of the map. For example, given a map containing two entries {key1: [a, b, c], key2: [c, d]} The resulting map would be {a: [key1], b: [key1], c: [key1, key2], d: [key2]} [ ] denotes Set in the above examples. 回答1: Java 8: map.entrySet() .stream() .flatMap(e -> e.getValue() .stream() .map(v -> new SimpleEntry<>(v, e.getKey())))

How do you invert Map<v1, Set<v2>> to Map<v2, Set<v1>> in Java with stream()

人盡茶涼 提交于 2020-01-01 19:11:47
问题 I have a Map object Map<t1, Set<t2>> , and I want to go into the set and turn t2 in the sets into the keys of the new map. The original key t1 will be the new value of the map. For example, given a map containing two entries {key1: [a, b, c], key2: [c, d]} The resulting map would be {a: [key1], b: [key1], c: [key1, key2], d: [key2]} [ ] denotes Set in the above examples. 回答1: Java 8: map.entrySet() .stream() .flatMap(e -> e.getValue() .stream() .map(v -> new SimpleEntry<>(v, e.getKey())))

Java 8: How to convert String to Map<String,String>?

情到浓时终转凉″ 提交于 2020-01-01 11:23:00
问题 I have a Map: Map<String, String> utilMap = new HashMap(); utilMap.put("1","1"); utilMap.put("2","2"); utilMap.put("3","3"); utilMap.put("4","4"); I converted it to a String: String utilMapString = utilMap .entrySet() .stream() .map(e -> e.toString()).collect(Collectors.joining(",")); Out put: 1=1,2=2,3=3,4=4,5=5 How to convert utilMapString to Map in Java8? Who can help me with? 回答1: Split the string by , to get individual map entries. Then split them by = to get the key and the value. Map