같은 타입을 가진 List 병합하기
List<User> user1 = repository.getUsers(a);
List<User> user2 = repository.getUsers(b);
List<User> user3 = repository.getUsers(c);
List<User> mergedUsers = mergeLists(user1, user2, user3);
private static List<User> mergeLists(List<User>... lists) {
return Stream.of(lists)
.flatMap(List::stream)
.collect(Collectors.toList());
}
다른 타입을 가진 List 병합하기
List<K8sPodMetricVo> k8sPodMetricVos = gson.fromJson(k8sResourceItemJson, new TypeToken<ArrayList<K8sPodMetricVo>>() {
}.getType());
List<PodEntityVo> podEntityVos = gson.fromJson(podEntityJson, new TypeToken<ArrayList<PodEntityVo>>() {
}.getType());
List<NodeEntityVo> nodeEntityVos = gson.fromJson(nodeEntityJson, new TypeToken<ArrayList<NodeEntityVo>>() {
}.getType());
List<PodMergeDataVo> podMergeDataList = k8sPodMetricVos.stream()
.flatMap(K8sPodMetricVo -> podEntityVos.stream()
.filter(PodEntityVo -> K8sPodMetricVo.getMetadata().getName().equals(PodEntityVo.getPodName()))
.map(PodEntityVo -> new PodMergeDataVo(K8sPodMetricVo, PodEntityVo, null)))
.flatMap(PodMergeDataVo -> nodeEntityVos.stream()
.filter(NodeEntityVo -> PodMergeDataVo.getPodEntity().getNodeName().equals(NodeEntityVo.getNodeName()))
.map(NodeEntityVo -> new PodMergeDataVo(PodMergeDataVo.getK8sPodMetric(), PodMergeDataVo.getPodEntity(), NodeEntityVo))
)
.collect(Collectors.toList());