JavaDeveloper
JavaDeveloper

Reputation: 5660

How to convert a list into a set(avoid duplication) in Java 8?

Here is the situation: We have a Student object which contains courseId as one of its fields. I want to return true if there is any student with course in history.

List<Student> students = new ArrayList();
students.stream.anyMap(s -> getFromDataBase(s.getCourseId()).name == "History");

Now, there is an optimization to prevent DB calls. Since many students can have the same courseId, it makes sense to filter out redundant courseId.

Eg:

List<Student> students = new ArrayList();
Set<Id> course = new HashSet<>();
for (Student s : students) {
    course.add(s.getCourseId());  
}

course.stream.anyMap(s -> getFromDataBase(s.getCourseId()).name == "History");

Now is there some way in Java-8, which I can use so that I don't need to do the following stuff:

  Set<Id> course = new HashSet<>();
    for (Student s : students) {
        course.add(s.getCourseId());  
    }

Upvotes: 3

Views: 96

Answers (3)

Jagannath
Jagannath

Reputation: 4025

Is getFromDataBase() a DB call ? Then why not change your DB call to take the courseId as parameter to the DB call (stored procedure?) and return the students with the course in question.

This way, we don't need to worry about the distinct transformations you are looking for.

If you still want to convert List<Student> to Set<Id> then

List<Student> students = ....

Set<Id> studentsId = new HashSet<>(students.stream()
                                           .map(s -> s.courseId)
                                           .Collectors.toList());

Upvotes: 1

Grzegorz Piwowarek
Grzegorz Piwowarek

Reputation: 13773

If you simply want to avoid duplication in a Java 8 idiom, you can utilize distinct() method:

students.stream()
  .map(s -> s.getCourseId())
  .distinct()
  // .. whatever

Upvotes: 2

Jacob G.
Jacob G.

Reputation: 29680

Note: Remember to compare Strings with String#equals and not ==.

With Java 8, you don't even need to use a Set, as there exists distinct Streams:

List<Student> students = ...;

students.stream()
        .map(Student::getCourseId)
        .distinct()
        .map(this::getFromDataBase)
        .map(Database::getName)
        .anyMatch(s -> s.equals("History"));

Keep in mind that you have not provided the names of your classes, so I don't know how to map a database entry to its name, so you'll need to modify that.

Upvotes: 3

Related Questions