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);
}
}热门推荐
10 香港老妈结婚祝福语简短
11 毕业立体贺卡祝福语简短
12 简短新年年会祝福语
13 评论小品祝福语大全简短
14 恭喜师兄结婚祝福语简短
15 员工集体辞职祝福语简短
16 高中新生祝福语 简短
17 装修祝福语男生搞笑简短
18 生日开业蛋糕祝福语简短