Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/step05_lamda_and_stream/lamda/Calculable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package step05_lamda_and_stream.lamda;

@FunctionalInterface
public interface Calculable {
//추상메소드
void calculate(int x, int y);
}
23 changes: 23 additions & 0 deletions src/step05_lamda_and_stream/lamda/LamdaEx.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package step05_lamda_and_stream.lamda;

public class LamdaEx {
public static void main(String[] args) {
action((x,y) -> {
int result = x+y;
System.out.println("result: "+result);
});

action((x, y)-> {
int result = x - y;
System.out.println("result: "+result);
});
}

public static void action(Calculable calculable){
//데이터
int x = 10;
int y = 4;
//데이터 처리
calculable.calculate(x, y);
}
}
31 changes: 31 additions & 0 deletions src/step05_lamda_and_stream/stream/FilteringExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package step05_lamda_and_stream.stream;

import java.util.*;

public class FilteringExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("이규현");
list.add("이규현"); //중복
list.add("김규현2");
list.add("김규현3");
list.add("이규현4");

list.stream()
.distinct()
.forEach(name -> System.out.println(name));
System.out.println();

//김으로 시작하는 요소 필터링
list.stream()
.filter(name -> name.startsWith("김"))
.forEach(name -> System.out.println(name));
System.out.println();

//중복요소 먼저 제거 후 -> 필터링
list.stream()
.distinct()
.filter(name -> name.startsWith("김"))
.forEach(name -> System.out.println(name));
}
}
27 changes: 27 additions & 0 deletions src/step05_lamda_and_stream/stream/SortedExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package step05_lamda_and_stream.stream;

import java.util.*;
public class SortedExample {
public static void main(String[] args) {
List<Student> studentList = new ArrayList<>();

studentList.add(new Student("이규현1", 100));
studentList.add(new Student("이규현2", 50));
studentList.add(new Student("이규현3", 80));

//오름차순
studentList.stream()
//231
.sorted((s1, s2) -> Integer.compare(s1.getScore(), s2.getScore()))
.forEach(student ->
System.out.println("이름: "+student.getName()+",점수: "+student.getScore()));
System.out.println();

//내림차순
studentList.stream()
//132
.sorted((s1, s2) -> Integer.compare(s2.getScore(), s1.getScore()))
.forEach(student ->
System.out.println("이름: "+student.getName()+",점수: "+student.getScore()));
}
}
18 changes: 18 additions & 0 deletions src/step05_lamda_and_stream/stream/StreamExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package step05_lamda_and_stream.stream;

import java.util.*;
import java.util.stream.Stream;

public class StreamExample {
public static void main(String[] args) {
//Set 컬렉션
Set<String> set = new HashSet<>();
set.add("이규현1");
set.add("이규현2");
set.add("이규현3");

//Stream으로 반복처리하기
Stream<String> stream = set.stream();
stream.forEach(name -> System.out.println(name));
}
}
14 changes: 14 additions & 0 deletions src/step05_lamda_and_stream/stream/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package step05_lamda_and_stream.stream;

public class Student {
private String name;
private int score;

public Student(String name, int score){
this.name = name;
this.score = score;
}

public String getName(){return name;};
public int getScore(){return score;}
}