Java Collections框架用于存储和处理数据组。它是一个内存中的数据结构,应先计算集合中的每个元素,然后才能将其添加到集合中。
Stream API仅用于处理数据组。它不会修改实际的集合,它们仅根据流水线方法提供结果。
| 序号 | 键 | 馆藏 | 流 |
|---|---|---|---|
| 1个 | 基本的 | It is used for storing and manipulating group of data | 流API仅用于处理数据组 |
| 2 | 包 | All the classes and interfaces of this API is in the Java.util package | 此API的所有类和接口都在java.util.stream包中 |
| 3 | 渴望/懒惰 | All the elements in the collections are computed in the beginning. | 在流中,中间操作是惰性的。 |
| 4。 | 资料修改 | In collections, we can remove or add elements. | 我们无法修改流。 |
| 5 | 外部/内部迭代器 | Collections perform iteration over the collection. | 流在内部执行迭代。 |
public class CollectiosExample {
public static void main(String[] args) {
List<String> laptopList = new ArrayList<>();
laptopList.add("HCL");
laptopList.add("Apple");
laptopList.add("Dell");
Comparator<String> com = (String o1, String o2)->o1.compareTo(o2);
Collections.sort(laptopList,com);
for (String name : laptopList) {
System.out.println(name);
}
}
}public class StreamsExample {
public static void main(String[] args) {
List<String> laptopList = new ArrayList<>();
laptopList.add("HCL");
laptopList.add("Apple");
laptopList.add("Dell");
laptopList.stream().sorted().forEach(System.out::println);
}
}