Lambda练习
虽然java8已经很普遍了,但是它的其中之一特性——Lambda我很少用,甚至是不会用。今天查资料时,无意发现一遍博客,写的不错,我就练习了会!
一、相关博文
Java8之Lambda表达式概念篇:https://www.jianshu.com/p/67ebee04b2b3
Java8之Stream类:https://www.jianshu.com/p/c53eb31752c4
二、涉及到的实体类
public class User { @Setter @Getter String userName; @Setter @Getter String passWord; @Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof User)) { return false; } User user = (User) obj; Boolean flag = Objects.equals(userName, user.userName) && Objects.equals(passWord, user.passWord); return flag; } @Override public int hashCode() { return Objects.hash(passWord, userName); } public User(String userName, String passWord) { this.userName = userName; this.passWord = passWord; } @Override public String toString() { return "User{" + "userName='" + userName + '\'' + ", passWord='" + passWord + '\'' + '}'; } }
三、各种功能
static List<User> userList = new ArrayList<User>() { { add(new User("a", "b")); add(new User("a1", "b1")); add(new User("a222", "b2")); add(new User("a", "bb")); add(new User("a", "bbc")); add(new User("a3", "b1")); add(new User("a44", "b2")); } };
1、forEach两种遍历方式
@Test public void forEachTest() { System.out.println("forEach遍历方式一"); userList.forEach(s -> System.out.print(s + " ")); System.out.println("forEach遍历方式二"); userList.forEach(System.out::println); }
2、排序
@Test public void sortTest() { String[] players = {"1", "3", "2", "0"}; System.out.println("排序前"); List<String> list = Arrays.asList(players); list.forEach(s -> System.out.print(s + " ")); Arrays.sort(players, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.compareTo(o2); } }); System.out.println("\n排序后"); List<String> list1 = Arrays.asList(players); list1.forEach(s -> System.out.print(s + " ")); }
3、调用setter、getter
@Test public void setterGetterTest() { userList.forEach(s -> s.setUserName(s.getUserName() + " --123-- ")); userList.forEach(System.out::print); }
4、过滤器
@Test public void filterTest() { userList.stream() .filter(p -> p.getUserName().equals("a")) .limit(1) .forEach(p -> System.out.println(p.getPassWord() + " ")); userList.stream() .filter(p -> p.getUserName().equals("a")) .forEach(p -> System.out.println(p.getPassWord() + " ")); }
5、stream处理
@Test public void streamTest() { List<User> userList1 = userList.stream() .sorted((p1, p2) -> p1.getUserName().compareTo(p2.getUserName())) .limit(5) .collect(Collectors.toList()); userList1.forEach(p -> System.out.println(p + " ")); System.out.println(""); userList.stream() .sorted((p1, p2) -> p1.getUserName().compareTo(p2.getUserName())) .limit(10) .collect(Collectors.toList()) .forEach(System.out::println); }
6、max
@Test public void maxTest() { User maxUser = userList.stream() .max((p1, p2) -> p1.getUserName().length() - p2.getUserName().length()) .get(); System.out.println(maxUser); }
7、min
@Test public void minTest() { User minUser = userList.stream() .min((p1, p2) -> p1.getUserName().length() - p2.getUserName().length()) .get(); System.out.println(minUser); }
8、sum
@Test public void sumTest() { int totalSalary = userList.parallelStream() .mapToInt(p -> p.getUserName().length()) .sum(); System.out.println(totalSalary); // 输出:14 }
9、将list转化为String
@Test public void toStringTest() { String strUser = userList.stream() .map(User::getUserName) .collect(Collectors.joining(";")); System.out.println(strUser); // 输出:a;a1;a222;a;a;a3;a44 }
10、将list转化为Set
@Test public void toSetTest() { Set<String> setUser = userList.stream() .map(User::getUserName) .collect(Collectors.toSet()); setUser.forEach(s -> System.out.print(s + " ")); // 输出:a1 a44 a a3 a222 }
11、将list转化为TreeSet
@Test public void toTreeSetTest() { TreeSet<String> treeSet = userList.stream() .map(User::getUserName) .collect(Collectors.toCollection(TreeSet::new)); treeSet.forEach(s -> System.out.print(s + " ")); //输出:a a1 a222 a3 a44 }
12、统计
@Test public void test11() { List<Integer> intNum = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); IntSummaryStatistics summaryStatistics = intNum.stream() .mapToInt(x -> x).summaryStatistics(); System.out.println("总数:" + summaryStatistics.getCount()); System.out.println("平均:" + summaryStatistics.getAverage()); System.out.println("最大:" + summaryStatistics.getMax()); System.out.println("最小:" + summaryStatistics.getMin()); System.out.println("总计:" + summaryStatistics.getSum()); }
13、线程
@Test public void threadTest() { // 继承Thread的形式 new Thread(() -> System.out.println("我是线程的run方法")) .start(); // 实现runable的形式 Runnable r2 = () -> System.out.println("我也是线程的run方法"); r2.run(); }
参考:https://blog.csdn.net/qq_37722734/article/details/82148490
在全栈的道路上,积极向上、成熟稳重、谦虚好学、怀着炽热的心向前方的走得更远。