public class JunitTest {private final List<Student> li = new ArrayList<Student>();
@Before
public void init(){ //初始化一个对象集合
li.add(new Student(102, "李四", 59, 6666.66));
li.add(new Student(101, "张三", 18, 9999.99));
li.add(new Student(103, "王五", 28, 3333.33));
li.add(new Student(104, "赵六", 8, 7777.77));
li.add(new Student(104, "赵六", 8, 7777.77));
li.add(new Student(104, "赵六", 8, 7777.77));
li.add(new Student(105, "田七", 38, 5555.55));
}
/**
* filter——接收 Lambda , 从流中排除某些元素。
*/
@Test //取age大于8的
public void testFilter(){
Stream<Student> aa = li.stream().filter( e ->e.getAge() > 18);
aa.forEach(s -> {
System.out.println(s);
});
}
/**
* limit——截断流,使其元素不超过给定数量。
*/
@Test//取age大于8的,取3个
public void testLimit(){
li.stream().filter( e -> e.getAge()>8)
.limit(3)
.forEach(s -> System.out.println(s));
}
/**
* skip(n) ——跳过元素,返回一个扔掉了前 n 个元素的流。若流中元素不足 n 个,则返回一个空流。与 limit(n) 互补
*/
@Test
public void testSkip() {
li.stream()
.filter((e)->e.getAge()>8)
.skip(2)//这里可以查找filter过滤后的数据,前两个不要,要后面的,与limit相反
.forEach(System.out::println);
}
/**
* distinct——筛选,通过流所生成元素的 hashCode() 和 equals() 去除重复元素
* 需要重写hashCode()和equals()
*/
@Test
public void testDistinct() {
li.stream()
.distinct()//去除重复的元素,因为通过流所生成元素的 hashCode() 和 equals() 去除重复元素,所以对象要重写hashCode跟equals方法
.forEach(System.out::println);//终止操作
}
/**
* sorted()---自然排序(Comparable)
* sorted(Comparator com)----定制排序(Comparator)
*/
@Test
public void testSorted() {
List<String> list=Arrays.asList("ccc","aw1a","2bb","1dwd","eee");
list.stream().sorted().forEach(s -> {
System.out.println(s);
});
System.out.println("=======定制排序=========");
li.stream().sorted((x,y) ->{
if(x.getAge()==y.getAge()){
return x.getName().compareTo(y.getName());
}else{
return Integer.compare(x.getAge(), y.getAge());
}
}).forEach(a -> {
System.out.println(a);
});Collections.sort(集合, Comparator.comparing(对象::属性));
}
/**
* 转换为集合
*/
@Test
public void testReduce() {
List<String> a = li.stream().map(Student:: getName).collect(Collectors.toList());
System.out.println(a);
}
/**
* allMatch——检查是否匹配所有元素
* anyMatch——检查是否至少匹配一个元素
* noneMatch——检查是否没有匹配的元素
*/
boolean boole = li.stream().anyMatch((e) -> e.getName().equals("赵六"));
System.out.println(boole);
List<Student> listu = li.stream()
.filter(m -> m.getAge() > 5 ) //年龄大于5的
.sorted(Comparator.comparingInt(Student :: getId).reversed()) //按id倒序排列
.distinct() //去重
.collect(Collectors.toList()); //转换为list集合
listu.forEach(stu -> {
System.out.println(stu);
});
String stuname = listu.stream()
.map(Student:: getName) //获取所有student的Name
.collect(Collectors.joining(",")); //拼接字符串
System.out.println(stuname);
Integer sum = li.stream().map(Student::getAge).reduce(0,(x,y) -> x+y);
System.out.println(sum);
/**
* 求和,平均值,最大最小值,统计
*/
DoubleSummaryStatistics conllect = li.stream().collect(Collectors.summarizingDouble(Student::getAge));
System.out.println("sum:"+conllect.getSum()+" max:"+conllect.getMax()+" min:"+conllect.getMin()+" count:"+conllect.getCount()+" avg:"+conllect.getAverage());
//分组
Map<Object, List<Student>> con = li.stream().collect(Collectors.groupingBy(Student::getId));
con.forEach((key,value) -> {
System.out.println(key+"-------"+value);
});
Map<Integer, Map<String, List<Student>>> stuSexMap = li.stream().collect(Collectors.groupingBy(Student::getId,Collectors.groupingBy((v) ->{
if(v.getAge()<18){
return "小于18岁的";
}else
return "大于18岁的";
})));
stuSexMap.forEach((key,values) -> {
System.out.println(key+" ---- "+values);
});
}
/**
* 根据物料编号和仓库id分组 求和
*/
Map<String, Double> countMap = m.getItems().stream().collect(Collectors.groupingBy(o -> o.getMaterId() + "_" +o.getWhId(),Collectors.summingDouble(d -> d.getQty().doubleValue())));
List<WmsOtherPutStrgD> countRecords = countMap.keySet().stream().map(key -> {
String[] temp = key.split("_");
String productType = temp[0];
String country = temp[1];
WmsOtherPutStrgD wm = new WmsOtherPutStrgD();
wm.setMaterId(productType);
wm.setWhId(country);
wm.setQty(new BigDecimal(countMap.get(key).toString()));
return wm;
}).collect(Collectors.toList());
mybatis中乐观锁:
@Version 用于注解实体字段,数据库中也应有对应的字段
1:在version属性上面加入@Version注解 ,这个注解必须要有
2:配置乐观锁插件 代码,以下是springboot的写法,写在配置类里面
/**
* 乐观锁插件
*/
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor() {
return new OptimisticLockerInterceptor();
}
3:在业务代码中遵循 先 查询 后 修改的 操作,这样乐观锁version版本才会 version+1
User user1 = userMapper.selectById(1);
user1.setName("666");
userMapper.updateById(user1);