<> De duplication according to an object property value
1, Create extraction method private <T> Predicate<T> distinctByKey(Function<? super T, Object>
keyExtractor) { Map<Object, Boolean> concurrentHashMap = new ConcurrentHashMap<>
(); return t -> concurrentHashMap.putIfAbsent(keyExtractor.apply(t), Boolean.
TRUE) == null; } 2, filter duplicate removal List<TestCommodity> codeDistinctList =
testCommodityList.stream() .filter(distinctByKey(TestCommodity::getCode)) .
collect(Collectors.toList());
<> De duplication based on multiple attribute values
List<TestCommodity> cbList = testCommodityList .stream() .collect( Collectors.
collectingAndThen( Collectors.toCollection( () -> new TreeSet<>( Comparator.
comparing( tc -> tc.getCode() + ";" + tc.getBarCode()))), ArrayList::new));
<> Get the highest price object after grouping
Map<String, TestCommodity> maxPriceCommodityMap = testCommodityList .stream() .
collect( Collectors.groupingBy( TestCommodity::getCode, Collectors.
collectingAndThen( Collectors.maxBy( Comparator.comparingDouble(TestCommodity::
getPrice)),Optional::get)));
<> Basic use
private static List<User> list = new ArrayList<User>(); public static void main
(String[] args) { list = Arrays.asList( new User(1, "a", 10), new User(4, "d",
19), new User(5, "e", 13), new User(2, "b", 14), new User(3, "a", 10), new User(
6, "f", 16) ); long start = System.currentTimeMillis(); order(); println(String.
format(" time consuming [%s] millisecond ", (System.currentTimeMillis() - start))); } public static void
group() { Map<Integer, List<User>> collect = list.stream().collect(Collectors.
groupingBy(User::getAge)); System.out.println(collect); } /** * filter filter *
@param list */ public static void filterAge() { list.stream().filter(u -> u.
getAge() == 10).forEach(u -> println(u)); } /** * sorted sort */ public static void
stord() { list.stream().sorted(Comparator.comparing(u-> u.getAge())).forEach(u -
> println(u)); } /** * limit Method limits the maximum number of elements returned */ public static void limit() { list.
stream().limit(2).forEach(u -> println(u)); } /** * No more n Elements ,n Returns an empty stream if the number of elements is greater than the number that satisfy the condition
*/ public static void skip() { list.stream().skip(2).forEach(u -> println(u)); }
// Maximum minimum value public static void statistics() { Optional<User> min = list.stream().
min(Comparator.comparing(User::getUserId)); println(min); Optional<User> max =
list.stream().max(Comparator.comparing(User::getUserId)); println(max); } // Statistics
public static void summarizingInt(){ IntSummaryStatistics statistics = list.
stream().collect(Collectors.summarizingInt(User::getAge)); double average =
statistics.getAverage(); long count = statistics.getCount(); int max =
statistics.getMax(); int min = statistics.getMin(); long sum = statistics.getSum
(); println(average); println(count); println(min); println(sum); println(max);
} /** * turn set */ public static void toSet() { Set<User> collect = list.stream().
collect(Collectors.toSet()); Iterator<User> iterator = collect.iterator(); while
(iterator.hasNext()) { System.out.println(iterator.next().getUserId()); } } /**
* turn map */ public static void toMap() { Map<Integer, User> collect = list.stream(
).collect(Collectors.toMap(User::getUserId, u -> u)); for (Integer in : collect.
keySet()) { User u = collect.get(in);// Get each key Multi use value Value of println(u); } } /**
*map */ public static void map() { list.stream().map(User::getUserId).forEach(
userId-> println(userId)); list.stream().mapToInt(User::getAge).forEach(userId -
> println(userId)); list.stream().mapToDouble(User::getUserId).forEach(userId ->
println(userId)); list.stream().mapToLong(User::getUserId).forEach(userId ->
println(userId)); } /** * Find and match *
allMatch Methods and anyMatch almost , Indicates that all elements are satisfied before returning true.noneMatch Method means no element satisfied */ public static
void anyMatch() { boolean anyMatch = list.stream().anyMatch(u -> u.getAge() ==
100); boolean allMatch = list.stream().allMatch(u -> u.getUserId() == 10);
boolean noneMatch = list.stream().noneMatch(u -> u.getUserId() == 10); println(
anyMatch); println(allMatch); println(noneMatch); } /** * reduce operation */ public
static void reduce() { Optional<Integer> sum = list.stream().map(User::getAge).
reduce(Integer::sum); Optional<Integer> max = list.stream().map(User::getAge).
reduce(Integer::max); Optional<Integer> min = list.stream().map(User::getAge).
reduce(Integer::min); println(sum); println(max); println(min); } /** * Common output *
@param c */ public static void println(Object c) { System.out.println(c.toString
()); }

Technology
©2019-2020 Toolsou All rights reserved,
QCustomPlot series (5)- Real time dynamic curve python To solve the problem of dictionary writing list in GDOI2019 travels Java Basics ( Bubble sort )Unity Scene loading asynchronously ( Implementation of loading interface )" Cephalosporin wine Say go and go "? DANGER ! Don't drink alcohol when taking these drugs 2021 year 1 Monthly programmer salary statistics , average 14915 element Huawei certification HCIA-AI artificial intelligence java Simple lottery algorithm , luck draw DemoNOI2019 travels