Created
September 28, 2017 08:33
-
-
Save AndersonChoi/c75537738569b81c1104fa1eb83d34bf to your computer and use it in GitHub Desktop.
java 8 stream으로 join을 걸어서 추출하기
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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