package com.example.projectmain;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Created with IntelliJ IDEA.
* User: Administrator
* Date: 2023/1/6/006
* Time: 15:55
* Description: Lambada List集合操作
*/
public class test {
public static void main(String[] args) {
List<Person> persons = new ArrayList<>();
persons.add(new Person(22, "男", 90.0,new BigDecimal(150)));
persons.add(new Person(17, "女", 91.0,new BigDecimal(152)));
persons.add(new Person(12, "男", 92.0,new BigDecimal(155)));
persons.add(new Person(12, "女", 92.0,new BigDecimal(158)));
persons.add(new Person(25, "女", 93.0,new BigDecimal(159)));
// sortPersons(persons);//排序
// ages(persons);//取某个属性list
groupbySex(persons);//按照属性分组
// filterPersons(persons);//过滤集合
// keyMap(persons);//list转map重复key覆盖
// getDataMap(persons);//获取最大最小值及平均数
// sumAge(persons);//获取基本类型的和
// bigDecimalWeightSum(persons);//获取Bigdecimal求和
// distinctAgeList(persons);//对基本集合去重
// countByList(persons);//对集合中相同属性进行计数
// sort(persons);//集合排序
// sorted(persons);//集合排序
// reverseOrder(persons);//倒叙排序
// multiOrder(persons);//多个字段排序
}
/**
* List排序
* @param persons
* @return
*/
public static List<Person> sortPersons(List<Person> persons){
persons.sort((Person o1, Person o2) -> {
return o1.age - o2.age;
});
/*
Person{age=12, sex=男, score=92.0}
Person{age=17, sex=女, score=91.0}
Person{age=22, sex=男, score=90.0}
Person{age=25, sex=女, score=93.0}
*/
persons.forEach(System.out::println);
return persons;
}
//取某个属性List
public static List<Integer> ages(List<Person> persons){
List<Integer> ages=persons.stream().map(Person::getAge).collect(Collectors.toList());
ages.forEach(System.out::println);
return ages;
}
//按照属性分组
public static Map<String, List<Person>> groupbySex(List<Person> persons){
Map<String, List<Person>> groupbySexs=persons.stream().collect(Collectors.groupingBy(Person::getSex));
//{女=[Person{age=17, sex=女, score=91.0}, Person{age=25, sex=女, score=93.0}], 男=[Person{age=12, sex=男, score=92.0}, Person{age=22, sex=男, score=90.0}]}
System.out.println(groupbySexs);
return groupbySexs;
}
//过滤集合
public static List<Person> filterPersons(List<Person> persons){
List<Person> filterPersons=persons.stream().filter(Person->Person.getAge()>=17).collect(Collectors.toList());
//[Person{age=17, sex=女, score=91.0}, Person{age=22, sex=男, score=90.0}, Person{age=25, sex=女, score=93.0}]
System.out.println(filterPersons);
return filterPersons;
}
//list转map重复key覆盖
public static Map<Integer,Person> keyMap(List<Person> persons){
Map<Integer,Person> keyPersonMap=persons.stream().collect(Collectors.toMap(Person::getAge, Function.identity(),(key1, key2)->key1));
System.out.println(keyPersonMap);
return keyPersonMap;
}
//获取最大最小值及平均数
public static Map<String,Object> getDataMap(List<Person> persons){
Map<String,Object> dataMap=new HashMap<>();
//最大值
Integer max=persons.stream().mapToInt(Person::getAge).max().getAsInt();
Integer min=persons.stream().mapToInt(Person::getAge).min().getAsInt();
double Average=persons.stream().mapToInt(Person::getAge).average().getAsDouble();
dataMap.put("max",max);
dataMap.put("min",min);
dataMap.put("Average",Average);
System.out.println(dataMap);
return dataMap;
}
//基本类型求和
public static Integer sumAge(List<Person> persons){
Integer sumAge=persons.stream().mapToInt(Person::getAge).sum();
System.out.println("sumAge:"+sumAge);
return sumAge;
}
//bigdecimal求和
public static BigDecimal bigDecimalWeightSum(List<Person> persons){
BigDecimal weightSum=persons.stream().map(Person::getWeight).reduce(BigDecimal.ZERO,BigDecimal::add);
System.out.println("weightSum:"+weightSum);
return weightSum;
}
//对基本集合去重
public static List<Integer> distinctAgeList(List<Person> persons){
List<Integer> ages=ages(persons);
System.out.println("ages:"+ages);
List<Integer> distinctAgeList=ages.stream().distinct().collect(Collectors.toList());
System.out.println("distinctAgeList:"+distinctAgeList);
return distinctAgeList;
}
//判断是否存在某个属性值
public static boolean exist(List<Person> persons){
boolean exist=persons.stream().allMatch(s->s.getAge()==12);
System.out.println("exist:"+exist);
return exist;
}
//对集合中相同属性进行计数
public static long countByList(List<Person> persons){
long count=persons.stream().filter(s->s.getAge()==12).count();
System.out.println("count:"+count);
return count;
}
//对集合进行排序
public static List<Person> sort(List<Person> persons){
persons.sort(Comparator.comparing(Person::getAge));
System.out.println("persons-1:"+persons);
return persons;
}
//对集合进行排序
public static List<Person> sorted(List<Person> persons){
persons.stream().sorted(Comparator.comparing(Person::getAge));
System.out.println("persons-2:"+persons);
return persons;
}
//倒序排序
public static List<Person> reverseOrder(List<Person> persons){
persons.sort(Comparator.comparing(Person::getAge,Comparator.reverseOrder()));
System.out.println("persons-rever:"+persons);
return persons;
}
//多个字段排序
public static List<Person> multiOrder(List<Person> persons){
persons.sort(Comparator.comparing(Person::getAge,Comparator.reverseOrder()).thenComparing(Person::getWeight));
System.out.println("persons:"+persons);
return persons;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
主要包含常用的排序、取某个属性list、按照属性分组、过滤集合、list转map重复key覆盖、获取最大最小值及平均数、获取基本类型的和、获取Bigdecimal求和、对基本集合去重、对集合中相同属性进行计数、集合排序、倒叙排序、多个字段排序等。对lambada更容易理解及掌握。即用的方式,了解lambada List,map等操作
资源推荐
资源详情
资源评论
收起资源包目录
project-main.zip (44个子文件)
project-main
.gradle
buildOutputCleanup
cache.properties 51B
buildOutputCleanup.lock 17B
outputFiles.bin 18KB
7.6
fileChanges
last-build.bin 1B
checksums
md5-checksums.bin 27KB
sha1-checksums.bin 42KB
checksums.lock 17B
executionHistory
executionHistory.bin 45KB
executionHistory.lock 17B
dependencies-accessors
gc.properties 0B
dependencies-accessors.lock 17B
fileHashes
fileHashes.lock 17B
resourceHashesCache.bin 19KB
fileHashes.bin 19KB
gc.properties 0B
vcsMetadata
vcs-1
gc.properties 0B
file-system.probe 8B
gradle
wrapper
gradle-wrapper.jar 59KB
gradle-wrapper.properties 200B
HELP.md 685B
src
test
java
com
example
projectmain
ProjectMainApplicationTests.java 229B
main
resources
application.properties 1B
java
com
example
projectmain
test.java 6KB
ProjectMainApplication.java 338B
Person.java 1KB
gradlew.bat 3KB
build.gradle 522B
.idea
jarRepositories.xml 858B
uiDesigner.xml 9KB
workspace.xml 4KB
misc.xml 570B
compiler.xml 174B
gradle.xml 657B
.gitignore 190B
settings.gradle 34B
build
classes
java
main
com
example
projectmain
Person.class 2KB
ProjectMainApplication.class 768B
test.class 10KB
resources
main
application.properties 1B
tmp
compileJava
compileTransaction
header-output
compile-output
com
example
projectmain
stash-dir
test.class.uniqueId0 10KB
Person.class.uniqueId1 2KB
annotation-output
previous-compilation-data.bin 13KB
generated
sources
annotationProcessor
java
main
headers
java
main
gradlew 8KB
.gitignore 444B
共 44 条
- 1
为你写诗_xue
- 粉丝: 13
- 资源: 29
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页