java-stream

Java 8 Stream flatMap and group by code compiler error

孤街醉人 提交于 2019-12-01 06:46:18
问题 // given a set of Item objects, group them by the managers of creator and owners Map<String, List<Item>> managersItems = itemSet.parallelStream().flatMap(item -> { // get the list of the creator and owners List<String> users = new ArrayList(); users.add(item.getCreator()); users.addAll(item.getOwners()); return Stream.of(users.toArray(new String[] {})).map(user -> { LdapUserInfo ldapUser = LdapUserInfoFactory.create(user); String manager = ldapUser.getManager(); return new AbstractMap

Java stream - purpose of having both anyMatch and noneMatch operations?

自古美人都是妖i 提交于 2019-12-01 06:37:10
The anyMatch operation will return true if it finds an element - the noneMatch operation will return false it if finds a matching element. The anyMatch operation will return false if it finds no matching element - the noneMatch operation will return true if finds no matching element. Therefore, instead of having both of these operations, could we not just do with one, or am I missing something? In essence, anyMatch returning false is a way of evaluating the truth of noneMatch's predicate. Same reason you have a != b , instead of only supporting ! (a == b) : Easy of use. Clarity of purpose. Yes

How to sort hash map based on number of keys for a value using flatmap java8?

╄→尐↘猪︶ㄣ 提交于 2019-12-01 06:20:35
问题 This is a follow-up of How to get the count of keys for values in a hash map using lambda. I have a HashMap and I want to find the number of keys for each value Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>() {{ put(0, Arrays.asList(1, 2)); put(1, Arrays.asList(2, 0, 3)); put(2, Arrays.asList(4,0,1)); put(3, Arrays.asList(4,1, 5)); put(4, Arrays.asList(5,2,3)); put(5, Arrays.asList(4,3)); }}; According to the above post, I tried flat mapping: Map<Object, Long> ex = map

Replace nested loop with Java 8 flatmap

会有一股神秘感。 提交于 2019-12-01 06:16:16
I'm trying to use flatmap to make a nested loop with the Stream API, but I can't seem to figure it out. As an example, I want to recreate the following loop: List<String> xs = Arrays.asList(new String[]{ "one","two", "three"}); List<String> ys = Arrays.asList(new String[]{"four", "five"}); System.out.println("*** Nested Loop ***"); for (String x : xs) for (String y : ys) System.out.println(x + " + " + y); I can do it like this, but this seems ugly: System.out.println("*** Nested Stream ***"); xs.stream().forEach(x -> ys.stream().forEach(y -> System.out.println(x + " + " + y)) ); Flatmap looks

Java 8 Streams: List to Map with mapped values

纵然是瞬间 提交于 2019-12-01 05:53:18
问题 I'm trying to create a Map from a List using Stream s. The key should be the name of the original item, The value should be some derived data. After .map() the stream consists of Integer s and at the time of .collect() I can't access "foo" from the previous lambda . How do I get the original item in .toMap() ? Can this be done with Stream s or do I need .forEach() ? (The code below is only for demonstration, the real code is of course much more complex and I can't make doSomething() a method

Partition java streams in categories [duplicate]

倖福魔咒の 提交于 2019-12-01 05:52:11
This question already has an answer here: Java 8 List<V> into Map<K, V> 20 answers I have a stream<A> , where class A { String category(); // ... } I would like to get a map<String, list<A>> , where the original stream is partitioned into sublists based on the value of category(). It is pretty trivial to have it implemented using a for loop, but is it possible to get a more elegant solution harnessing java streams? EXAMPLE: Given {[a, xyz], [a, zyx], [b, abc]} , I would like to get a map: a -> {[a, xyz], [a, zyx]} b -> {[b, abc]} Use the groupingBy collector . stream.collect(Collectors

Java 8 Stream add elements to list and sum

有些话、适合烂在心里 提交于 2019-12-01 05:34:15
问题 I believe I can do next using one stream operation on listOfPricedObjects: List<BigDecimal> myList = new ArrayList(); myList = listOfPricedObjects.stream().map(PricedObject::getPrice).collect(Collectors.toList()); BigDecimal sum = listOfPricedObjects.stream().map(PricedObject::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add) How I can fill myList with prices and calculate sum of prices using stream one time? Thanks UPD: As the result I need myList filled with prices and sum variable with

How to sort an ArrayList with object using stream().sorted()

末鹿安然 提交于 2019-12-01 05:20:59
I am having real trouble with using stream and sorted to sort my ArrayList and hope someone can help out. The code uses Croatian words, but I don't think that will be a problem for someone who understands what I mean. This is the ArrayList ArrayList<Publikacija> listaPublikacija = new ArrayList<>(); listaPublikacija.add(prvaKnjiga); listaPublikacija.add(drugaKnjiga); listaPublikacija.add(prviCasopis); listaPublikacija.add(drugiCasopis); In my assignment I am supposed to sort these objects above by getCijena() which is double. This is the best I got and it still doesn't sort it as it should...

Comparator in collector in stream causes issues with type inference?

房东的猫 提交于 2019-12-01 05:15:26
问题 I have the following simplified example that groups a List of Strings into categories, in the form of a TreeMap from Integer to List public static void main(String[] args) { List<String> list = Arrays.asList("A", "B", "C", "D", "E"); TreeMap<Integer, List<String>> res = list.stream() .collect(Collectors.groupingBy( s -> s.charAt(0) % 3, () -> new TreeMap<>(Comparator.<Integer>reverseOrder()), // Type required Collectors.toList() )); System.out.println(res); } If I don't specify the type of

Java stream - purpose of having both anyMatch and noneMatch operations?

梦想与她 提交于 2019-12-01 05:03:10
问题 The anyMatch operation will return true if it finds an element - the noneMatch operation will return false it if finds a matching element. The anyMatch operation will return false if it finds no matching element - the noneMatch operation will return true if finds no matching element. Therefore, instead of having both of these operations, could we not just do with one, or am I missing something? In essence, anyMatch returning false is a way of evaluating the truth of noneMatch's predicate. 回答1