import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.util.Assert;
public class Java8Test {
class Member {
int id;
String name;
public Member(int id, String name) {
this.id = id;
this.name = name;
}
}
@Test
public void test1() {
Member member1 = new Member(1, "samuel");
Member member2 = new Member(2, "keans");
Member member3 = new Member(3, "xy");
List<Member> members = Arrays.asList(member1, member2, member3);
Map<Boolean, List<Member>> results = members.stream().collect(Collectors.partitioningBy(m -> StringUtils.startsWith(m.name, "x")));
results.forEach((k, v) -> System.out.println("key:" + k + ", value:" + v.stream().map(member -> member.name).collect(Collectors.joining(", "))));
Assert.isTrue(results.get(true).get(0) == member3);
Assert.isTrue(results.get(false).get(0) == member1);
Assert.isTrue(results.get(false).get(1) == member2);
}
}
결과는 다음과 같다.
key:false, value:samuel, keans
key:true, value:xy
단순히 Collectors.partitionBy 대신 Collectors.groupingBy로 사용할 수도 있다.
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.util.Assert;
public class Java8Test {
class Member {
int id;
String name;
public Member(int id, String name) {
this.id = id;
this.name = name;
}
}
@Test
public void test1() {
Member member1 = new Member(1, "samuel");
Member member2 = new Member(2, "keans");
Member member3 = new Member(3, "xy");
List<Member> members = Arrays.asList(member1, member2, member3);
Map<Boolean, List<Member>> results = members.stream().collect(Collectors.groupingBy(m -> StringUtils.startsWith(m.name, "x")));
results.forEach((k, v) -> System.out.println("key:" + k + ", value:" + v.stream().map(member -> member.name).collect(Collectors.joining(", "))));
Assert.isTrue(results.get(true).get(0) == member3);
Assert.isTrue(results.get(false).get(0) == member1);
Assert.isTrue(results.get(false).get(1) == member2);
}
}
groupingBy메소드에서는 Aggregation 기능도 제공한다.
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.junit.Test;
public class Java8Test {
class Member {
int id;
String name;
public Member(int id, String name) {
this.id = id;
this.name = name;
}
public String toString() {
return id + ", " + name;
}
}
@Test
public void test1() {
Member member1 = new Member(1, "samuel");
Member member2 = new Member(2, "keans");
Member member3 = new Member(3, "xy");
Member member4 = new Member(4, "keans"); // same name, but not same person
List<Member> members = Arrays.asList(member1, member2, member3, member4);
Map<Member, Long> results = members.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
results.forEach((k, v) -> System.out.println("key:" + k + ", value:" + v));
System.out.println("");
Map<String, Long> otherResults = members.stream().collect(Collectors.groupingBy(m-> m.name, Collectors.counting()));
otherResults.forEach((k, v) -> System.out.println("key:" + k + ", value:" + v));
}
}
결과는 다음과 같다.
key:2, keans, value:1
key:4, keans, value:1
key:1, samuel, value:1
key:3, xy, value:1
key:xy, value:1
key:samuel, value:1
key:keans, value:2
difference 예제이다. map의 요소 중 특정 key를 제외하고 싶을 때 사용할 수 있는 예제이다.
먼저 groupingBy 또는 parittioningBy를 사용하지 않는 예제이다.
Map<Integer, Object> maps = Maps.newHashMap();
maps.put(1, "samuel");
maps.put(2, "jackson");
maps.put(3, "ice");
maps.put(4, "xxx");
List<Integer> excludeList = Arrays.asList(4);
List<Integer> keys = maps.entrySet().stream().map(k -> k.getKey()).collect(Collectors.toList());
Map<Integer, Object> map = (Map<Integer, Object>) CollectionUtils.subtract(keys, excludeList).stream().collect(Collectors.toMap(p->p, p->maps.get(p)));
System.out.println(map);
결과는 다음과 같다.
{1=samuel, 2=jackson, 3=ice}
Map<Integer, Object> maps = Maps.newHashMap();
maps.put(1, "samuel");
maps.put(2, "jackson");
maps.put(3, "ice");
maps.put(4, "xxx");
List<Integer> excludeList = Arrays.asList(4);
Map<Boolean, Map<Object, Object>> map = maps.entrySet().stream().collect(
Collectors.partitioningBy(p -> !excludeList.contains(p.getKey()), Collectors.toMap(p -> p.getKey(), p -> p.getValue()))
);
System.out.println(map);
System.out.println(map.get(true));
결과는 다음과 같다.
{false={4=xxx}, true={1=samuel, 2=jackson, 3=ice}}
{1=samuel, 2=jackson, 3=ice}
'java core' 카테고리의 다른 글
java visualvm의 self time 설명 (0) | 2018.01.10 |
---|---|
[java] Exception in thread "main" java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class 해결하기 (0) | 2017.04.27 |
[java8] 람다 사용시 스칼라와 자바의 블럭 차이 (0) | 2017.04.21 |
java의 volatile (0) | 2017.04.17 |
하나의 인터페이스를 가진 interface에 @FunctionalInterface를 사용해 java7에서 java8 전환 (0) | 2017.04.04 |