• 浅谈集合框架五——集合框架扩展:Collections工具类的使用,自定义比较器


    最近刚学完集合框架,想把自己的一些学习笔记与想法整理一下,所以本篇博客或许会有一些内容写的不严谨或者不正确,还请大神指出。初学者对于本篇博客只建议作为参考,欢迎留言共同学习。

      之前有介绍集合框架的体系概念(http://www.cnblogs.com/yjboke/p/8761195.html),本篇介绍一下集合扩展内容:Collections工具类的使用;可变参数;集合数组相互转换方法;


    Collection与Collections的区别:

    Collection是java.util下的一个接口,是List、Set等接口的父接口,

    Collections是针对集合类的一个帮助类,提供了一系列静态方法实现对集合的搜索、排序、同步等操作,接下来将基本用法进行简单介绍:

    一、搜索及替换(主要针对于Collection相关接口):

      max(Collection c);  返回最大值;

      min(Collection c);   返回最小值;

      fill(List list,Object obj);将list集合中所有元素替换为指定元素(obj);

      frequency(Collection Object o);返回指定集合中指定对象出现的次数;

      replaceAll(list,Object obj,Object newObj);将指定元素替换为新元素;

      copy(List list1,List list2);将集合List2中的元素全部复制到list1中,并覆盖其相应索引的元素。

      binarySearch(List list ;Object obj);根据元素找下标,需先用sort进行排序,如果没有该元素返回负值);

    二、排序(主要针对于List相关接口):

      sort(List list);对list里的元素进行自然升序排序;

      reverse(List list);反转排序;

      shuffle(List list);对集合中的元素进行随机排序;

      swap(List list,int i1,int i2);交换两个下标的位置;

      rotate(List list,Int distance);将所有元素向右移位指定长度,如distance等于size则不变。

    三、同步

      synchronizedCollection 返回指定的Collection支持的同步的collection;

      也可应用于Set、Map、List。

    四、将集合设置为不可变

      emptyList();返回一个空的不可变得集合对象;

      singletonList();返回一个只包含指定对象的,不可变的集合对象;

      unmodiflableList();返回指定集合对象的不可变视图;

    五、比较器

    Comparator; 实现Comparator接口,复写compare方法,可自定义比较器。

    -------------------------------------------------------------

    以下将挑几个方法进行代码实例:

    首先是普通的排序或替换方法的代码:

    public static void main(String[] args) {
            sortDemo();
            StrSortDemo();
        }
        
        public static void sortDemo() {
            //定义集合并添加元素
            List<String> list = new ArrayList<String>();
            list.add("abc");
            list.add("aaa");
            list.add("bcde");
            list.add("bc");
            //原顺序输出   
            System.out.println(list);        //输出结果为:[abc, aaa, bcde, bc]
            
            //使用sort方法排序
            Collections.sort(list);
            //输出排序后的集合   
            System.out.println(list);        //输出结果为:[aaa, abc, bc, bcde]
            
            //根据元素“bcde”找下标
            int i = Collections.binarySearch(list, "bcde");
            System.out.println(i);            //输出结果为:3
            
            //反转排序
            Collections.reverse(list);
            System.out.println(list);        //输出结果为:[bcde, bc, abc, aaa]
            
            //交换下标位置
            Collections.swap(list, 2, 3);
            System.out.println(list);        //输出结果为:[bcde, bc, aaa, abc]
            
            //输出最大值
            String max = Collections.max(list);
            System.out.println(max);            //输出结果为:bcde
            
            //将集合中所有元素都替换为over
            Collections.fill(list, "over");    
            System.out.println(list);        //输出结果为:[over, over, over, over]
            
        }

      接下来是复写比较器方法,比较元素长度长短:

    class StrComparator implements Comparator<String>{
        
        public int compare(String s1, String s2) {
            
            if(s1.length() > s2.length()) {
                return 1;
            }
            if(s1.length() == s2.length()) {
                //如果字符串长度相同则比较内容
                return s1.compareTo(s2);
            }
            return -1;
        }
        
    }

      加入长度比较器之后的排序方法:

        public static void StrSortDemo() {
            //定义集合并添加元素
            List<String> list = new ArrayList<String>();
            list.add("abc");
            list.add("aaaaa");
            list.add("bcde");
            list.add("bc");
            
            //根据字符串长度进行排序
            Collections.sort(list,new StrComparator());
            System.out.println(list);        //输出结果为:[bc, abc, bcde, aaaaa]
            
            String max = Collections.max(list,new StrComparator());
            System.out.println(max);            //输出结果为:aaaaa
        }

    以上。

    PS:如果不严谨之处还请提出,共同学习。

  • 相关阅读:
    C语言ASM汇编内嵌语法
    Linux下安装安装arm-linux-gcc
    苹果手机(ios系统)蓝牙BLE的一些特点
    蓝牙BLE数据包格式汇总
    蓝牙BLE4.0的LL层数据和L2CAP层数据的区分与理解
    nrf52840蓝牙BLE5.0空中数据解析
    nrf52840蓝牙BLE5.0空中速率测试(nordic对nordic)
    nrf52832协议栈S132特性记录
    使用 Open Live Writer 创建我的第一个博文
    Codeforces Round #691 (Div. 2) D
  • 原文地址:https://www.cnblogs.com/yjboke/p/8908816.html
Copyright © 2020-2023  润新知