一、List操作
1.1 List集合去重
public static void main(String[] args) {
List<String> memberIds = new ArrayList<>();
memberIds.add("A");
memberIds.add("B");
memberIds.add("A");
List<String> distinctList = memberIds.stream().distinct().collect(Collectors.toList());
//[A, B]
System.out.println(distinctList);
}
1.2 List排序
List<Student> sortedCivilStudents = students.stream()
.filter(student -> "土木工程".equals(student.getMajor())).sorted((s1, s2) -> s1.getAge() - s2.getAge())
.limit(2)
.collect(Collectors.toList());
1.3 只取List集合对象的某一个字段
List<String> names = students.stream()
.filter(student -> "计算机科学".equals(student.getMajor()))
.map(Student::getName).collect(Collectors.toList());
1.4 快速创建List
List<Double> list1=Arrays.asList(32.5,78.3,45.6);
二、时间
2.1 毫秒转小时
public static void main(String[] args) {
Long lastTime = 1650091356556L - 1650011356556L;
System.out.println(TimeUnit.MILLISECONDS.toHours(lastTime));
}
2.2 LocalDate格式化
LocalDate now = LocalDate.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
String JDK8Time = now.format(dateTimeFormatter);
System.out.println("JDK8格式化后的时间数据=="+JDK8Time);
2.3 LocalDate转String
LocalDate localDate = LocalDate.now();
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateStr = localDate.format(fmt);
System.out.println("LocalDate转String:" + dateStr);
2.4 Date转LocalDate
Date date = new Date();
System.out.println("LocalDateTime():" + (new Date()).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
2.5 时间戳转LocalDateTime
long timestamp = System.currentTimeMillis();
Instant instant1 = Instant.ofEpochMilli(timestamp);
System.out.println("时间戳 转 LocalDateTime:" + LocalDateTime.ofInstant(instant1, ZoneId.systemDefault()));
网友评论