java-stream

How to merge two arrays into a map using Java streams?

牧云@^-^@ 提交于 2019-12-01 18:00:43
问题 Lets suppose we were given the following two arrays String[] keys = new String[] {"a", "b", "c", "aa", "d", "b"} int[] values = new int[] { 1 , 2 , 3 , 4 , 5 , 6 } And by merging these 2 arrays into HashTable we get the following // pseudo-code Map<String, Integer> dictionary = new HashTable<>( ("a" => 1) ("b" => 8) // because "b" appeared in index 1 and 5 ("c" => 3) ("aa" => 4) ("d" => 5) ); How can we do this using java Lambda style? So far I have the following: // this loops through the

what does java8 stream map do here?

一笑奈何 提交于 2019-12-01 17:26:20
I was confused about the difference between map() and forEach() method in java8 stream. For instance, List<String> strings = Lists.newArrayList("1", "2"); Map<String, String> map = Maps.newHashMap(); strings.stream().map(s->map.put(s, s)); System.out.println(map); I got empty output here, but if I change map to forEach() just like List<String> strings = Lists.newArrayList("1", "2"); Map<String, String> map = Maps.newHashMap(); strings.stream().forEach(s->map.put(s, s)); System.out.println(map); I can get {1=1, 2=2} Why it just didn't run map() method? What's difference between them? strings

Stream doesn't preserve the order after grouping

孤街浪徒 提交于 2019-12-01 16:56:08
问题 I have a List name availableSeats I am sorting and grouping by the blockIndex property like below: availableSeats.stream() .sorted(Comparator.comparing(SeatedTicketAssignment::getBlockIndex)) .collect(Collectors.groupingBy(SeatedTicketAssignment::getBlockIndex)) .forEach((block, blockAssignments) -> { //Rest of the code } The problem is that the result of grouping by is not sorted by the blockIndex. 回答1: Keep in mind, Collectors#groupingBy(Function) will return a HashMap , which does not

Filling a Multidimensional Array using a Stream

岁酱吖の 提交于 2019-12-01 16:33:40
问题 I'm new to Java 8 and currently failing to grasp Streams fully, is it possible to fill an array using the Stream functional operations? This is an example code of how I would do it with a standard for loop: public static void testForLoop(){ String[][] array = new String[3][3]; for (int x = 0; x < array.length; x++){ for (int y = 0; y < array[x].length; y++){ array[x][y] = String.format("%c%c", letter(x), letter(y)); } } } public static char letter(int i){ return letters.charAt(i); } If it is

Filling a Multidimensional Array using a Stream

两盒软妹~` 提交于 2019-12-01 16:31:25
I'm new to Java 8 and currently failing to grasp Streams fully, is it possible to fill an array using the Stream functional operations? This is an example code of how I would do it with a standard for loop: public static void testForLoop(){ String[][] array = new String[3][3]; for (int x = 0; x < array.length; x++){ for (int y = 0; y < array[x].length; y++){ array[x][y] = String.format("%c%c", letter(x), letter(y)); } } } public static char letter(int i){ return letters.charAt(i); } If it is possible how would I do it using Stream? If it is possible, is it convenient (performance and

java 8 Collector<String, A, R> is not a functional interface, who can tell why?

别来无恙 提交于 2019-12-01 16:20:38
The follow code : public class Test { public static void main(String[] args) { Stream.of(1,2,3).map(String::valueOf).collect(Collectors::toList) } } intellij tell me : Collector<String, A, R> is not a functional interface but when i modify the code as follows, everything is ok, i don't know why? public class Test { public static void main(String[] args) { Stream.of(1,2,3).map(String::valueOf).collect(Collectors.<String>toList) } } The reason that the first syntax is illegal is that the target type implied by the method signature— Stream.collect(Collector) —is a Collector . Collector has

Skip last x elements in Stream<T>

自古美人都是妖i 提交于 2019-12-01 16:12:41
If I have a Stream<T> , I can easily use skip(long) to skip the first few elements of a stream. However, there seems to be no equivalent for skipping a given number of elements at the end of the stream. The most obvious solution is to use limit(originalLength - elementsToRemoveAtEnd) , but that requires knowing the initial length beforehand, which isn't always the case. Is there a way to remove the last few elements of a stream of unknown length without having to collect it into a Collection , count the elements and stream it again? There is no general storage-free solution for Stream s that

Java stream API: are there syntax sugar for identity functor?

只谈情不闲聊 提交于 2019-12-01 16:10:01
问题 We use several Map as simple memory DB over list of objects: class Person { public String id; public String phone; public String email; // and get/set and other fields... } List<Person> persons; Map<String, Person> emailLookup = persons.stream() .collect(Collectors.toMap(Person::getEmail, p -> p)); Map<String, Person> phoneLookup = persons.stream() .collect(Collectors.toMap(Person::getPhone, p -> p)); Map<String, Person> idLookup = persons.stream() .collect(Collectors.toMap(Person::getId, p -

What is more efficient: sorted stream or sorting a list?

点点圈 提交于 2019-12-01 15:45:46
Assume we have some items in a collection and we want to sort them using certain comparator, expecting result in a list: Collection<Item> items = ...; Comparator<Item> itemComparator = ...; One of the approaches is to sort items in a list, something like: List<Item> sortedItems = new ArrayList<>(items); Collections.sort(sortedItems, itemComparator); Anothe approach is using a sorted stream: List<Item> sortedItems = items .stream() .sorted(itemComparator) .collect(Collectors.toList()); I wonder, which approach is more efficient? Are there any advantages of a sorted stream (like faste sorting on

Stream of maps to map

烈酒焚心 提交于 2019-12-01 15:30:00
How can I flatten a Stream of Map s (of the same types) to a single Map in Java 8? Map<String, Long> toMap(Stream<Map<String, Long>> stream) { return stream. ??? } My syntax may be a bit off, but flatMap should do most of the work for you : Map<String, Long> toMap(Stream<Map<String, Long>> stream) { return stream.flatMap (map -> map.entrySet().stream()) // this would create a flattened // Stream of all the map entries .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())); // this should collect // them to a single map } 来源: https://stackoverflow.com/questions/26752919/stream-of-maps