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 婆婆高寿祝福语大全简短