1.根据list集合获取流对象
ArrayList<String> strings = new ArrayList<>();
strings.add("火麟诀");
strings.add("寒影诀");
strings.add("奔雷诀");
System.out.println(strings);
//可以list集合变成一个流对象
Stream<String> stream = strings.stream();
System.out.println(stream);//内存地址
2.根据Set集合获取流对象
Set<String> sets = new HashSet<>();
sets.add("闪灵诀");
sets.add("亡月劫");
sets.add("血印诀");
Stream<String> stream1 = sets.stream();
System.out.println(stream1);
3.根据map集合获取对象
import java.util.*;
import java.util.stream.Stream;
public class Demo2 {
public static void main(String[] args) {
ArrayList<String> strings = new ArrayList<>();
strings.add("火麟诀");
strings.add("寒影诀");
strings.add("奔雷诀");
System.out.println(strings);
//可以list集合变成一个流对象
Stream<String> stream = strings.stream();
System.out.println(stream);//内存地址
Set<String> sets = new HashSet<>();
sets.add("闪灵诀");
sets.add("亡月劫");
sets.add("血印诀");
Stream<String> stream1 = sets.stream();
System.out.println(stream1);
Map<String, String> maps = new HashMap<>();
maps.put("1", "幻天诀");
maps.put("2", "鬼影劫");
maps.put("3", "万恶劫");
//map集合没有stream方法,转为集合再来操作
Set<String> strings1 = maps.keySet();
Stream<String> stream2 = strings1.stream();
Collection<String> values = maps.values();
Stream<String> stream3 = values.stream();
Set<Map.Entry<String, String>> entries = maps.entrySet();
Stream<Map.Entry<String, String>> stream4 = entries.stream();
}
}
4.数组
String[] arr = {"a", "b"};
//数组也可以变成stream流形式的
Stream<String> arr1 = Stream.of(arr);