基础数据:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
private String sex;
private String city;
private Integer money;
static List<User> users = new ArrayList<>();
public static void main(String[] args) {
test1();
}
public static void test1(){
users.add(new User("诺手",21,"男","北京",1200));
users.add(new User("佛爷个",21,"男","北京",2100));
users.add(new User("刀妹",23,"女","上海",3200));
users.add(new User("海哥2",30,"女","上海",2600));
users.add(new User("压缩",21,"男","苏州",2800));
users.add(new User("马儿扎哈",21,"男","苏州",1500));
users.add(new User("螳螂",30,"女","杭州",2560));
users.add(new User("狮子狗",21,"男","杭州",2780));
users.add(new User("赵信",21,"男","成都",1890));
}
}
练习一 : 根据城市分组,代码如下:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
private String sex;
private String city;
private Integer money;
static List<User> users = new ArrayList<>();
public static void main(String[] args) {
test1();
test2{}
}
public static void test1(){
users.add(new User("诺手",21,"男","北京",1200));
users.add(new User("佛爷个",21,"男","北京",2100));
users.add(new User("刀妹",23,"女","上海",3200));
users.add(new User("海哥2",30,"女","上海",2600));
users.add(new User("压缩",21,"男","苏州",2800));
users.add(new User("马儿扎哈",21,"男","苏州",1500));
users.add(new User("螳螂",30,"女","杭州",2560));
users.add(new User("狮子狗",21,"男","杭州",2780));
users.add(new User("赵信",21,"男","成都",1890));
}
//根据城市分组
public static void test2(){
Map<String,List<User>> collect=users.stream().collect(Collectors.groupingBy(User::getCity));
collect.forEach((k,v)->{
System.out.println("城市名:"+k);
System.out.println(v);
});
}
}
运行结果如下:
练习二:列出每个城市的最有钱的
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
private String sex;
private String city;
private Integer money;
static List<User> users = new ArrayList<>();
public static void main(String[] args) {
test1();
test4 ()
}
public static void test1(){
users.add(new User("诺手",21,"男","北京",1200));
users.add(new User("佛爷个",21,"男","北京",2100));
users.add(new User("刀妹",23,"女","上海",3200));
users.add(new User("海哥2",30,"女","上海",2600));
users.add(new User("压缩",21,"男","苏州",2800));
users.add(new User("马儿扎哈",21,"男","苏州",1500));
users.add(new User("螳螂",30,"女","杭州",2560));
users.add(new User("狮子狗",21,"男","杭州",2780));
users.add(new User("赵信",21,"男","成都",1890));
}
//列出每个城市的最有钱的
public static void test4(){
Map<String, Optional<User>> collect=users.stream().collect(Collectors.groupingBy(User::getCity,Collectors.
maxBy(Comparator.comparingInt(User::getMoney))));
collect.forEach((k,v)->{
System.out.println("地区:"+k);
System.out.println("冠军是:"+v);
});
};
}
运行结果:
练习三根据性别分组:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
private String sex;
private String city;
private Integer money;
static List<User> users = new ArrayList<>();
public static void main(String[] args) {
test1();
test3()
}
public static void test1(){
users.add(new User("诺手",21,"男","北京",1200));
users.add(new User("佛爷个",21,"男","北京",2100));
users.add(new User("刀妹",23,"女","上海",3200));
users.add(new User("海哥2",30,"女","上海",2600));
users.add(new User("压缩",21,"男","苏州",2800));
users.add(new User("马儿扎哈",21,"男","苏州",1500));
users.add(new User("螳螂",30,"女","杭州",2560));
users.add(new User("狮子狗",21,"男","杭州",2780));
users.add(new User("赵信",21,"男","成都",1890));
}
public static void test3(){
Map<String,List<User>> collect=users.stream().collect(Collectors.groupingBy(User::getSex));
collect.forEach((k,v)->{
System.out.println("性别:"+k);
System.out.println(v);
});
}
}
结果如下
练习四 :求每个城市的总金额
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
private String sex;
private String city;
private Integer money;
static List<User> users = new ArrayList<>();
public static void main(String[] args) {
test1();
test5();
}
public static void test1(){
users.add(new User("京爷1",21,"男","北京",1200));
users.add(new User("京爷2",21,"男","北京",2100));
users.add(new User("海哥1",23,"女","上海",3200));
users.add(new User("海哥2",30,"女","上海",2600));
users.add(new User("苏姐1",21,"男","苏州",2800));
users.add(new User("苏姐2",21,"男","苏州",1500));
users.add(new User("螳螂",30,"女","杭州",2560));
users.add(new User("狮子狗",21,"男","杭州",2780));
users.add(new User("赵信",21,"男","成都",1890));
}
//求每个城市的销售总金额s
public static void test5(){
Map<String,Integer> map = users.stream().collect(Collectors.groupingBy(User::getCity,
Collectors.summingInt(User::getMoney)));
map.forEach((k,v)->{
System.out.println("城市:"+k);
System.out.println("销售总额:"+v);
});
}
}
结果
练习六; 年龄大于28岁的
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
private String sex;
private String city;
private Integer money;
static List<User> users = new ArrayList<>();
public static void main(String[] args) {
test1();
test4();
}
public static void test1(){
users.add(new User("京爷1",21,"男","北京",1200));
users.add(new User("京爷2",21,"男","北京",2100));
users.add(new User("海哥1",23,"女","上海",3200));
users.add(new User("海哥2",30,"女","上海",2600));
users.add(new User("苏姐1",21,"男","苏州",2800));
users.add(new User("苏姐2",21,"男","苏州",1500));
users.add(new User("螳螂",30,"女","杭州",2560));
users.add(new User("狮子狗",21,"男","杭州",2780));
users.add(new User("赵信",21,"男","成都",1890));
}
//过滤年领大于28的员工
public static void test7(){
List<User> collect = users.stream().filter(s -> s.getAge() > 28).collect(Collectors.toList());
System.out.println(collect);
}
}
结果如下:
练习八:根据moey给所有人排序
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
private String sex;
private String city;
private Integer money;
static List<User> users = new ArrayList<>();
public static void main(String[] args) {
test1();
test10();
}
public static void test1(){
users.add(new User("京爷1",21,"男","北京",1200));
users.add(new User("京爷2",21,"男","北京",2100));
users.add(new User("海哥1",23,"女","上海",3200));
users.add(new User("海哥2",30,"女","上海",2600));
users.add(new User("苏姐1",21,"男","苏州",2800));
users.add(new User("苏姐2",21,"男","苏州",1500));
users.add(new User("螳螂",30,"女","杭州",2560));
users.add(new User("狮子狗",21,"男","杭州",2780));
users.add(new User("赵信",21,"男","成都",1890));
}
//全国根据销售金额排序
public static void test10(){
users.sort(Comparator.comparing(User::getMoney).reversed());
int i=0;
//遍历输出
for(User user : users){
i++;
System.out.println("第"+i+"名:"+user);
}
}
结果如下:
练习九,计算每人平均money
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
private String sex;
private String city;
private Integer money;
static List<User> users = new ArrayList<>();
public static void main(String[] args) {
test1();
test10();
}
public static void test1(){
users.add(new User("京爷1",21,"男","北京",1200));
users.add(new User("京爷2",21,"男","北京",2100));
users.add(new User("海哥1",23,"女","上海",3200));
users.add(new User("海哥2",30,"女","上海",2600));
users.add(new User("苏姐1",21,"男","苏州",2800));
users.add(new User("苏姐2",21,"男","苏州",1500));
users.add(new User("螳螂",30,"女","杭州",2560));
users.add(new User("狮子狗",21,"男","杭州",2780));
users.add(new User("赵信",21,"男","成都",1890));
}
//人均销售金额
public static void test9(){
double asDouble = users.stream().mapToDouble(User::getMoney).average().getAsDouble();
System.out.println(asDouble);
}
}
练习十:总共有多少个不同的城市?
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int age;
private String sex;
private String city;
private Integer money;
static List<User> users = new ArrayList<>();
public static void main(String[] args) {
test1();
test10();
}
public static void test1(){
users.add(new User("京爷1",21,"男","北京",1200));
users.add(new User("京爷2",21,"男","北京",2100));
users.add(new User("海哥1",23,"女","上海",3200));
users.add(new User("海哥2",30,"女","上海",2600));
users.add(new User("苏姐1",21,"男","苏州",2800));
users.add(new User("苏姐2",21,"男","苏州",1500));
users.add(new User("螳螂",30,"女","杭州",2560));
users.add(new User("狮子狗",21,"男","杭州",2780));
users.add(new User("赵信",21,"男","成都",1890));
}
public static void testi(){
List<String> collect = users.stream().map(User::getCity).distinct().collect(Collectors.toList());
int size = collect.size();
System.out.println("总共有"+size+"个城市,它们是:"+collect);
}
}
结果如下:
练习本章代码需要额外的lombok包依赖文件,加载到pomwen文件中
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.24</version>
</dependency>