Skip to content

Instantly share code, notes, and snippets.

@AndersonChoi
Created September 28, 2017 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndersonChoi/c75537738569b81c1104fa1eb83d34bf to your computer and use it in GitHub Desktop.
Save AndersonChoi/c75537738569b81c1104fa1eb83d34bf to your computer and use it in GitHub Desktop.
java 8 stream으로 join을 걸어서 추출하기
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Main{
public static void main(String []args){
System.out.println("Hello World");
List<Person> persons = new ArrayList();
List<Student> students = new ArrayList();
persons.add(new Person(1,"원영"));
persons.add(new Person(2,"two"));
persons.add(new Person(3,"one"));
persons.add(new Person(4,"apple"));
persons.add(new Person(5,"mod"));
persons.add(new Person(6,"hell"));
persons.add(new Person(7,"hello"));
students.add(new Student(10,"홍길동"));
students.add(new Student(20,"apple"));
students.add(new Student(30,"one"));
students.add(new Student(40,"two"));
students.add(new Student(50,"second"));
students.add(new Student(60,"sorry"));
students.add(new Student(70,"hello"));
List<Person> result = persons.stream()
.filter(e -> students.stream().map(Student::getStudentName).anyMatch(name -> name.equals(e.getPersonName())))
.collect(Collectors.toList());
for(Person p : result){
System.out.println(p.getSid()+" "+p.getPersonName());
}
}
}
class Student{
int id;
String studentName;
Student(int id, String studentName){
this.id = id;
this.studentName = studentName;
}
int getId(){return this.id;}
String getStudentName(){return this.studentName;}
}
class Person{
int id;
String personName;
Person(int id, String personName){
this.id = id;
this.personName = personName;
}
int getSid(){return this.id;}
String getPersonName(){return this.personName;}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment