-1

Which is the best way in Java 8 to pick&collect Lists<T> from a List of Objects (Records) which has several Lists with different Types {List<Type1>, List<Type2>, List<Type3>, ..}?

Type1, Type2, Type3 etc. are not related to each other. T = Type1, Type2, Type3 ...

List<Records> allRecords;

class Records {
    List<Type1> listOfType1; // can be empty or null
    List<Type2> listOfType2; // can be empty or null
    List<Type3> listOfType3; // can be empty or null
}

List<T> getAllOccurrencesForType(T t, List<Records> allRecords) {
    return ?all occurrences of List<t> from all Records collected to one List? 
}
5
  • If your code is like that there is exactly one occurence of each type and you can use record.getListOfType1(). Maybe you add these lists dynamically to the records class? Commented Oct 24, 2017 at 10:13
  • 5
    What is the difference to stackoverflow.com/questions/46894007/… ? Commented Oct 24, 2017 at 10:14
  • I agree with @luk2302 - it looks like you are asking the same thing again. Maybe I am wrong - but then please explain why this second question is required and where it is different from what you asked before?! Commented Oct 24, 2017 at 10:39
  • Veselin Davidov, thanks for you comment. The list can have several Records and each Records has one List for each type. Commented Oct 24, 2017 at 10:52
  • @luk2302, @GhostCat: Thanks for your hint. I will check this ASAP. I asked a colleague of mine yesterday to try to find a solution for this task and he worked on my PC, while I was in meeting. I assume he used my account to post the same question. If so, I will flag this one as duplicated. Thanks again! Commented Oct 24, 2017 at 10:57

1 Answer 1

1

I believe passing a getter Function that returns the required List can work:

static <T> List<T> getAllOccurrencesForType(Function<Records,List<T>> getter, List<Records> allRecords) {
    return allRecords.stream()
                     .flatMap(r->getter.apply(r).stream())
                     .collect(Collectors.toList());
}

And then you call it with:

List<Type1> getAllOccurrencesForType (Records::getListOfType1,allRecords);

Here's a full example:

class Records {
    List<String> listOfType1;
    List<Integer> listOfType2;
    List<Double> listOfType3;
    public Records (List<String> l1, List<Integer> l2, List<Double> l3) {
      listOfType1 = l1;
      listOfType2 = l2;
      listOfType3 = l3;
    }
    public List<String> getListOfType1() {
      return listOfType1;
    }
    public List<Integer> getListOfType2(){
      return listOfType2;
    }
    public List<Double> getListOfType3(){
      return listOfType3;
    }
}

Some main method:

 List<Records> recs = new ArrayList<> ();
 recs.add (new Records (Arrays.asList ("a","b"), Arrays.asList (1,2), Arrays.asList (1.1,4.4)));
 recs.add (new Records (Arrays.asList ("c","d"), Arrays.asList (4,3), Arrays.asList (-3.3,135.3)));
 List<String> allStrings = getAllOccurrencesForType(Records::getListOfType1,recs);
 List<Integer> allIntegers = getAllOccurrencesForType(Records::getListOfType2,recs);
 List<Double> allDoubles = getAllOccurrencesForType(Records::getListOfType3,recs);
 System.out.println (allStrings);
 System.out.println (allIntegers);
 System.out.println (allDoubles);

output:

[a, b, c, d]
[1, 2, 4, 3]
[1.1, 4.4, -3.3, 135.3]
Sign up to request clarification or add additional context in comments.

10 Comments

Hi Eran, great answer, thanks a lot :-) I get the following compile error because of r->getter.apply(r) from the method getAllOccurrencesForType()! Cannot infer type argument(s) for <R> flatMap(Function<? super T,? extends Stream<? extends R>>)
@ThomasMuller The code I posted passes compilation and runs on Eclipse Neon. What IDE are you using?
Eran, I'm using eclipse Oxygen, Java EE IDE for Web Developers
@ThomasMuller Are you sure that you are using the exact code as in my answer? I find that Java 8 Error messages are often misleading. A simple typo can give you an error message that won't help you find it.
@Eran: Thanks for your time. I placed your method into the following class: public class Utility<T> { static <T> List<T> getAllOccurrencesForType(Function<Records,List<T>> getter, List<Records> allRecords) { return allRecords.stream().flatMap(r->getter.apply(r).stream()).collect(Collectors.toList()); } }
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.