Java爬坑--集合(一)Collection

下午测试上线,闲来没事,然后继续我的爬坑之路.

 
  
 1 import java.util.ArrayList;
 2 import java.util.Collection;
 3 import java.util.Iterator;
 4 import java.util.Vector;
 5 
 6 /**
 7  * Created by Q_先森 on 2017/12/5.
 8  */
 9 public class CollectionReview {
10     public static void main(String[] args) {
11         test();
12     }
13 
14     private static void test() {
15         // Collection<E>为接口,使用多态进行创建
16         Collection<String> collection = new Vector<>();
17         collection.add("德玛西亚之力");
18         collection.add("诺克萨斯之手");
19         collection.add("卡特琳娜");
20 
21         Collection<String> coll = new ArrayList();
22         coll.add("卡牌大师");
23         coll.add("光辉女郎");
24         coll.add("蛮族之王");
25 
26         // collection.clear(); // 清空集合
27         // boolean empty = collection.isEmpty(); // 集合是否为空
28         // System.out.println(collection.isEmpty());
29 
30         // int size = collection.size(); // 获取集合大小
31         // System.out.println(size);
32 
33         // boolean contains = collection.contains("阿木木"); // 是否包含另一个元素
34         // System.out.println(contains);
35 
36         // boolean containsAll = collection.containsAll(coll); //是否完全包含另一个集合
37         // System.out.println(containsAll);
38 
39         // boolean remove = collection.remove("卡特琳娜");// 删除第一个匹配项,删除了匹配项则返回true
40         // boolean removeAll = collection.removeAll(coll);  // 删除与指定集合有交集的部分,原集合有改变就返回true
41         // System.out.println(removeAll);
42 
43         // boolean retainAll = collection.retainAll(coll);// 保留与指定集合有交集的部分,原集合有改变就返回true
44         // System.out.println(retainAll);
45 
46         // 方式1 (后面会研究一下具体内容,这里只做展示)
47         Iterator<String> iterator = collection.iterator();
48         while (iterator.hasNext()) {
49             System.out.println(iterator.next());
50         }
51         System.out.println("--------割-----------");
52 
53         // 方式2 ,for循环完iter就会被销毁,节约内存提高效率
54         for (Iterator<String> iter = collection.iterator(); iter.hasNext(); ) {
55             System.out.println(iter.next());
56 
57         }
58         System.out.println("--------割-----------");
59 
60         Object[] array = collection.toArray();  // 转化为object数组
61         for (Object string : array) {
62             System.out.println(string);
63         }
64         System.out.println("--------割-----------");
65 
66         String[] arr = new String[collection.size()];
67         String[] array2 = collection.toArray(arr);  // 指定要转化的数组类型
68         for (String string : array2) {
69             System.out.println(string);
70         }
71         System.out.println("--------割-----------");
72 
73         // Stream(JAVA 8) 大家可以把Stream当成一个高级版本的Iterator。这里只举了一个例子,后面会详细研究一下
74         System.out.println(collection.stream().count()); // 3
75         System.out.println(collection.stream().filter(str -> !str.equals("卡特琳娜")).count()); // 过滤掉'卡特琳娜' 2
76         System.out.println("--------割-----------");
77 
78         // parallelStream其实就是一个并行执行的流.它通过默认的ForkJoinPool,可能提高你的多线程任务的速度.
79         collection.parallelStream().forEach(System.out::println);// 打印顺序不一样,不多做解释,待后面慢慢来研究
80         System.out.println("--------割-----------");
81 
82         collection.removeIf(str -> str.startsWith("德玛")); // 移除集合中满足给定条件的所有元素
83         collection.forEach(System.out::println);
84         System.out.println("--------割-----------");
85 
86         // 这里只举了一个例子,后面会详细研究一下
87         collection.retainAll(coll); // 求交集(1.首先调用retainAll的方法 2、通过判断集合的大小,来确定是否存在交集。)
88         if (collection.size() > 0) {
89             System.out.println("这两个集合有相同的交集");
90         } else {
91             System.out.println("这两个集合没有相同的交集");
92         }
93         System.out.println("--------割-----------");
94 
95 
96     }
97 
98 }
 
  

 

 

 

这是接口Collection的一些总结,后面还会陆续细化,爬坑路上难免出错,欢迎各位道友纠错与指正,共同进步.

转载于:https://www.cnblogs.com/chuzihang/p/7988400.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值