public void testJavaXinTeXing() {
//set集合一
HashSet memberSet= new HashSet<>();
memberSet.add("1");
memberSet.add("2");
memberSet.add("3");
//set集合二
Set collect1 = null;
//查询出所有的企业会员
List memberCompanyNo = memberCompanyDao.findNoPost();
if (memberCompanyNo.size() > 0) {
//拿到企业会员的会员Id并且存入到set集合中
collect1 = memberCompanyNo.stream().map(x -> x.getMemberId()).collect(Collectors.toSet());
}
//把Set 快速转为 Set
Set memberDb = new HashSet<>(collect1.size());
collect1.forEach(i -> memberDb.add(i.toString()));
Stream stream1 = memberDb.stream();
Stream stream2 = memberSet.stream();
//把两个set集合快速合并在一个新set集合中
//Stream.concat(stream, stream1).collect(Collectors.toSet()).stream().sorted().forEach(name -> System.out.println(name));
Set memberDbAndRedis = Stream.concat(stream1, stream2).collect(Collectors.toSet());
//将set集合转为List 因为List可以排序 字符串按首字母 数字按 大小
List tempList = new ArrayList(memberDbAndRedis);
//排序
Collections.sort(tempList, (a, b) -> b.compareTo(a));
System.out.println(tempList);
}
/**
* 测试条件过滤
*/
public void testJavaXi() {
String[] str = {"扫地僧", "乔峰", "段誉", "虚竹"};
String[] str1 = {"张三丰", "张无忌", "张翠山", "宋远桥"};
//数组转为流
Stream str6 = Stream.of(str);
Stream str7 = Stream.of(str1);
//打印数组中字符串长度为2 并且只要前两个 并且转为list 或者set
Stream.of(str).filter((s) -> s.length() == 2).limit(2).collect(Collectors.toList()).forEach(name -> System.out.println(name));
List collect = Stream.of(str).filter((s) -> s.length() == 2).limit(2).collect(Collectors.toList());
//打印数组中字符串首字母为张, 并且跳过前两个 并且转为list 或者set
Stream.of(str1).filter((s) -> s.startsWith("张")).skip(2).collect(Collectors.toList()).forEach(name -> System.out.println(name));
//合并两个流 不管时List set 数组 先要转为流 再合并
Stream.concat(str6, str7).forEach(name -> System.out.println(name));
}
/**
* 测试hashMap一直添加值
*/
public void testHashMap(){
ArrayList> list = new ArrayList<>();
list.add(new HashMap(){{put("username" , "wty");put("password" , "123456");put("phone" , "18198318706");}});
list.add(new HashMap(){{put("username" , "wtytest");put("password" , "1234test");put("phone" , "18198318706");}});
list.stream().forEach(name -> System.out.println(name));
}
public void testJavaStream(){
ArrayList userList = new ArrayList<>();
userList.add(new User("wty","111","222",10));
userList.add(new User("zrm","zzz","233",2));
userList.add(new User("wty","111","222",10));
userList.add(new User("wty","111","222",10));
userList.add(new User("wty","111","222",1));
userList.add(new User("zkq","567","222",5));
userList.add(new User("zzm","567","222",7));
userList.add(new User("smm","567","222",5));
userList.add(new User("phh","567","222",4));
// 过滤操作 Filter
//拿到userList集合中部门ID是偶数 并且需要去重 的全部数据 去重需要在User类中重写hashCode 和 equas方法
//filter操作 + distinct
List userList1 = userList.stream().filter(x -> x.getDeptId() % 2 == 0).distinct().collect(Collectors.toList());
userList1.forEach(s-> System.out.println(s));
//打印值:
// User{id=null, userName='wty', phone='111', address='222', deptId='10'}
// User{id=null, userName='zrm', phone='zzz', address='233', deptId='2'}
// User{id=null, userName='phh', phone='567', address='222', deptId='4
//拿到用户名为wty 的前两条 在这里需要注意 先拿到两条数据之后 如果是set会去重 是list不会去重
//filter操作 + limit
Set user1Set = userList.stream().filter(x -> "wty".equals(x.getUserName())).limit(2).collect(Collectors.toSet());
List user1List2 = userList.stream().filter(x -> "wty".equals(x.getUserName())).limit(2).collect(Collectors.toList());
user1Set.forEach(s-> System.out.println(s));
//打印值 :
//User{id=null, userName='wty', phone='111', address='222', deptId='10'}
user1List2.forEach(a-> System.out.println(a));
//打印值:
// User{id=null, userName='wty', phone='111', address='222', deptId='10'}
//User{id=null, userName='wty', phone='111', address='222', deptId='10'}
//拿到姓名为wty 并且根据部门Id排序 拿到前4条数据
//filter操作 + sorted + limit limit和skip拿的数据正好相反
List user2Set = userList.stream().filter(x -> "wty".equals(x.getUserName())).sorted((x1, x2) -> x2.getDeptId() - x1.getDeptId()).limit(4).collect(Collectors.toList());
user2Set.forEach(x-> System.out.println(x));
//打印值:
// User{id=null, userName='wty', phone='111', address='222', deptId='10'}
// User{id=null, userName='wty', phone='111', address='222', deptId='10'}
// User{id=null, userName='wty', phone='111', address='222', deptId='10'}
// User{id=null, userName='wty', phone='111', address='222', deptId='1'}
//拿到用户名为wty 并且根据部门Id排序 跳过前面两个
//filter操作 + sorted + skip
List user3Set = userList.stream().filter(x -> "wty".equals(x.getUserName())).sorted((x1, x2) -> x2.getDeptId() - x1.getDeptId()).skip(2).collect(Collectors.toList());
user3Set.forEach(f-> System.out.println(f));
//打印值:
// User{id=null, userName='wty', phone='111', address='222', deptId='10'}
// User{id=null, userName='wty', phone='111', address='222', deptId='1'}
//映射操作:
//查询出集合中所有名字为wty 的电话号码
List userNameList = userList.stream().filter(x -> "wty".equals(x.getUserName())).map(User::getPhone).collect(Collectors.toList());
userNameList.forEach(o-> System.out.println(o));
//打印值:
// 111
// 111
// 111
// 111
//三. 终端操作
// anyMatch 集合中的某个对象 只要有任何一个满足 返回true
boolean b = userList.stream().anyMatch(x -> x.getDeptId() > 10);
//allMatch 集合中的某个对象 都必须满足 返回true
boolean b1 = userList.stream().allMatch(x -> "wty".equals(x.getUserName()));
//noneMatch 都不满足 返回 true
boolean b2 = userList.stream().noneMatch(x -> x.getDeptId() > 5);
System.out.println(b);
System.out.println(b1);
System.out.println(b2);
// //打印:
// b : false
// b1: false
// b2: false
//字符串拼接
//拿到集合中所有的名字就行拼接
String collect = userList.stream().map(x -> x.getUserName()).collect(Collectors.joining());
//拿到集合中所有的名字就行拼接 中间以 # 隔开
String collect1 = userList.stream().map(x -> x.getUserName()).collect(Collectors.joining("#"));
System.out.println(collect);
System.out.println(collect1);
//打印:
// wtyzrmwtywtywtyzkqzzmsmmphh
// wty#zrm#wty#wty#wty#zkq#zzm#smm#phh
//把数组str中的大于3的值存放在另一个list中
ArrayList shuZulist = new ArrayList<>();
String[] str = {"I" ,"love" ,"YOU" ,"too" ,"zhanruMEI"};
//数组转为List 两种方式
List strings = Arrays.asList(str);
List strings1 = Stream.of(str).collect(Collectors.toList());
strings.forEach(str1 ->{
if(str1.length() >= 3)
shuZulist.add(str1);
});
System.out.println(shuZulist);
// 打印值:
// [love, YOU, too, zhanruMEI]
public void testResult(){
// 如果空值使用list流的话 会包空指针;
ArrayList list1 = new ArrayList<>();
MemberVo memberVo = null;
list1.add(memberVo);
List collect2 = list1.stream().map(x -> x.getAddressAreaId()).collect(Collectors.toList());
for (Integer integer : collect2) {
System.out.println(integer);
}
//将map中的值 拼接成为一个地址
HashMap testMap = new HashMap<>();
testMap.put("username","wty");
testMap.put("password","123456");
testMap.put("phone","18198318706");
testMap.put("age","23");
//打印map值中的key value
testMap.forEach((key , vlaue)-> System.out.println(key +"====" + vlaue));
StringBuffer stringBuffer = new StringBuffer();
//按照先后添加顺序拼接
stringBuffer.append("loc.91xjm.com:8082/apiseller/update/goods/storage?");
testMap.forEach((k , v) -> {
try {
stringBuffer.append(k +"="+ URLEncoder.encode((String)v ,"utf-8") +"&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
});
//去掉字符串中的最后一个字符
String substring = stringBuffer.substring(0, stringBuffer.length() - 1);
System.out.println(substring);
//如果map中没有age2这个key 那么打印默认值 默认值 如果map中存在age2打印出age2对应的值
System.out.println(testMap.getOrDefault("age2", "默认值"));
//替换到map中value=wty2的值 也可以替换key
testMap.replaceAll((k , v) ->{
if(v.equals("wty2")){
v = "zkq";
}
return v;
});
testMap.forEach((x , y)-> System.out.println(x+"===="+y));
ArrayList objects3 = new ArrayList<>();
objects3.add("edwrfwe564a");
objects3.add("54662544sc");
objects3.add("66621");
objects3.add("666");
//拿到字符串中 最大的那个纯数子 字符串可能字符串字母
String s4 = objects3.stream().filter(x -> x.matches("[0-9]{1,}")).max(Comparator.comparing((x) -> Integer.valueOf(x))).get();
System.out.println(s4);//66621
String s3 = objects3.stream().filter(x -> x.matches("[0-9]{1,}")).collect(Collectors.toList()).stream().max(Comparator.comparing((x) -> x)).get();
System.out.println(s3);//66621
//去掉list集合中包含字符串的字符串,之后拿到字符串中最大的那位数
String s2 = objects3.stream().filter(x -> x.matches("[0-9]{1,}")).reduce((x1, x2) -> Integer.valueOf(x1) >= Integer.valueOf(x2) ? x1 : x2).get();
System.out.println(s2); //66621
//拿到字符串随机最长的那一位
Optional reduce = objects3.stream().reduce((x1, x2) -> x1.length() > +x2.length() ? x1 : x2);
System.out.println(reduce.get());//edwrfwe564a
//随机获取3 到 5 之间的随机数
double number = Math.random() * ((3 - 1) / 1) + (5 + 1 - 3);
System.out.println(number);
List integers1 = Arrays.asList(1, 2, 3 , 4);
List integers2 = Arrays.asList(4, 5, 6);
//获取集合interger2中偶数的值
List collect1 = integers2.stream().filter(x -> x % 2 == 0).collect(Collectors.toList());
System.out.println(collect1);
//合并两个集合 ,并且去掉重复的数字
List collect = Stream.of(integers1, integers2).flatMap(test -> test.stream()).distinct().collect(Collectors.toList());
collect.forEach(s-> System.out.println(s));
integers1.stream().map(x->x*x).forEach(s-> System.out.println(s));
List integers3 = Arrays.asList("helloWord" , "wheword");
integers3.stream().map(x->x.split("")).forEach(o-> System.out.println(o));
//将list中的值单个的拆分出来
integers3.stream().map(x->x.split("")).flatMap(s->Arrays.stream(s)).distinct().forEach(o-> System.out.println(o));
integers3.stream().map(x->x.split("")).flatMap(Arrays::stream).distinct().forEach(o-> System.out.println(o));
List userList = Arrays.asList(new User("汪太勇", "18198318706", "重庆", 91),
new User("肖云云", "18198318707", "云南", 91),
new User("张开窍", "18198318708", "永川", 89),
new User("周忠明1", "18198318709", "重庆", 78),
new User("周忠明2", "18198318709", "a", 80),
new User("周忠明3", "18198318709", "b", 80),
new User("汪太勇1", "18198318706", "重庆", 67),
new User("汪太勇", "18198318706", "重庆", 67),
new User("汪太勇2", "18198318706", "重庆", 100),
new User("汪太勇3", "18198318706", "重庆", 80));
// //拿到集合中部门ID小于25之后最大部门Id 如果没有值 会包错
User user = userList.stream().filter(x -> x.getDeptId() < 25).max(Comparator.comparing((x) -> x.getDeptId())).get();
System.out.println(user);
/**
* 需求:按照部门Id排序打印 如果部门Id相同 那么名词Id 就为一样
*/
List collect3 = userList.stream().map(x -> x.getDeptId()).collect(Collectors.toSet()).stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
HashMap map = new HashMap<>();
for(int i=0; ix.setId(((Integer)map.get(x.getDeptId()))));
userList.stream().sorted((x1,x2)->x1.getId().compareTo(x2.getId())).forEach(x-> System.out.println(x));
/*
* 排序
*/
Collections.sort(userList, new Comparator() {
@Override
public int compare(User o1, User o2) {
if (o1.getDeptId() < o2.getDeptId()) {
return 1;
} else if (o1.getDeptId() == o2.getDeptId()) {
return 0;
} else {
return -1;
}
}
});
int index = 0;// 排名
int no = 0;//去重
int lastScore = -1;// 最近一次的分
for (int i = 0; i < userList.size(); i++) {
User s = userList.get(i);
if (lastScore != s.getDeptId()) { // 如果成绩和上一名的成绩不相同,那么排名+1
lastScore = s.getDeptId();
no = 0;
index = index + 1 + no;
} else {
no++;
}
s.setId(index);
}
//订制排序
userList.stream().sorted((x1 , x2) -> {
if(x1.getDeptId() == x2.getDeptId()){
return x1.getUserName().compareTo(x2.getUserName());
}else{
return -Integer.valueOf(x1.getDeptId()).compareTo(x2.getDeptId());
}
}).forEach(u-> System.out.println(u));
}
List list1 = new ArrayList();
list1.add("1");
list1.add("2");
list1.add("3");
list1.add("5");
list1.add("6");
List list2 = new ArrayList();
list2.add("2");
list2.add("3");
list2.add("7");
list2.add("8");
// 交集
List intersection = list1.stream().filter(item -> list2.contains(item)).collect(toList());
System.out.println("---交集 intersection---");
intersection.parallelStream().forEach(System.out::println);
// 差集 (list1 - list2)
List reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
System.out.println("---差集 reduce1 (list1 - list2)---");
reduce1.parallelStream().forEach(System.out::println);
// 差集 (list2 - list1)
List reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
System.out.println("---差集 reduce2 (list2 - list1)---");
reduce2.parallelStream().forEach(System.out::println);
// 并集
List listAll = list1.parallelStream().collect(toList());
List listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
System.out.println("---并集 listAll---");
listAll.parallelStream().forEachOrdered(System.out::println);
// 去重并集
List listAllDistinct = listAll.stream().distinct().collect(toList());
System.out.println("---得到去重并集 listAllDistinct---");
listAllDistinct.parallelStream().forEachOrdered(System.out::println);
System.out.println("---原来的List1---");
list1.parallelStream().forEachOrdered(System.out::println);
System.out.println("---原来的List2---");
list2.parallelStream().forEachOrdered(System.out::println);
Map> collect = companyDistributionList.stream().collect(Collectors.groupingBy(x -> x.getStoreId()));