java-stream

Comparison between legacy for loop, streams and parallelStream in Java 8

限于喜欢 提交于 2019-12-30 07:50:53
问题 import java.util.ArrayList; import java.util.List; public class IterationBenchmark { public static void main(String args[]){ List<String> persons = new ArrayList<String>(); persons.add("AAA"); persons.add("BBB"); persons.add("CCC"); persons.add("DDD"); long timeMillis = System.currentTimeMillis(); for(String person : persons) System.out.println(person); System.out.println("Time taken for legacy for loop : "+ (System.currentTimeMillis() - timeMillis)); timeMillis = System.currentTimeMillis();

Collect a Stream of Map<K,V> to Map<K,List<V>>

左心房为你撑大大i 提交于 2019-12-30 06:35:10
问题 I have a Stream< Map< K, V > > and I'm trying to merge those maps together, but preserve duplicate values in a list, so the final type would be Map< K, List<V> > . Is there a way to do this? I know the toMap collector has a binary function to basically choose which value is returned, but can it keep track of the converted list? i.e. if a is a Stream< Map< String, Int > > a.flatMap(map -> map.entrySet().stream()).collect( Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (val1, val2) ->

Using streams to manipulate a String

大兔子大兔子 提交于 2019-12-30 05:43:48
问题 Let's say that I want to remove all the non-letters from my String . String s = "abc-de3-2fg"; I can use an IntStream in order to do that: s.stream().filter(ch -> Character.isLetter(ch)). // But then what? What can I do in order to convert this stream back to a String instance? On a different note, why can't I treat a String as a stream of objects of type Character ? String s = "abc-de3-2fg"; // Yields a Stream of char[], therefore doesn't compile Stream<Character> stream = Stream.of(s

What is equivalent to C#'s Select clause in JAVA's streams API

北慕城南 提交于 2019-12-30 04:41:09
问题 I wanted to filter list of Person class and finally map to some anonymous class in Java using Streams. I am able to do the same thing very easily in C#. Person class class Person { public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } } Code to map the result in desire format. List<Person> lst = new List<Person>(); lst.Add(new Person() { Name = "Pava", Address = "India", Id = 1 }); lst.Add(new Person() { Name = "tiwari", Address = "USA", Id = 2 });

Any way to stream a map like “(k,v)” instead of working with (entry)?

99封情书 提交于 2019-12-30 03:05:11
问题 Basically I look for a way to avoid working with entry -> entry.getValue and entry -> entry.getKey similar to what Map.forEach() does. If only I could get a way to work as map.stream().filter((k,v) -> ) ... and so on It seems the interface is called BiConsumer. Perhaps with a converter to BiConsumer or a Stream.generate() someway 回答1: Since this is a repeating question, I will throw a full solution into the ring. It’s a PairStream type that is by default a simple wrapper around an ordinary

Java Collectors.groupingBy()---is List ordered?

China☆狼群 提交于 2019-12-30 02:45:29
问题 For the Collectors.groupingBy() that returns Map<K,List<T>> is it implied that the List<T> is in order that the stream is evaluated? I see no explicit description of the ordering of the list, whereas the concurrent version explicitly states no ordering. If it weren't ordered somehow, I'd expect it to be a Collection though, and I don't see what other ordering it could possibly be, other than order received. I'm hoping it's guaranteed that the last value in each list is the last value received

Cumulative Sum using Java 8 stream API

做~自己de王妃 提交于 2019-12-30 02:07:54
问题 I have a List of Integer say list1, and I want to get another list list2 which will contain the cumulative sum up until the current index from start. How can I do this using Stream API java 8 ? List<Integer> list1 = new ArrayList<>(); list1.addAll(Arrays.asList(1, 2, 3, 4)); List<Integer> list2 = new ArrayList<>(); // initialization list2.add(list1.get(0)); for(int i=1;i<list1.size();i++) { // increment step list2.add(list2.get(i-1) + list1.get(i)); } How can I change above imperative style

Merge Map<String, List<String> Java 8 Stream

独自空忆成欢 提交于 2019-12-30 01:07:08
问题 I would like to merge two Map with JAVA 8 Stream: Map<String, List<String>> mapGlobal = new HashMap<String, List<String>>(); Map<String, List<String>> mapAdded = new HashMap<String, List<String>>(); I try to use this implementation: mapGlobal = Stream.of(mapGlobal, mapAdded) .flatMap(m -> m.entrySet().stream()) .collect(Collectors.groupingBy(Map.Entry::getKey, Collectors.mapping(Map.Entry::getValue, Collectors.toList()) )); However, this implementation only create a result like: Map<String,

Providing Limit condition on Stream generation [duplicate]

ぐ巨炮叔叔 提交于 2019-12-29 09:01:33
问题 This question already has answers here : Limit a stream by a predicate (19 answers) Closed 5 years ago . I am writing a code to calculate Fibonacci numbers. With this code I can generate first n numbers of the Fibonacci sequence. Stream.generate(new Supplier<Long>() { private long n1 = 1; private long n2 = 2; @Override public Long get() { long fibonacci = n1; long n3 = n2 + n1; n1 = n2; n2 = n3; return fibonacci; } }).limit(50).forEach(System.out::println); The method limit returns the Stream

Why does the Streams API need a hint for generic type in this case?

大兔子大兔子 提交于 2019-12-29 08:45:12
问题 The following fails to compile: @NotNull String defaultFormatter(@Nullable Object value) { if (value instanceof Collection) { return ((Collection) value).stream() .map(MyClass::defaultFormatter) .collect(Collectors.joining(eol)); } return String.valueOf(value); } In particular, when compiled with javac, the error would be: Error:(809, 94) java: incompatible types: java.lang.Object cannot be converted to @org.jetbrains.annotations.NotNull java.lang.String But the following compiles just fine: