一、interface 1.8之后支持在接口中 static 、default修饰的方法,不用强制要求被重写;
default修饰符:同包中或者类中调用;
二、Lambda表达式
流式编程:
目录
collect用法
List<String> codeids = progressdatas.stream().collect(ArrayList::new, (list, codeinfo) -> {
list.add(StringUtils.toString(codeinfo.getCodeid()));
}, List::addAll);
public class stream {
public static void main(String[] args) throws ParseException {
List<UserInfo> userInfoList = new ArrayList<>();
// Long userid, Date birthday, String usersex
userInfoList.add(new UserInfo(1L,"小明",Generatebirthday("2000-01-01"),"男"));
userInfoList.add(new UserInfo(1L,"小明",Generatebirthday("2000-01-01"),"男"));
userInfoList.add(new UserInfo(3L,"小红",Generatebirthday("2001-01-01"),"女"));
userInfoList.add(new UserInfo(4L,"小房",Generatebirthday("2003-01-01"),"男"));
foreach循环调用
//List
// userInfoList.forEach(userInfo -> System.out.println(userInfo));
//List转map
// Map<Object, Object> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getUsername, userInfo -> userInfo));
// Map<Object, Object> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getUsername, userInfo -> userInfo, (o, o2) -> o2));
//SET集合去重
// userInfoList.stream().collect(Collectors.toSet()).stream().forEach(o -> System.out.println(o));
// HashSet<Object> set = new HashSet<>();
// userInfoMap.forEach((k,v) -> set.add(v));
// userInfoList.forEach(v -> set.add(v));
//以性别进行分组
// Map<String, List<UserInfo>> collect = userInfoList.stream().collect(Collectors.groupingBy(userInfo -> userInfo.getUsersex()));
// System.out.println(collect);
//排序
//时间排序
/*userInfoList.stream().sorted(new Comparator<UserInfo>() {
@Override
public int compare(UserInfo o1, UserInfo o2) {
return o1.getBirthday().compareTo(o2.getBirthday());
}
}).forEach(System.out::println);*/
//userid排序
/*collect.entrySet().stream().sorted(new Comparator<Map.Entry<String, List<UserInfo>>>() {
@Override
public int compare(Map.Entry<String, List<UserInfo>> o1, Map.Entry<String, List<UserInfo>> o2) {
return o2.getKey().compareTo(o1.getKey());
}
}).forEach(System.out::println);*/
//计算
//累加id
// Long aLong = userInfoList.stream().map(userInfo -> userInfo.getUserid()).reduce((aLong1, aLong2) -> aLong2 + aLong1).get();
// System.out.println(aLong);
//求最大最小值 先排序再取最开头或者结尾的
/*UserInfo userInfo = userInfoList.stream().max(new Comparator<UserInfo>() {
@Override
public int compare(UserInfo o1, UserInfo o2) {
return o1.getBirthday().compareTo(o2.getBirthday());
}
}).get();
System.out.println(userInfo);*/
/*UserInfo userInfo = userInfoList.stream().min(new Comparator<UserInfo>() {
@Override
public int compare(UserInfo o1, UserInfo o2) {
return o1.getBirthday().compareTo(o2.getBirthday());
}
}).get();
System.out.println(userInfo);*/
//匹配
//所有匹配
// boolean b = userInfoList.stream().allMatch(userInfo -> userInfo.getUsername().equals("小明"));
// System.out.println(b);
//匹配任意一条
// boolean b1 = userInfoList.stream().anyMatch(userInfo -> userInfo.getUsername().equals("小明"));
//过滤+取前几条 peek()用来打印日志 属于中间操作
/*List<UserInfo> 小明 = userInfoList.stream().peek(userInfo1 -> System.out.println(userInfo1)).filter(new Predicate<UserInfo>() {
@Override
public boolean test(UserInfo userInfo) {
return userInfo.getUsername().equals("小明");
}
}).peek(userInfo1 -> System.out.println(userInfo1)).collect(Collectors.toList());
System.out.println(小明);*/
//取前几个 limit 跳过前几个 skip
List<UserInfo> userInfoList1 = userInfoList.stream().peek(o -> System.out.println(o)).skip(1).limit(3).collect(Collectors.toList());
//合并两个流 使用流里面得内容创建新对象
List<UserInfo> collect = Stream.concat(userInfoList1.stream(), userInfoList.stream())
.map(userInfo -> new UserInfo(userInfo.getUserid(), userInfo.getUsername(), userInfo.getBirthday(), userInfo.getUsersex())).collect(Collectors.toList());
System.out.println(collect);
}
static Date Generatebirthday(String day) throws ParseException {
return new SimpleDateFormat("yyyy-MM-dd").parse(day);
}
}
UserInfo 类
package bean;
import java.util.Date;
public class UserInfo {
/**
* 用户ID
*/
private Long userid;
/**
* 生日
*/
private Date birthday;
/**
* 性别
*/
private String usersex;
/**
* 用户名
*/
private String username;
/**
* 手机
*/
private String mobile;
public UserInfo(Long userid,String username ,Date birthday, String usersex) {
this.userid = userid;
this.birthday = birthday;
this.username = username;
this.usersex = usersex;
}
@Override
public String toString() {
return "UserInfo{" +
"userid=" + userid +
", birthday=" + birthday +
", usersex='" + usersex + '\'' +
", username='" + username + '\'' +
", mobile='" + mobile + '\'' +
'}';
}
public Long getUserid() {
return userid;
}
public void setUserid(Long userid) {
this.userid = userid;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getUsersex() {
return usersex;
}
public void setUsersex(String usersex) {
this.usersex = usersex;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserInfo userInfo = (UserInfo) o;
if (userid != null ? !userid.equals(userInfo.userid) : userInfo.userid != null) return false;
if (birthday != null ? !birthday.equals(userInfo.birthday) : userInfo.birthday != null) return false;
if (usersex != null ? !usersex.equals(userInfo.usersex) : userInfo.usersex != null) return false;
if (username != null ? !username.equals(userInfo.username) : userInfo.username != null) return false;
return mobile != null ? mobile.equals(userInfo.mobile) : userInfo.mobile == null;
}
@Override
public int hashCode() {
int result = userid != null ? userid.hashCode() : 0;
result = 31 * result + (birthday != null ? birthday.hashCode() : 0);
result = 31 * result + (usersex != null ? usersex.hashCode() : 0);
result = 31 * result + (username != null ? username.hashCode() : 0);
result = 31 * result + (mobile != null ? mobile.hashCode() : 0);
return result;
}
}