0

I am writing a code to loop through multiple Lists and create another list with merging of unique values from both the lists using Java 8 lambda expressions.

Model class:

class ServiceMap{
    Integer serviceMapId;
    Integer seviceId;
}

Code logic:

List<ServiceMap> listA = getServiceMaps();//Will get from database
List<Integer> listB = Arrays.asList(1, 10, 9);//Will get from client
List<ServiceMap> listC = new ArrayList<>();//Building it merging of both lists above

listA.stream().forEach(e -> {
    if (listB.parallelStream().noneMatch(x -> x == e.getServiceId())) {
        listC.add(new ServiceMap(e.getServiceId()));
        return;
    }

    listB.stream().forEach(x -> {
        if (listC.stream().anyMatch(e2->e2.getServiceId() == x)) {
            return;
        }
        if (x == e.getServiceId()) {
            listC.add(new ServiceMap(e.getServiceId()));
        } else {
            listC.add(new ServiceMap(x));
        }
    });

});
listC.stream().forEach(x -> System.out.println(x));

Is it efficient way writing code using java lambda expressions?

10
  • Stream.of(listA, listB).map(Collection::stream).reduce(Stream.empty(), Stream::concat).distinct().collect(Collectors.toList()). Your solution is certainly not correct. Commented Sep 25, 2018 at 19:41
  • 1
    isn't this better ? Set<Integer> set = new HashSet<>(listA); listB.stream().filter(i -> !set.contains(i)).forEach(set::add); Commented Sep 25, 2018 at 19:49
  • 1
    @BoristheSpider this has horrible performance though... Commented Sep 25, 2018 at 19:49
  • 2
    @BoristheSpider I actually misread your code, my bad. A minor improvement for you version could be Stream.of(listA, listB) .map(List::stream) .reduce(Stream::concat) .orElse(Stream.empty()) .distinct() .collect(Collectors.toList()); but, still, why not Stream.concat(listA, listB) directly... Commented Sep 25, 2018 at 20:08
  • 1
    @Krish you posted code, received comments and answers that we took time over - reading and understanding your code and posting. Changing your code after the fact is not acceptable. Please roll back your edit. Commented Sep 25, 2018 at 20:08

2 Answers 2

3

You could stream each list, apply distinct to them, and collect:

List<Integer> result = 
    Stream.concat(listA.stream(), listB.stream())
          .distinct()
          .collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

3

You should use also like this ;

Stream<Integer> streamOfServiceMapIds = listA.stream().map(ServiceMap::getSeviceId);
List<ServiceMap> collectedList = Stream.concat(streamOfServiceMapIds, listB.stream())
        .distinct()
        .map(ServiceMap::new)
        .collect(Collectors.toList());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.