java-stream

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

孤人 提交于 2020-01-01 11:20:33
问题 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

How to eliminate duplicate entries within a stream based on a own Equal class

假如想象 提交于 2020-01-01 10:49:32
问题 I do have a simialar problem like descripted here. But with two differences first I do use the stream api and second I do have an equals() and hashCode() method already. But within the stream the equalitity of the of Blogs are in this context not the same as defined in the Blog class. Collection<Blog> elements = x.stream() ... // a lot of filter and map stuff .peek(p -> sysout(p)) // a stream of Blog .? // how to remove duplicates - .distinct() doesn't work I do have a class with an equal

Lambda expression for supplier to generate IntStream

一曲冷凌霜 提交于 2020-01-01 09:04:35
问题 How do I replace the Supplier code here with lambda expression IntStream inStream = Stream.generate(new Supplier<Integer>() { int x= 1; @Override public Integer get() { return x++ ; } }).limit(10).mapToInt(t -> t.intValue()); inStream.forEach(System.out::println); The output of above piece of code is: 1 2 3 4 5 6 7 8 9 10 回答1: Something like this if you're bound to use Stream.generate specifically : IntStream inStream = Stream.generate(new AtomicInteger(1)::getAndIncrement) .limit(10)

Java 8 reference to a static method vs. instance method

半城伤御伤魂 提交于 2020-01-01 08:47:33
问题 say I have the following code public class A { int x; public boolean is() {return x%2==0;} public static boolean is (A a) {return !a.is();} } and in another class... List<A> a = ... a.stream().filter(b->b.isCool()); a.stream().filter(A::is); //would be equivalent if the static method is(A a) did not exist the question is how do I refer to the instance method version using the A::is type notation? Thanks a lot 回答1: In your example, both the static and the non-static method are applicable for

Conditionally add an operation to a Java 8 stream

女生的网名这么多〃 提交于 2020-01-01 08:33:48
问题 I'm wondering if I can add an operation to a stream, based off of some sort of condition set outside of the stream. For example, I want to add a limit operation to the stream if my limit variable is not equal to -1 . My code currently looks like this, but I have yet to see other examples of streams being used this way, where a Stream object is reassigned to the result of an intermediate operation applied on itself: // Do some stream stuff stream = stream.filter(e -> e.getTimestamp() < max); /

Is Java 8 findFirst().isPresent() more efficient than count() > 0?

佐手、 提交于 2020-01-01 08:26:13
问题 Given I have a stream Stream<T> stream = list.stream().filter(some predicate) where the list is very large, is it more efficient to check whether the stream is non-empty by doing: stream.count() > 0 or by doing: stream.findFirst().isPresent() ? 回答1: If all you want to know, is whether there is a match, you should use list.stream().anyMatch(some predicate) , not only because it is more efficient, but also, because it is the correct idiom for expressing you intention. As said by others,

Multiple aggregate functions in Java 8 Stream API

时光总嘲笑我的痴心妄想 提交于 2020-01-01 04:55:23
问题 I have a class defined like public class TimePeriodCalc { private double occupancy; private double efficiency; private String atDate; } I would like to perform the following SQL statement using Java 8 Stream API. SELECT atDate, AVG(occupancy), AVG(efficiency) FROM TimePeriodCalc GROUP BY atDate I tried : Collection<TimePeriodCalc> collector = result.stream().collect(groupingBy(p -> p.getAtDate(), .... What can be put into the code to select multiple attributes ? I'm thinking of using multiple

How to interleave (merge) two Java 8 Streams?

こ雲淡風輕ζ 提交于 2020-01-01 04:26:14
问题 Stream<String> a = Stream.of("one", "three", "five"); Stream<String> b = Stream.of("two", "four", "six"); What do I need to do for the output to be the below? // one // two // three // four // five // six I looked into concat but as the javadoc explains, it just appends one after the other, it does not interleave / intersperse. Stream<String> out = Stream.concat(a, b); out.forEach(System.out::println); Creates a lazily concatenated stream whose elements are all the elements of the first

Streams in Java, can't figure this code out

强颜欢笑 提交于 2020-01-01 04:11:05
问题 I've found the following code snippet: Function<Integer, Predicate<Integer>> smallerThan = x -> y -> y < x; List<Integer> l = Arrays.asList(5, 6, 7, 23, 4, 5645, 6, 1223, 44453, 60182, 2836, 23993, 1); List<Integer> list2 = l.stream() .filter(smallerThan.apply(l.get(0))) .collect(Collectors.toList()); System.out.println(list2); As output I receive: [4, 1] How does the smallerThan function in this example work, considering that we only pass one parameter smallerThan.apply(l.get(0)) ? 回答1:

“Good” method to call method on each object using Stream API

假装没事ソ 提交于 2020-01-01 04:08:10
问题 Is it possible to run a method, in a consumer, like a method reference, but on the object passed to the consumer: Arrays.stream(log.getHandlers()).forEach(h -> h.close()); would be a thing like: Arrays.stream(log.getHandlers()).forEach(this::close); but that's not working... Is there a possibility with method references, or is x -> x.method() the only way working here? 回答1: You don't need this . YourClassName::close will call the close method on the object passed to the consumer : Arrays