0

I need to get unique elements in list based on job attribute - name - of inner list. So from below example I would like to have in result list only person/person3 (we remove person2 as it has the same job's name). I just want to have unique elements in list based on job's name.

 Person person = new Person("andri", "pik", Collections.singletonList(new Job("piotzr", 12)));
        Person person2 = new Person("kotak", "zik", Collections.singletonList(new Job("piotzr", 112)));
        Person person3 = new Person("lame", "sri", Collections.singletonList(new Job("piotra", 12)));

    public class Person {
            String name;
            String surname;
            List<Job> job;
    
    }

public class Job {

    String name;
    int pension;
}

Another example: I have 3 people in list, 2 from them have the same job's names. So i want to delete second person as it might be duplicated and in result list I will have just 2 people

I have found something like:

 private <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

but it refers only to attribute of iterated list, I do not know how to get to inner and based on it collect elements of upper list.

1
  • 1
    what if one person works at multiple jobs, what is considered unique entry? Lets say person1 works at job1 and job2, person2 at job1 only, person3 at job3 and job2. What output do you expect in this case? Feb 18, 2021 at 9:53

2 Answers 2

1

Using streams you can do that as below

ArrayList<Person> persons = new ArrayList<>();
String requiredJobName = "";
List<Person> filteredByJob = persons.stream()
                .filter(person -> hasJobWithName(person.job, requiredJobName))
                .collect(Collectors.toList());


public boolean hasJobWithName(List<Job> jobs, String name){
    for(Job job : jobs){
        if(job.name.equals(name)){
            return true;
        }
    }
    return false;
}
1
  • Edited again my question.
    – traki111
    Feb 18, 2021 at 9:32
0

You can use anyMatch() in the filter:

Person output = list.stream().filter(p -> p.getJob().stream().anyMatch(j -> j.getName().equals("piotra")))
        .findFirst().get();
8
  • I do not know what elements will be in this list I cannot do something like .equals("piotra")
    – traki111
    Feb 18, 2021 at 9:20
  • @traki111 '...filter elements in list based on one job name...' Can you give a concrete example, what will you use for searching?
    – Hülya
    Feb 18, 2021 at 9:23
  • Edited my question. Hope it is more clear now. I don't have any required job name. I just want to have unique elements in list based on job's name.
    – traki111
    Feb 18, 2021 at 9:27
  • I think he wants group Person with same job having names together, not able to understand from question.
    – Rajan Kali
    Feb 18, 2021 at 9:27
  • 1
    @traki111 So you wanna compare two lists? Should they have exactly same elements to remove one? Or eg. what to do with {"jobA", "jobB", "jobC"} and {"jobA", "jobB", "jobD"} job lists?
    – Hülya
    Feb 18, 2021 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.