-
-
Save eliezio/c2c3f3c0d5ce4707e24e389366016d23 to your computer and use it in GitHub Desktop.
Test1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import lombok.SneakyThrows; | |
import lombok.Value; | |
import org.apache.commons.beanutils.PropertyUtils; | |
import sun.reflect.CallerSensitive; | |
import java.util.*; | |
import java.util.function.Function; | |
import java.util.stream.*; | |
public class TestBatch { | |
public static void main(String args[]) { | |
new TestBatch().newMain(); | |
} | |
private void newMain() { | |
List<User> pojos = new ArrayList<>(); | |
pojos.add(new User(1, "U1", "M", "E", new Bean(1, "A"))); | |
pojos.add(new User(2, "U1", "M", "S", new Bean(2, "B"))); | |
pojos.add(new User(3, "U2", "M", "N", new Bean(3, "C"))); | |
pojos.add(new User(4, "U3", "M", "E", new Bean(1, "A"))); | |
pojos.add(new User(5, "U4", "F", "S", new Bean(2, "B"))); | |
pojos.add(new User(6, "U5", "F", "E", new Bean(3, "C"))); | |
pojos.add(new User(7, "U5", "F", "N", new Bean(1, "A"))); | |
pojos.add(new User(8, "U6", "F", "S", new Bean(2, "B"))); | |
pojos.add(new User(9, "U7", "F", "E", new Bean(3, "C"))); | |
pojos.add(new User(10, "U8", "M", "N", new Bean(1, "A"))); | |
pojos.add(new User(11, "U9", "M", "E", new Bean(2, "B"))); | |
pojos.add(new User(12, "U10", "M", "S", new Bean(3, "C"))); | |
pojos.add(new User(13, "U11", "M", "E", new Bean(1, "A"))); | |
pojos.add(new User(14, "U12", "F", "N", new Bean(2, "B"))); | |
pojos.add(new User(15, "U13", "F", "S", new Bean(3, "C"))); | |
Set<Map.Entry<ArrayList<String>, List<User>>> result = | |
pojos.stream().collect(Collectors.groupingBy(item -> new ArrayList<>(Arrays.asList(item.getSex(), | |
item.getComp())))).entrySet(); | |
Map<String, List<User>> result2 = partition1(pojos, Arrays.asList(User::getSex, User::getComp)); | |
System.out.println("result2 = " + result2); | |
List<String> propertyNames = Arrays.asList("sex", "bean.intProperty", "bean.stringProperty"); | |
Map<String, List<User>> result3 = partition2(pojos, propertyNames); | |
System.out.println("result3 = " + result3); | |
} | |
static <T> Map<String, List<T>> partition1(Collection<T> col, List<Function<T, Object>> getters) { | |
return col.stream() | |
.collect(Collectors.groupingBy(item -> getters.stream() | |
.map(getter -> getter.apply(item).toString()) | |
.collect(Collectors.joining(", ", "[", "]")))); | |
} | |
static <T> Map<String, List<T>> partition2(Collection<T> col, List<String> propertyNames) { | |
return col.stream() | |
.collect(Collectors.groupingBy(item -> propertyNames.stream() | |
.map(name -> propertyOf(item, name).toString()) | |
.collect(Collectors.joining(", ", "[", "]")))); | |
} | |
@SneakyThrows | |
static Object propertyOf(Object bean, String name) { | |
return PropertyUtils.getNestedProperty(bean, name); | |
} | |
@Value | |
public static class User { | |
private int id; | |
private String name; | |
private String sex; | |
private String comp; | |
private Bean bean; | |
} | |
@Value | |
public static class Bean { | |
private int intProperty; | |
private String stringProperty; | |
private RefreshType refreshType; | |
} | |
@Value | |
public static class Id { | |
private String uuid; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment