공부한 내용을 정리하는 글이라 틀린 내용이 있을 수 있습니다!
오늘 학습한 Functional, Stream 대한 정리.
public class FuncTest {
public static void test() {
// PrintTest t = new PrintTest() {
// @Override
// public void print() {
// System.out.println("인터페이스로 클래스를 바로 생성한다.");
// }
// };
// t.print();
action(new FuncTest() :: print);
}
// action((name)->{
// System.out.println(name + "람다 테스트");
// });
// }
private static void action(PrintTest printTest) {
printTest.print("강아지");
}
private static void action1(PrintTest printTest) {
printTest.print("강아지");
}
private void print(String name) {
System.out.println(name + "람다 테스트");
}
}
@FunctionalInterface
public interface PrintTest {
void print(String name);
public default int test() { //default는 쓸수있음
return 1;
}
}
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamTest {
public static void test() {
Set<String> set = new HashSet<String>();
set.add("강아지");
set.add("고양이");
set.add("말");
set.add("소");
set.add("돼지");
set.add("호랑이");
set.add("토끼");
set.add("사자");
set.add("하마");
Stream<String> stream = set.stream();
stream.forEach(item->System.out.println(item));
List<String> list = new ArrayList<String>();
list.add("강아지");
list.add("고양이");
list.add("말");
list.add("소");
list.add("돼지");
list.add("호랑이");
list.add("토끼");
list.add("사자");
list.add("하마");
Stream<String> stream1 = list.stream();
stream1.forEach(item->System.out.println(item));
// for(String temp : arr) {}
// set.toArray();
Set<Animal> animals = new HashSet<Animal>();
students.add(new animal("강아지", 85));
students.add(new animal("고양이", 98)); // 주관식 틀렷나?
students.add(new animal("말", 90));
students.add(new animal("소", 80));
students.add(new animal("돼지", 90));
students.add(new animal("호랑이", 70));
students.add(new animal("토끼", 75));
students.add(new animal("사자", 100));
students.add(new animal("하마", 80));
Set<Animal> animals2 = new HashSet<Animal>();
IntStream scores = stream2.mapToInt(item->item.getScore());//.map(i->-i).sorted().map(i->-i);
// stream2.forEach(item->System.out.println(item));
// stream2.forEach(item->System.out.println(item.getName() + " : " + item.getScore()));
//동물이름 : 점수 => 강아지 : 85
double avg = scores.average().getAsDouble();
// scores.forEach(item->System.out.println(item));
System.out.println("avg : " + avg);
IntStream scoresDis = animals.stream().mapToInt(item->item.getScore()).distinct();
// int[] temp = scoresDis.toArray();
scoresDis.forEach(item->System.out.print(item + ", "));
System.out.println();
Stream<Animal> stream3 = animals.stream().sorted(Comparator.reverseOrder());
stream3.forEach(item->System.out.println(item));
Stream<Animal> stream4 = animals.stream().filter(item->{
return item.getScore() > avg;
});
stream4.forEach(item->System.out.print(item));
}
}
public class Animal implements Comparable<Animal>{
private String name;
private int score;
Student(String name, int score){
this.name = name;
this.score = score;
}
public String getName() {return name;}
public int getScore() {return score;}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return name + " : " + score;
}
@Override
public int compareTo(Animal s) {
return score - s.getScore();
}
}
Main코드
import functional.FuncTest;
import stream.StreamTest;
public class Main {
public static void main(String[] args) {
FuncTest.test();
StreamTest.test();
}
}
Lambda
Stream