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 婆婆生日祝福语简短的