public class Student {
private Long stuId;
private String stuName;
private Integer age;
private String stuCar;
private Integer stuClass;
}
public class RedisTest {
public static void main(String[]age){
Student st1 = new Student();
st1.setStuId(1l);
st1.setAge(1);
st1.setStuName("cui1");
st1.setStuCar("111");
st1.setStuClass(1);
Student st2 = new Student();
st2.setStuId(2l);
st2.setAge(2);
st2.setStuName("cui2");
st2.setStuCar("222");
st2.setStuClass(1);
Student st3 = new Student();
st3.setStuId(3l);
st3.setAge(3);
st3.setStuName("cui3");
st3.setStuCar("333");
st3.setStuClass(2);
Student st4 = new Student();
st4.setStuId(4l);
st4.setAge(4);
st4.setStuName("cui4");
st4.setStuCar("4444");
st4.setStuClass(2);
Student st5 = new Student();
st5.setStuId(5l);
st5.setAge(5);
st5.setStuName("cui5");
st5.setStuCar("55");
st5.setStuClass(3);
List<Student> stList = new ArrayList<>();
stList.add(st1);
stList.add(st2);
stList.add(st3);
stList.add(st4);
stList.add(st5);
listToMap(stList);
System.out.println("---------------1");
foreachList(stList);
System.out.println("---------------2");
ListToObjectMap(stList);
System.out.println("---------------3");
listTofilter(stList);
System.out.println("---------------4");
listToMapList(stList);
}
/**
* 只要班级为2班的学生
* list filter过滤
* @param stList
*/
public static void listTofilter(List<Student> stList){
List<Student> listType = stList.stream().filter(t->t.getStuClass()==2).collect(Collectors.toList());
listType.forEach(item->{
System.out.println(item.getStuName());
});
}
/**
* list转map 对象
* @param stList
*/
public static void ListToObjectMap(List<Student> stList){
Map<Long,Student> stuMap = stList.stream().collect(Collectors.toMap(Student::getStuId,Student->Student));
stuMap.forEach((k,v)->{
System.out.println(k+"--"+v.getStuName());
});
}
/**
* list转map
* @param stList
*/
public static void listToMap(List<Student> stList){
Map<Long,String> map = stList.stream().collect(Collectors.toMap(Student::getStuId,Student::getStuName));
map.forEach((k,v)->{
System.out.println("Item : " + k + " Count : " + v);
});
}
/**
* list 循环
* @param stList
*/
public static void foreachList(List<Student> stList){
stList.forEach(a->{
System.out.println(a.getStuId());
});
}
/**
* list 分组
* @param stuList
*/
public static void listToMapList(List<Student> stuList){
Map<Integer,List<Student>> s = stuList.stream().collect(Collectors.groupingBy(obj->obj.getStuClass()));
s.forEach((k,v)->{
System.out.println("k--"+k+"---"+v.size());
});
}
}
编写了jdk1.8 stream() 常用属性