in Education by
given I have a list of items that all inherit from the same base class: class Item { protected String name; public Item(String name) { this.name = name; } getName() { return name; } } class ItemA extends Item {...} class ItemB extends Item {...} List itemList = Arrays.asList(new ItemA("itemA"), new ItemB("itemB")); In my case I have no control over how these classes are implemented but I need to separate the list into two different lists containing the name of the respective element. Here is the first try at a solution that contains lots of if and instanceof statements: List itemAList = new ArrayList<>(); List itemBList = new ArrayList<>(); itemList.forEach(item -> { if(item instanceof ItemA) { itemAList.add(item.getName()); } else if(item instanceof ItemB) { itemBList.add(item.getName()); } }); So this works but I gave it some thought on how to avoid the if statements. Since I'm using Java 8 I can do this: List itemAList = itemList.stream() .filter(ItemA.class::isInstance) .map(item -> item.getName()) .collect(Collectors.toList()); List itemBList = itemList.stream() .filter(ItemB.class::isInstance) .map(item -> item.getName()) .collect(Collectors.toList()); This works as well but it means I have to process the list two times. As I said I have no bearing on the implementation of the Item classes so what would be the best way to implement such a behavior? Greetings [edit:] Thank you for all the responses. I've learned something new today. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can group the content based on the class of the elements : Map> grouped = itemList.stream() .collect(Collectors.groupingBy(Item::getClass, Collectors.mapping(Item::getName, Collectors.toList()))); and access it as: List itemAList = grouped.get(ItemA.class);

Related questions

0 votes
    given I have a list of items that all inherit from the same base class: class Item { protected ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    given I have a list of items that all inherit from the same base class: class Item { protected ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    given I have a list of items that all inherit from the same base class: class Item { protected ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I want to optimize this code instead of using td(String.valueof(dataset.get())) mutliple times. I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    . Accept 8 digit number. Separate 8 digits into 4 parts. Display appropriate message in case of number with less ... again as abcd Select the correct answer from above options...
asked Nov 26, 2021 in Education by JackTerrance
0 votes
    How will you get the sum of all numbers present in a list using Java 8 in Java8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    How will you get the highest number present in a list using Java 8...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    How will you sort a list of string using Java 8 lambda expression?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    Plz write a program for the following pattern in Java class 10 ICSE 2 6 12 20 30 42 4 6 8 10 12 2 2 2 ... spam. Answer only if you know Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    Write a JAVA program to find the area and perimeter of a square and rectangle. Store side in s , length as ... USE ANY INPUT METHOD Select the correct answer from above options...
asked Dec 22, 2021 in Education by JackTerrance
0 votes
    Write a JAVA program to find the area and perimeter of a square and rectangle. Store side in s , length ... results in separate lines. Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    I wanted to find a way to extract from the url the classes to be added to the body. I made some ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I wanted to find a way to extract from the url the classes to be added to the body. I made some ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Write a program to input name and percentage of 20 students of class X in two separate one dimensional arrays. ... of the class. Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    What are the two types of Streams offered by java 8? (a) sequential and parallel (b) sequential and random ... & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
...