Java Collections

举报
福州司马懿 发表于 2021/11/19 04:09:43 2021/11/19
【摘要】 Collections是Java中操作集合的一个工具类,主要方法有以下10种: 排序(Sort) 混排(Shuffling) 反转(Reverse) 拷贝(Copy) 替换所以的元素(Fill)返回Co...

Collections是Java中操作集合的一个工具类,主要方法有以下10种:

  1. 排序(Sort)
  2. 混排(Shuffling)
  3. 反转(Reverse)
  4. 拷贝(Copy)
  5. 替换所以的元素(Fill)
  6. 返回Collections中最小元素(min)
  7. 返回Collections中最小元素(max)
  8. 查找子列表第一次出现的位置(IndexOfSubList)
  9. 查找子列表最后一次出现的位置(lastIndexOfSubList)
  10. 循环转动(Rotate)
package com.demo.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class CollectionsDemo {

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("China");
        list.add("America");
        list.add("Russia");
        list.add("Japan");
        list.add("china");
        list.add("america");
        list.add("russia");
        list.add("japan");
        System.out.println("test list is " + list);

        /*
        (1) 排序(Sort) 
        使用sort方法可以根据元素的自然顺序 对指定列表按升序进行排序。列表中的所有元素都必须实现 Comparable 接口。此列表内的所有元素都必须是使用指定比较器可相互比较的 
         */
        //java7的排序方式
        Collections.sort(list, new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        System.out.println("sort = " + list);

        //java8的排序方式
        Collections.sort(list, (o1,o2) -> -o1.compareTo(o2));
        System.out.println("sort = " + list);

        /*
        (2) 混排(Shuffling) 
        混排算法所做的正好与 sort 相反: 它打乱在一个 List 中可能有的任何排列的踪迹。也就是说,基于随机源的输入重排该 List, 这样的排列具有相同的可能性(假设随机源是公正的)。这个算法在实现一个碰运气的游戏中是非常有用的。例如,它可被用来混排代表一副牌的 Card 对象的一个 List 。另外,在生成测试案例时,它也是十分有用的。
         */
        Collections.shuffle(list);
        System.out.println("shuffle = " + list);

        /*
        (3) 反转(Reverse) 
                 使用Reverse方法可以根据元素的自然顺序 对指定列表按降序进行排序。
         */
        Collections.reverse(list);
        System.out.println("reverse = " + list);

        /*
        (4) 拷贝(Copy) 
        用两个参数,一个目标 List 和一个源 List, 将源的元素拷贝到目标,并覆盖它的内容。目标 List 至少与源一样长。如果它更长,则在目标 List 中的剩余元素不受影响。
        Collections.copy(dest, src): 前一个是目标列表(填充到该表),后一个是源列表(用来填充)
        注意:dest中不能没有元素(即不能只是new一个空的list,即使声明了大于src的长度也不可以), dest中一定要有大于等于src的元素
        否则报错:java.lang.IndexOutOfBoundsException: Source does not fit in dest
         */

        ArrayList<String> destList = new ArrayList<String>(list.size()+1);
        for(int i=0; i<list.size()+1; i++) {
            destList.add("xxx");
        }
        Collections.copy(destList, list);
        System.out.println("copy = " + destList);

        /*
        (5) 替换所以的元素(Fill) 
         使用指定元素替换指定列表中的所有元素。
         */
        Collections.fill(destList, "xxx");
        System.out.println("fill = " + destList);

        /*
        (6) 返回Collections中最小元素(min) 
        根据指定比较器产生的顺序,返回给定 collection的最小元素。collection中的所有元素都必须是通过指定比较器可相互比较的 
         */
        String minValue = Collections.min(list, (o1,o2) -> o1.compareTo(o2));
        System.out.println("min = " + minValue);

        /*
        (7) 返回Collections中最小元素(max) 
        根据指定比较器产生的顺序,返回给定 collection的最大元素。collection中的所有元素都必须是通过指定比较器可相互比较的 
         */
        String maxValue = Collections.max(list, (o1,o2) -> o1.compareTo(o2));
        System.out.println("max = " + maxValue);

        ArrayList<String> newList = new ArrayList<String>();
        newList.add("hello");
        newList.add("world");
        newList.add("java");
        newList.add("hello");
        newList.add("world");
        ArrayList<String> subList = new ArrayList<String>();
        subList.add("hello");
        subList.add("world");
        System.out.println("test list is " + newList);
        System.out.println("test sublist is " + subList);

        /*
        (8) 查找子列表第一次出现的位置(IndexOfSubList)
        返回指定源列表中第一次出现指定目标列表的起始位置 
         */
        int index = Collections.indexOfSubList(newList, subList);
        System.out.println("indexOfSubList = " + index);

        /*
        (9) 查找子列表最后一次出现的位置(lastIndexOfSubList)
        返回指定源列表中最后一次出现指定目标列表的起始位置 
         */
        int lastIndex = Collections.lastIndexOfSubList(newList, subList);
        System.out.println("lastIndexOfSubList = " + lastIndex);

        /*
        (10) 循环转动(Rotate) 
         根据指定的距离循环移动指定列表中的元素。若是负数则向左移动,正数则向右移动 
         */
        Collections.rotate(list, -2);
        System.out.println("rotate = " + list);
    }
}
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122

运行截图
这里写图片描述

文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/chy555chy/article/details/52153071

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。