sm.forEach(System.out::println);
});
System.out.println("---------------------------");
Stream<Character> stream2 = list.stream().flatMap(TestStreamAPI3::filterCharacter);
stream2.forEach(System.out::println);
}
public static Stream<Character> filterCharacter(String str) {// add(Object obj) addAll(collection coll)
List<Character> list = new ArrayList<>();
for (Character character : str.toCharArray()) {
list.add(character);
}
return list.stream();
}
}
文章插图
文章插图
44、Stream排序
package com.stream.api4;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import com.stream.api1.Employee;
public class TestStreamAPI4 {
List<Employee> employees = Arrays.asList(new Employee("张三", 12, 1200.99), new Employee("小明", 15, 4500.99),
new Employee("小丽", 16, 5500.99), new Employee("王二", 32, 1100.99), new Employee("二虎", 22, 9825.99),
new Employee("李静", 18, 4502.99), new Employee("小三", 17, 1469.99), new Employee("小三", 17, 1469.99),
new Employee("小三", 17, 1469.99), new Employee("小三", 17, 1469.99));
/**
* 排序 sorted() —— 天然排序(Comparable) sorted(Comparater com) —— 定制排序(Comparater)
*/
@Test
public void test1() {
List<String> list = Arrays.asList("ccc", "aaa", "bbb", "ddd", "eee");
list.stream().sorted().forEach(System.out::println);
System.out.println("--------------------------------------------");
employees.stream().sorted((e1, e2) -> {
if (e1.getAge() == e2.getAge()) {
return e1.getName().compareTo(e2.getName());
} else {
return Integer.compare(e1.getAge(), e2.getAge());
}
}).forEach(System.out::println);
}
}
文章插图
55、Stream查找与匹配
package com.stream.api5;
import java.util.Arrays;
【java jdk1.8新特性Stream API】import java.util.List;
import java.util.Optional;
import org.junit.Test;
import com.stream.api1.Employee;
import com.stream.api1.Employee.Status;
public class TestStreamAPI5 {
List<Employee> employees = Arrays.asList(new Employee("张三", 12, 1200.99, Status.BUSY),
new Employee("小明", 15, 4500.99, Status.BUSY), new Employee("小丽", 16, 5500.99, Status.BUSY),
new Employee("王二", 32, 1100.99, Status.FREE), new Employee("二虎", 22, 9825.99, Status.FREE),
new Employee("李静", 18, 4502.99, Status.FREE), new Employee("小三", 17, 1469.99, Status.VOCATION),
new Employee("小三", 17, 1469.99, Status.VOCATION), new Employee("小三", 17, 1469.99, Status.VOCATION),
new Employee("小三", 17, 1469.99, Status.VOCATION));
推荐阅读
- java怎么发短信
- 如何选择JAVA学习书籍
- Java中怎么设置日期
- 怎么把JSON写入下拉框
- Java String 方法
- 使用JavaScript制作一个级联菜单
- JAVA JDK安装
- Java语言有哪些优势,为什么要学习java技术
- JAVA编程不得不看的几本经典书籍
- java学习路线