java-stream

How to collect two fields of an object into the same list?

柔情痞子 提交于 2021-02-04 11:21:08
问题 I have an Object of goods, which has two properties: firstCategoryId and secondCategoryId . I have a list of goods, and I want to get all category Ids (including both firstCategoryId and secondCategoryId). My current solution is: List<Integer> categoryIdList = goodsList.stream().map(g->g.getFirstCategoryId()).collect(toList()); categoryIdList.addAll(goodsList.stream().map(g->g.getSecondCategoryId()).collect(toList())); Is there a more convenient manner I could get all the categoryIds in a

How to create a nested Map using Collectors.groupingBy?

我是研究僧i 提交于 2021-02-04 10:28:11
问题 I have a list of class say ProductDto public class ProductDto { private String Id; private String status; private Booker booker; private String category; private String type; } I want to have a Map as below:- Map<String,Map<String,Map<String,Booker>> The properties are to be mapped as below: Map<status,Map<category,Map<type,Booker> I know one level of grouping could be done easily without any hassles using Collectors.groupingBy . I tried to use this for nested level but it failed for me when

filtering a stream against items in another list

不打扰是莪最后的温柔 提交于 2021-02-04 07:49:48
问题 trying to filter a stream against data within a different list: It works, but I use a for loop in the middle of the stream. I cannot find any information of how to convert the for loop to a stream. I could just .stream() the selction.getItems() than .forEach() and have a new .stream() of DATA.accounts, but that is poor code as it would have to restream on every .forEach. y=1; DATA.accounts.stream() .flatMap(estimate -> estimate.getElements().stream()) .filter( ele-> { // different list; for

Collect a list of ids based on multiple fields

走远了吗. 提交于 2021-02-04 07:38:10
问题 I have a person object with personId, age and gender. public class Person { private int personId; private int age; private int gender; // 0 for male and 1 for female } List<Person> person = new Arraylist<>(); person.add(new Person(1,1,1)); person.add(new Person(2,2,0)); person.add(new Person(3,10,1)); person.add(new Person(4,11,0)); person.add(new Person(5,20,1)); person.add(new Person(6,20,1)); person.add(new Person(7,2,0)); person.add(new Person(8,20,0)); person.add(new Person(9,11,0));

Difference between anyMatch and findAny in java 8

ε祈祈猫儿з 提交于 2021-02-04 03:20:34
问题 I have an Array and want to perform some matching on it's element. I came to know that it could be done in two ways in java 8 : String[] alphabet = new String[]{"A", "B", "C"}; anyMatch : Arrays.stream(alphabet).anyMatch("A"::equalsIgnoreCase); findAny : Arrays.stream(alphabet).filter("a"::equalsIgnoreCase) .findAny().orElse("No match found")); As I can understand both are doing the same work. However, I could not found which one to prefer? Could someone please make it clear what is the

Filter a stream of a class using a value of subclass

浪子不回头ぞ 提交于 2021-02-02 09:39:26
问题 I have a parent class Company which has a list of Employee objects. I need to create a stream of Parent class who has an Employee with the phone number mentioned. Company cmp1 = new Company(); cmp1.setName("oracle"); Company cmp2 = new Company(); cmp1.setName("Google"); Employee emp1 = new Employee(); emp1.setName("David"); emp1.setPhone("900"); Employee emp2 = new Employee(); emp2.setName("George"); emp2.setPhone("800"); Employee emp4 = new Employee(); emp4.setName("BOB"); emp4.setPhone("300

How to group objects from a list which can belong to two or more groups?

拥有回忆 提交于 2021-02-01 03:59:08
问题 I have a list of Items where each Item can belong to one or more category. For a limited set of categories(string) I want to create a map with category as key and list of Items as value. Assume my Item class is defined as shown below: public static class Item{ long id; List<String> belongsToCategories; public List<String> getBelongsToCategories() { return belongsToCategories; } public void setBelongsToCategories(List<String> belongsToCategories) { this.belongsToCategories =

Combine two Stream into one Flux

时光毁灭记忆、已成空白 提交于 2021-01-29 14:12:45
问题 How can I combine two streams Stream<String> into Flux ? What I understand is that I might need to use Flux create method to create this but I am not really sure about it: flux1.create(sink -> { sink.onRequest(L -> { for(long l = 0; l < L; l++) { sink.next(..); } }); }) Please help. 回答1: Concat the Stream s into one and then invoke Flux#fromStream : Flux<String> flux = Flux.fromStream(Stream.concat(stream1, stream2)); Another way of doing this would be to create a Flux using Flux#fromStream

Creating nested maps with Java stream and groupingBy

孤街浪徒 提交于 2021-01-29 12:52:07
问题 Given the following code: private enum TheA { AA, } private class TheB { String b; } private enum TheC { CC, } private class Rule { TheA a; TheB b; TheC c; public TheA getA() { return a; } public TheB getB() { return b; } public TheC getC() { return c; } } private Map<TheA, Map<TheB, Set<TheC>>> createView(Set<Rule> rules) { Map<TheA, Map<TheB, List<Rule>>> value = rules.stream().collect(groupingBy(Rule::getA, groupingBy(Rule::getB))); return value; } I want the types of createView to type

Creating nested maps with Java stream and groupingBy

女生的网名这么多〃 提交于 2021-01-29 11:57:09
问题 Given the following code: private enum TheA { AA, } private class TheB { String b; } private enum TheC { CC, } private class Rule { TheA a; TheB b; TheC c; public TheA getA() { return a; } public TheB getB() { return b; } public TheC getC() { return c; } } private Map<TheA, Map<TheB, Set<TheC>>> createView(Set<Rule> rules) { Map<TheA, Map<TheB, List<Rule>>> value = rules.stream().collect(groupingBy(Rule::getA, groupingBy(Rule::getB))); return value; } I want the types of createView to type