Stream<T> distinct(); 复制代码 1.1 对于 String 列表的去重 因为String 类已经覆写了 equals() 和 hashCode() 方法,所以可以去重成功。 @Test public void listDistinctByStreamDistinct() { // 1. 对于 String 列表去重 List<String> stringList = new ArrayList<String>() {{ add("A"); add("A");...
public void distinctByProperty() throws JsonProcessingException { // 这里第二种方法我们通过过滤来实现根据对象某个属性去重 ObjectMapper objectMapper = new ObjectMapper(); List<Student> studentList = getStudentList(); System.out.print("去重前 :"); System.out.println(objectMapper.writeValueAsString(st...
publicvoiddistinctByProperty1()throwsJsonProcessingException { // 这里第一种方法我们通过新创建一个只有不同元素列表来实现根据对象某个属性去重 ObjectMapper objectMapper =new ObjectMapper(); List<Student> studentList = getStudentList(); out.print("去重前 :"); out.println(objectMapper.writeValueAsString...
System.out.println(newGson().toJson(studentDistinct2List)); 工具类 1 2 3 4 5 6 7 8 9 10 11 12 publicclassStreamUtil { /** * https://stackoverflow.com/questions/23699371/java-8-distinct-by-property * @param keyExtractor * @param <T> * @return */ publicstatic<T> Predicate<T> d...
System.out.println(new Gson().toJson(studentDistinct2List)); 工具类 public class StreamUtil { /** *https://stackoverflow.com/questions/23699371/java-8-distinct-by-property * @param keyExtractor * @param <T> * @return */ public static <T> Predicate<T> distinctByKey(Function<? super T...
importjava.util.ArrayList;importjava.util.HashSet;importjava.util.List;importjava.util.Set;publicclassListDistinctByProperty{publicstaticvoidmain(String[]args){// 原始的客户信息列表List<Customer>customerList=newArrayList<>();customerList.add(newCustomer("1234567890","张三"));customerList.add(newCustome...
System.out.println(new Gson().toJson(studentDistinct2List)); 工具类 public class StreamUtil { /** * https://stackoverflow.com/questions/23699371/java-8-distinct-by-property * @param keyExtractor * @param * @return */ public staticPredicatedistinctByKey(Function super T, ?> keyExtractor) {...
import java.util.List; import java.util.stream.Collectors; public class DistinctSimpleDemo { public static void main(String[] args) { Listlist = Arrays.asList("AA", "BB", "CC", "BB", "CC", "AA", "AA"); long l = list.stream().distinct().count(); ...
@Test public void distinctByProperty1() throws JsonProcessingException { // 这里第一种方法我们通过新创建一个只有不同元素列表来实现根据对象某个属性去重 ObjectMapper objectMapper = new ObjectMapper(); List<Student> studentList = getStudentList(); out.print("去重前 :"); out.println(objectMapper.wri...
list.stream().filter(distinctByKey(b -> b.getName())); distinctByKey()方法返回一个使用ConcurrentHashMap 来维护先前所见状态的 Predicate 实例,如下是一个完整的使用对象属性来进行去重的示例。 DistinctByProperty.java packagecom.concretepage;importjava.util.ArrayList;importjava.util.List;importjava.util...