@FunctionalInterface
public interface TriFunction<T , U , V , R> {
R get(T t, U u, V v);
}
class Apple{
int weight;
Color color;
String status;
int objectHashCode = this.hashCode();
public Apple() {
}
public Apple(int weight) {
this.weight = weight;
}
public Apple(int weight, Color color) {
this.weight = weight;
this.color = color;
}
public Apple(int weight, String status) {
this.weight = weight;
this.status = status;
}
public Apple(int weight, Color color, String status) {
this.weight = weight;
this.color = color;
this.status = status;
}
@Override
public String toString() {
return "Apple{" +
"weight=" + weight +
", color='" + color + '\'' +
", status='" + status + '\'' +
", objectHashCode=" + objectHashCode +
'}';
}
public enum Color {GREEN , RED , YELLOW}
}
class Main {
public static void main(String[] args) throws Exception {
run();
Supplier<Apple> supplier1 = Apple::new;
System.out.println(supplier1.get());
Supplier<Apple> supplier2 = () -> new Apple();
System.out.println(supplier2.get());
Function<Integer , Apple> function1 = Apple::new;
System.out.println(function1.apply(11));
List<Integer> weights = Arrays.asList(21 , 22 , 23 , 24 , 25);
List<Apple> apples = map(weights , function1);
apples.forEach(System.out::println);
BiFunction<Integer , Apple.Color, Apple> biFunction1 = Apple::new;
System.out.println(biFunction1.apply(31 , Apple.Color.GREEN));
BiFunction<Integer , String , Apple> biFunction2 = Apple::new;
System.out.println(biFunction2.apply(41 , "GOOD"));
TriFunction<Integer , Apple.Color , String , Apple> triFunction1 = Apple::new;
System.out.println(triFunction1.get(51 , Apple.Color.RED , "BAD"));
// Apple{weight=0, color='null', status='null', objectHashCode=1854731462}
// Apple{weight=0, color='null', status='null', objectHashCode=214126413}
// Apple{weight=11, color='null', status='null', objectHashCode=1867750575}
// Apple{weight=21, color='null', status='null', objectHashCode=2046562095}
// Apple{weight=22, color='null', status='null', objectHashCode=1342443276}
// Apple{weight=23, color='null', status='null', objectHashCode=769287236}
// Apple{weight=24, color='null', status='null', objectHashCode=1587487668}
// Apple{weight=25, color='null', status='null', objectHashCode=1199823423}
// Apple{weight=31, color='GREEN', status='null', objectHashCode=1896277646}
// Apple{weight=41, color='null', status='GOOD', objectHashCode=1702297201}
// Apple{weight=51, color='RED', status='BAD', objectHashCode=1296064247}
}
public static List<Apple> map(List<Integer> list , Function<Integer , Apple> f){
List<Apple> result = new ArrayList<>();
for(Integer i : list){
result.add(f.apply(i));
}
return result;
}
}
- โ ๊ฐ์ ๋๋ค , ๋ค๋ฅธ ํจ์ํ ์ธํฐํ์ด์ค
Comparator<Apple> c1 =
(Apple a1 , Apple a2) -> a1.getWeight().compareTo(a2.getWeight());
ToIntBiFunction<Apple , Apple> c2 =
(Apple a1 , Apple a2) -> a1.getWeight().compareTo(a2.getWeight());
BiFunction<Apple , Apple , Integer> c3 =
(Apple a1 , Apple a2) -> a1.getWeight().compareTo(a2.getWeight());
๋๋ค ์กฐํฉ Comparator , Predicate
public class ClassForStreamAPIPractice {
private Integer id;
private String title;
private boolean closed;
}
Comparator<ClassForStreamAPIPractice> idASC = (o1 , o2) -> o1.getId().compareTo(o2.getId());
List<ClassForStreamAPIPractice> list = new ArrayList<>();
list.add(new ClassForStreamAPIPractice(10 , "H" , false));
list.add(new ClassForStreamAPIPractice(10 , "A" , true));
list.add(new ClassForStreamAPIPractice(50 , "B" , true));
list.add(new ClassForStreamAPIPractice(20 , "G" , true));
list.add(new ClassForStreamAPIPractice(90 , "D" , false));
list.add(new ClassForStreamAPIPractice(80 , "E" , false));
list.add(new ClassForStreamAPIPractice(20 , "C" , false));
list.add(new ClassForStreamAPIPractice(50 , "F" , true));
list.add(new ClassForStreamAPIPractice(50 , "AA" , true));
list.add(new ClassForStreamAPIPractice(30 , "BB" , true));
list.add(new ClassForStreamAPIPractice(40 , "CC" , true));
list.add(new ClassForStreamAPIPractice(70 , "GG" , true));
list.add(new ClassForStreamAPIPractice(20 , "EE" , true));
System.out.println("id ๋ณ๋ก ์ ๋ ฌ");
list.sort(Comparator.comparing(ClassForStreamAPIPractice::getId));
list.forEach(System.out::println);
System.out.println("id ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ , id๊ฐ ๊ฐ๋ค๋ฉด title ์ค๋ฆ์ฐจ์ ์ ๋ ฌ");
list.sort(idASC.reversed().thenComparing(ClassForStreamAPIPractice::getTitle));
list.forEach(System.out::println);
System.out.println("title ๊ธธ์ด๊ฐ 2์ด์์ด๊ณ , ๋ง๊ฐ๋๊ฒ๋ค ํํฐ , id ๋ณ๋ก ์ ๋ ฌ");
Predicate<ClassForStreamAPIPractice> titleLength = (e -> e.getTitle().length() >= 2);
Predicate<ClassForStreamAPIPractice> titleLengthAndClose = titleLength.and(ClassForStreamAPIPractice::isClosed);
List<ClassForStreamAPIPractice> result1 = list.stream()
.filter(titleLengthAndClose)
.sorted(idASC)
.collect(Collectors.toList());
result1.forEach(System.out::println);
System.out.println("title ๊ธธ์ด๊ฐ 2๋ฏธ๋ง");
List<ClassForStreamAPIPractice> result2 = list.stream()
.filter(titleLength.negate())
.collect(Collectors.toList());
result2.forEach(System.out::println);
๋๋ค ํํ์ , ์ต๋ช
ํด๋์ค , ๋ด๋ถ ํด๋์ค ์ง์ญ ๋ณ์ ์ฐธ์กฐ
private static void run(){
int baseNumber = 10;
// ๋๋ค ํํ์ ์ง์ญ ๋ณ์ ์ฐธ์กฐ
IntConsumer printInt = (i) -> {
System.out.println("๋๋ค ํํ์ ์ง์ญ ๋ณ์ ์ฐธ์กฐ : " + (i + baseNumber));
};
printInt.accept(10);
// ์ต๋ช
ํด๋์ค์์ ์ง์ญ ๋ณ์ ์ฐธ์กฐ
Consumer<Integer> test2 = new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
System.out.println("์ต๋ช
ํด๋์ค์์ ์ง์ญ ๋ณ์ ์ฐธ์กฐ : " + (integer + baseNumber));
}
};
test2.accept(10);
// ๋ด๋ถ ํด๋์ค์์ ์ง์ญ ๋ณ์ ์ฐธ์กฐ
class LocalClass{
void printBaseNumber(int value){
System.out.println("๋ด๋ถ(๋ก์ปฌ) ํด๋์ค์์ ์ง์ญ ๋ณ์ ์ฐธ์กฐ : " + (value + baseNumber));
}
}
LocalClass test3 = new LocalClass();
test3.printBaseNumber(10);
}
private static void run(){
int baseNumber = 10;
// ๋๋ค ํํ์ ์ง์ญ ๋ณ์ ์ฐธ์กฐ
IntConsumer printInt = (i) -> {
int baseNumber = 90;
System.out.println("๋๋ค ํํ์ ์ง์ญ ๋ณ์ ์ฐธ์กฐ : " + (i + baseNumber));
};
printInt.accept(10);
// ์ต๋ช
ํด๋์ค์์ ์ง์ญ ๋ณ์ ์ฐธ์กฐ
Consumer<Integer> test2 = new Consumer<Integer>() {
@Override
public void accept(Integer integer) {
int baseNumber = 90;
System.out.println("์ต๋ช
ํด๋์ค์์ ์ง์ญ ๋ณ์ ์ฐธ์กฐ : " + (integer + baseNumber));
}
};
test2.accept(10);
// ๋ด๋ถ ํด๋์ค์์ ์ง์ญ ๋ณ์ ์ฐธ์กฐ
class LocalClass{
void printBaseNumber(int value){
int baseNumber = 90;
System.out.println("๋ด๋ถ(๋ก์ปฌ) ํด๋์ค์์ ์ง์ญ ๋ณ์ ์ฐธ์กฐ : " + (value + baseNumber));
}
}
LocalClass test3 = new LocalClass();
test3.printBaseNumber(10);
}
List<List<String>> persons = Arrays.asList(
Arrays.asList("๊นํ๋ก,์ถ๊ตฌ:๋๊ตฌ:์ผ๊ตฌ,๊ตฌ๊ธฐ์ข
๋ชฉ ์ข์์".split(",")),
Arrays.asList("์ ํ๋ก,๊ฐ๋ฐ:๋น๊ตฌ:์กฑ๊ตฌ,๊ฐ๋ฐํ๋๋ฐ ๋ฐ๊ธด ์ซ์ด".split(",")),
Arrays.asList("์๋ชฌ๋,ํผ์๋
ธ, ์ฃ ๋ฅด๋๊ฐ ์ข์์ ์ข์์ข์๋๋ฌด์ข์".split(",")),
Arrays.asList("์ฃ ๋ฅด๋,์คํฌ์ธ ๋์ค:๊ฐ๋ฐ,๊ฐ๋ฐํ๋ ์ฃ ๋ฅด๋".split(","))
);
Map<String, Integer> result = new HashMap<>();
persons.stream()
.flatMap(member -> Arrays.stream(member.get(1).split(":")))
.forEach(hobby -> result.merge(hobby , 1 , (oldValue , newValue) -> ++oldValue));
result.entrySet().forEach(entry-> System.out.println(entry.getKey() + " " + entry.getValue()));
// ์คํฌ์ธ ๋์ค 1
// ์กฑ๊ตฌ 1
// ๋น๊ตฌ 1
// ๊ฐ๋ฐ 2
// ์ผ๊ตฌ 1
// ํผ์๋
ธ 1
// ๋๊ตฌ 1
// ์ถ๊ตฌ 1
default V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Objects.requireNonNull(value);
V oldValue = get(key);
V newValue = (oldValue == null) ? value :
remappingFunction.apply(oldValue, value);
if (newValue == null) {
remove(key);
} else {
put(key, newValue);
}
return newValue;
}
- ์ทจ๋ฏธ๋ณ ์ ์จ ์ฑ์ ๊ฐ๋ ๋ฉค๋ฒ ์
Map<String, Integer> result = new HashMap<>();
persons.stream()
.filter(member-> member.get(0).startsWith("์ "))
.flatMap(member -> Arrays.stream(member.get(1).split(":")))
.forEach(hobby -> result.merge(hobby, 1, (oldValue, newValue) -> ++oldValue));
result.entrySet().forEach(entry-> System.out.println(entry.getKey() + " " + entry.getValue()));
// ์กฑ๊ตฌ 1
// ๋น๊ตฌ 1
// ๊ฐ๋ฐ 1
- ์๊ฐ ๋ด์ฉ์ '์ข์'๊ฐ ๋ช ๋ฒ ๋ฑ์ฅํ๋์ง ๊ตฌํ๋ผ
final String word = "์ข์";
int result = persons.stream()
.map(member -> countFindString(member.get(2), word))
.reduce(0, Integer::sum);
System.out.println(word + " " + result);
private static int countFindString(String source , String target) {
int idx = source.indexOf(target);
if(idx == -1) {
return 0;
}
else {
return 1 + countFindString(source.substring(idx + 1) , target);
}
}
// ์ข์ 5