用Java将列表转换为流的程序
这是我们的带有字符串值的列表-
List<String> myList = Arrays.asList("One","Two","Three","Four", "Five");
现在,让我们将此列表转换为流-
Stream<String> s = myList.stream();
以下是在Java中将List转换为Stream的程序-
示例
import java.util.*; import java.util.stream.*; import java.util.function.Function; public class Demo { public static void main(String args[]) { List<String> myList = Arrays.asList("One","Two","Three","Four", "Five"); System.out.println("List: " + myList); Stream<String> s = myList.stream(); System.out.println("Stream (List to Stream) = " + Arrays.toString(s.toArray())); } }
输出结果
List: [One, Two, Three, Four, Five] Stream (List to Stream) = [One, Two, Three, Four, Five]