Java 8中流和集合之间的区别
JavaCollections框架用于存储和处理数据组。它是一个内存中的数据结构,应先计算集合中的每个元素,然后才能将其添加到集合中。
StreamAPI仅用于处理数据组。它不会修改实际的集合,它们仅根据流水线方法提供结果。
Itisusedforstoringandmanipulatinggroupofdata
AlltheclassesandinterfacesofthisAPIisintheJava.utilpackage
Alltheelementsinthecollectionsarecomputedinthebeginning.
Incollections,wecanremoveoraddelements.
Collectionsperformiterationoverthecollection.
集合范例
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); } }