①. Stream 流的常见生产方式
public class StreamDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
Stream<String> listStream = list.stream();
Set<String> set = new HashSet<String>();
Stream<String> setStream = set.stream();
Map<String,Integer> map = new HashMap<String, Integer>();
Stream<String> keyStream = map.keySet().stream();
Stream<Integer> valueStream = map.values().stream();
Stream<Map.Entry<String, Integer>> entryStream = map.entrySet().stream();
String[] strArray = {"hello","world","java"};
Stream<String> strArrayStream = Stream.of(strArray);
Stream<String> strArrayStream2 = Stream.of("hello", "world", "java");
Stream<Integer> intStream = Stream.of(10, 20, 30);
}
}
②. forEach Consumer
-
①. 然方法名字叫forEach ,但是与for循环中的“for-each”昵称不同 void forEach(Consumer<? super T> action); -
②. java.util.function.Consumer<T> 接口是一个消费型接口。Consumer接口中包含抽象方法void accept(T t),意为消费一个指定泛型的数据。
Stream<String> st1 = Stream.of("张三","李四","王二","麻子");
st1.forEach(System.out::println);
③. filter Predicate
-
①. Stream<T> filter?(Predicate predicate): 用于对流中的数据进行过滤 -
②. Predicate接口中的方法 boolean test?(T t): 对给定的参数进行判断,返回一个布尔值 -
③. java.util.stream.Predicate函数式接口唯一的抽象方法为boolean test(T t);,该方法将会产生一个boolean值结果,代表指定的条件是否满足。如果结果为true,那么Stream流的filter方法将会留用元素;如果结果为false,那么filter方法将会舍弃元素
public class StreamDemo01 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("林青霞");
list.add("张曼玉");
list.add("王祖贤");
list.add("柳岩");
list.add("张敏");
list.add("张无忌");
list.stream().filter(s -> s.startsWith("张")).forEach(System.out::println);
System.out.println("--------");
list.stream().filter(s -> s.length() == 3).forEach(System.out::println);
System.out.println("--------");
list.stream().filter(s -> s.startsWith("张")).filter(s -> s.length() == 3).forEach(System.out::println);
}
}
④. map Function
-
①. 如果需要将流中的元素映射到另一个流中,可以使用map方法,方法签名: <R> Stream<R> map(Function<? super T, ? extends R> mapper); -
②. 该接口需要一个Function函数式接口,可以将当前流中的T类型数据转换为另一种R类型的流 此前我们已经学习过 java.util.stream.Function 函数式接口,其中唯一的抽象方法为:R apply(T t);,这可以将一种T类型转换成为R类型,而这种转换的动作,就称为“映射”
Stream.of("1","2","3","4","5","6")
.map(Integer::parseInt)
.forEach(s -> System.out.println(s*10));
⑤. limit 和 skip
-
①.Stream<T> limit?(long maxSize) :返回此流中的元素组成的流,截取前指定参数个数的数据 -
②.Stream<T> skip?(long n): 跳过指定参数个数的数据,返回由该流的剩余元素组成的流
public class StreamDemo02 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("林青霞");
list.add("张曼玉");
list.add("王祖贤");
list.add("柳岩");
list.add("张敏");
list.add("张无忌");
list.stream().limit(3).forEach(System.out::println);
System.out.println("--------");
list.stream().skip(3).forEach(System.out::println);
System.out.println("--------");
list.stream().skip(2).limit(2).forEach(System.out::println);
}
}
⑥. concat 、distinct
-
①. static <T> Stream<T> concat?(Stream a, Stream b): 合并a和b两个流为一个流 -
②. Stream<T> distinct?(): 返回由该流的不同元素(根据Object.equals(Object) )组成的流
public class StreamDemo03 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("林青霞");
list.add("张曼玉");
list.add("王祖贤");
list.add("柳岩");
list.add("张敏");
list.add("张无忌");
Stream<String> s1 = list.stream().limit(4);
Stream<String> s2 = list.stream().skip(2);
Stream.concat(s1,s2).distinct().forEach(System.out::println);
}
}
⑦. sorted? 排序
-
①. Stream<T> sorted?(): 返回由此流的元素组成的流,根据自然顺序排序 -
②. Stream<T> sorted?(Comparator comparator): 返回由该流的元素组成的流,根据提供的Comparator进行排序Comparator接口中的方法 int compare?(T o1, T o2)
public class StreamDemo04 {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("linqingxia");
list.add("zhangmanyu");
list.add("wangzuxian");
list.add("liuyan");
list.add("zhangmin");
list.add("zhangwuji");
list.stream().sorted((s1,s2) -> {
int num = s1.length()-s2.length();
int num2 = num==0?s1.compareTo(s2):num;
return num2;
}).forEach(System.out::println);
}
}
⑧. collect
-
①. toList:将流中的元素放置到一个列表集合中去。这个列表默认为ArrayList -
②. toSet:将流中的元素放置到一个无序集set中去。默认为HashSet。 -
③. joining:joining的目的是将流中的元素全部以字符序列的方式连接到一起,可以指定连接符,甚至是结果的前后缀
public class CollectorsTest {
public static void toListTest(List<String> list) {
List<String> ll = list.stream().collect(Collectors.toList());
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
toListTest(list);
}
}
public class CollectorsTest {
public static void toSetTest(List<String> list) {
Set<String> ss = list.stream().collect(Collectors.toSet());
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
toSetTest(list);
}
}
public class CollectorsTest {
public static void joiningTest(List<String> list){
String s = list.stream().collect(Collectors.joining());
System.out.println(s);
String ss = list.stream().collect(Collectors.joining("-"));
System.out.println(ss);
String sss = list.stream().collect(Collectors.joining("-","S","E"));
System.out.println(sss);
}
public static void main(String[] args) {
List<String> list = Arrays.asList("123","456","789","1101","212121121","asdaa","3e3e3e","2321eew");
joiningTest(list);
}
}
⑨. 题目实战
public class StreamDemo {
public static void main(String[] args) {
User u1 = new User(11, "a", 23);
User u2 = new User(12, "b", 24);
User u3 = new User(13, "c", 22);
User u4 = new User(14, "d", 28);
User u5 = new User(16, "e", 26);
List<User> list = Arrays.asList(u1, u2, u3, u4, u5);
list.stream()
.filter(s->s.getId()%2==0)
.filter(s->s.getAge()>24)
.map(s->s.getUserName().toUpperCase())
.sorted((s1,s2)->s2.compareTo(s1))
.limit(1)
.forEach(System.out::print);
}
}
|