码迷,mamicode.com
首页 > 其他好文 > 详细

基础总结/集合/collection总结

时间:2020-03-30 16:05:12      阅读:63      评论:0      收藏:0      [点我收藏+]

标签:接口   顺序   size   ima   mamicode   als   list   method   split   

1.Collection接口

  • Collection是一个根接口,不提供具体的实现,作为子接口的父类使用,由子接口提供实现类
  • 1.5之后集合会使用泛型,即规定放入集合中的元素类。如果不设置泛型,则放入集合中元素的数据类型会丢失。都不会变成Object类型
2.Collection接口继承体系
技术图片
3.常用方法(API)
可以先查下collection有哪些共有方法
    // 查Collection公用的方法
    Class c = Collection.class;
    Method[] methods = c.getMethods();
    int n = 1;
    for (Method method : methods) {
      System.out.print(n++ + " " + method.getName() + " ");
   }

查到20个(含重写方法)

1 add 2 remove 3 equals 4 hashCode 5 clear 6 contains 7 isEmpty 8 iterator 9 size 10 toArray 11 toArray 12 spliterator 13 addAll 14 stream 15 containsAll 16 removeAll 17 removeIf 18 retainAll 19 parallelStream 20 forEach 

下面每个方法使用一次

               // 1、add(E e) 添加
        System.out.println("________1________");
        Collection<String> col = new ArrayList<>();
        col.add("Jack");
        System.out.println(col);
        // 2、remove 删除
        System.out.println("________2________");
        col.remove("Jack");
        System.out.println(col);
        // 3、equals 判断集合相同(个数相同,位置相同)
        System.out.println("________3________");
        Collection<String> col2 = new ArrayList<String>();
        col.add("Jack");
        col2.add("Jack");
        System.out.println(col.equals(col2));
        // 4、hashCode hashCode值
        System.out.println("________4________");
        System.out.println(col.hashCode());
        // 5、clear 清除
        System.out.println("________5________");
        col.clear();
        System.out.println(col);
        // 6、contains 是否包含
        System.out.println("________6________");
        col.add("Jack");
        System.out.println(col.contains("Jack"));
        // 7、isEmpty 判断是否为空,空返回true
        System.out.println("________7________");
        System.out.println(col.isEmpty());
        // 8、iterator 迭代器
        System.out.println("________8________");
        col.add("Tom");
        col.add("Alice");
        System.out.println(col);
        Iterator<String> iter = col.iterator();
        System.out.println(iter.next());
        // 9、size 获取大小 
        System.out.println("________9________");
        System.out.println(col.size());
        // 10、toArray() 将集合转换为数组
        System.out.println("________10________");
        Object[] objs = col.toArray();
        for (int i = 0; i < objs.length; i++) {
            System.out.print(objs[i] + " ");
        }
        System.out.println();
        // 11、toArray(T[] a) 将集合转换为数组
        System.out.println("________11________");
        String[] strs = col.toArray(new String[] {});
        for (String string : strs) {
            System.out.println(string);
        }
        // 12、spliterator 可分割迭代器//了解
        System.out.println("________12________");
        Spliterator<String> split = col.spliterator();
        System.out.println(split.SIZED);// 返回特征值
        // 13、addAll(Collection<? extends E> c) 将集合中的所有元素放入指定的集合中
        System.out.println("________13________");
        col.forEach(u -> System.out.println(u));
        System.out.println("__");
        col2.forEach(u -> System.out.println(u));
        System.out.println("__");
        col2.addAll(col);
        col2.forEach(u -> System.out.println(u));
        // 14、stream //流 —— 计算空字符串
        System.out.println("________14________");
        Collection<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        System.out.println("列表: " + strings);
        long count = strings.stream().filter(string -> string.isEmpty()).count();
        System.out.println("空字符串数量为: " + count);
        // 15、containsAll(Collection<?> c)  判断所有元素是否相同, 只看元素, 不看顺序
        System.out.println("________15________");
        System.out.println(col.containsAll(col2));
        // 16、public boolean removeAll(Collection<?> c)  差集
        System.out.println("________16________");
        col.removeAll(col2);
        System.out.println(col);
        // 17、removeIf 过滤集合中的元素
        System.out.println("________17________");
        System.out.println(col2);
        col2.removeIf(name -> name.length() < 4);// 删掉名字长度小于4的
        System.out.println(col2);
        // 18、retainAll(Collection<?> c) 交集
        System.out.println("________18________");
        System.out.println(col2.retainAll(col));
        System.out.println(col2);
        // 19、parallelStream//了解
        System.out.println("________19________");
        Collection<String> strings2 = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        System.out.println("列表: " + strings);
        long count2 = strings.parallelStream().filter(string -> string.isEmpty()).count();
        System.out.println("空字符串数量为: " + count);
        // 20、forEach 遍历
        System.out.println("________20________");
        strings2.forEach(u -> System.out.println(u));
    }        

输出结果:

________1________
[Jack]
________2________
[]
________3________
true
________4________
2300958
________5________
[]
________6________
true
________7________
false
________8________
[Jack, Tom, Alice]
Jack
________9________
3
________10________
Jack Tom Alice 
________11________
Jack
Tom
Alice
________12________
64
________13________
Jack
Tom
Alice
__
Jack
__
Jack
Jack
Tom
Alice
________14________
列表: [abc, , bc, efg, abcd, , jkl]
空字符串数量为: 2
________15________
true
________16________
[]
________17________
[Jack, Jack, Tom, Alice]
[Jack, Jack, Alice]
________18________
true
[]
________19________
列表: [abc, , bc, efg, abcd, , jkl]
空字符串数量为: 2
________20________
abc

bc
efg
abcd

jkl

 

以上。

基础总结/集合/collection总结

标签:接口   顺序   size   ima   mamicode   als   list   method   split   

原文地址:https://www.cnblogs.com/jzdbwdc/p/12598994.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!