美文网首页Java
Java Collections

Java Collections

作者: 一亩三分甜 | 来源:发表于2019-09-14 23:36 被阅读0次

    集合框架的工具类:Collections。

    • Collections.sort(List<T> list)排序
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class CollectionsDemo {
        public static void main(String[] args) {
            sortDemo();
        }
        public static void sortDemo()
        {
            List<String> list = new ArrayList<String>();
            list.add("abcd");
            list.add("aaa");
            list.add("z");
            list.add("kkkkk");
            list.add("qq");
            list.add("z");
    
            sop(list);
            Collections.sort(list);
            sop(list);
        }
        public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
    //输出
    [abcd, aaa, z, kkkkk, qq, z]
    [aaa, abcd, kkkkk, qq, z, z]
    

    Collections.sort(List<T> list, Comparator<? super T> c)传入自定义比较器排序

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class CollectionsDemo0 {
        public static void main(String[] args) {
            sortDemo();
        }
        public static void sortDemo()
        {
            List<String> list = new ArrayList<String>();
            list.add("abcd");
            list.add("aaa");
            list.add("z");
            list.add("kkkkk");
            list.add("qq");
            list.add("z");
    
            sop(list);
            Collections.sort(list,new StrLenComparator());
            sop(list);
        }
        public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
    class StrLenComparator implements Comparator<String>
    {
        public int compare(String s1,String s2)
        {
            int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
            if (num == 0)
                return s1.compareTo(s2);
            return num;
        }
    }
    //输出
    [abcd, aaa, z, kkkkk, qq, z]
    [z, z, qq, aaa, abcd, kkkkk]
    
    • max(Collection<? extends T> coll, Comparator<? super T> comp)获取最大值
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class CollectionsDemo1 {
        public static void main(String[] args) {
            maxDemo();
        }
        public static void maxDemo()
        {
            List<String> list = new ArrayList<String>();
            list.add("abcd");
            list.add("aaa");
            list.add("z");
            list.add("kkkkk");
            list.add("qq");
            list.add("zz");
    
            sop(list);
            String maxString = Collections.max(list,new StrLenComparator());
            sop("max="+maxString);
        }
        public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
    class StrLenComparator0 implements Comparator<String>
    {
        public int compare(String s1,String s2)
        {
            int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
            if (num == 0)
                return s1.compareTo(s2);
            return num;
        }
    }
    //输出
    [abcd, aaa, z, kkkkk, qq, zz]
    max=kkkkk
    
    • binarySearch(List<? extends Comparable<? super T>> list, T key)二分查找,若集合未排序或元素不存在则返回负数。返回值为-(插入点)-1。元素存在返回角标索引。
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class CollectionsDemo2 {
        public static void main(String[] args) {
            binarySearchDemo();
        }
        public static void binarySearchDemo()
        {
            List<String> list = new ArrayList<String>();
            list.add("abcd");
            list.add("aaa");
            list.add("z");
            list.add("kkkkk");
            list.add("qq");
            list.add("zz");
            sop(list);
            int index = Collections.binarySearch(list,"aaa");
            sop("index="+index);
        }
        public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
    //输出
    [abcd, aaa, z, kkkkk, qq, zz]
    index=-1
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class CollectionsDemo2 {
        public static void main(String[] args) {
            binarySearchDemo();
        }
        public static void binarySearchDemo()
        {
            List<String> list = new ArrayList<String>();
            list.add("abcd");
            list.add("aaa");
            list.add("z");
            list.add("kkkkk");
            list.add("qq");
            list.add("zz");
            sop(list);
            int index = Collections.binarySearch(list,"ddddd");
            sop("index="+index);
        }
        public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
    //输出
    [abcd, aaa, z, kkkkk, qq, zz]
    index=-3
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class CollectionsDemo2 {
        public static void main(String[] args) {
            binarySearchDemo();
        }
        public static void binarySearchDemo()
        {
            List<String> list = new ArrayList<String>();
            list.add("abcd");
            list.add("aaa");
            list.add("z");
            list.add("kkkkk");
            list.add("qq");
            list.add("zz");
            Collections.sort(list);
            sop(list);
            int index = Collections.binarySearch(list,"aaa");
            sop("index="+index);
        }
        public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
    //输出
    [aaa, abcd, kkkkk, qq, z, zz]
    index=0
    

    二分查找原理

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class CollectionsDemo2 {
        public static void main(String[] args) {
            binarySearchDemo();
        }
        public static void binarySearchDemo()
        {
            List<String> list = new ArrayList<String>();
            list.add("abcd");
            list.add("aaa");
            list.add("z");
            list.add("kkkkk");
            list.add("qq");
            list.add("zz");
            Collections.sort(list);
            sop(list);
    //        int index = Collections.binarySearch(list,"aaa");
            int index = halfSearch(list,"ddddd");
            sop("index="+index);
        }
        public static int halfSearch(List<String>list,String key)
        {
            int max,min,mid;
            max = list.size()-1;
            min = 0;
            while (min<=max)
            {
                mid = (max+min)>>1;//相当于/2
                String str = list.get(mid);
                int num = str.compareTo(key);
                if (num>0)
                    max = mid - 1;
                else if(num < 0)
                    min = mid + 1;
                else
                    return mid;
            }
            return - min -1;
        }
    
        public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
    //输出
    [aaa, abcd, kkkkk, qq, z, zz]
    index=-3
    

    Collections.sort(List<T> list, Comparator<? super T> c)传入比较器

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class CollectionsDemo2 {
        public static void main(String[] args) {
            binarySearchDemo();
        }
        public static void binarySearchDemo()
        {
            List<String> list = new ArrayList<String>();
            list.add("abcd");
            list.add("aaa");
            list.add("z");
            list.add("kkkkk");
            list.add("qq");
            list.add("zz");
            Collections.sort(list,new StrLenComparator1());
            sop(list);
            int index = Collections.binarySearch(list,"ddddd",new StrLenComparator1());
            sop("index="+index);
        }
        public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
    //输出
    [z, qq, zz, aaa, abcd, kkkkk]
    index=-6
    

    传入比较器,修改二分查找的比较方式。不传默认是自然顺序。

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class CollectionsDemo2 {
        public static void main(String[] args) {
            binarySearchDemo();
        }
        public static void binarySearchDemo()
        {
            List<String> list = new ArrayList<String>();
            list.add("abcd");
            list.add("aaa");
            list.add("z");
            list.add("kkkkk");
            list.add("qq");
            list.add("zz");
            Collections.sort(list,new StrLenComparator1());
            sop(list);
            int index = halfSearch2(list,"ddddd",new StrLenComparator1());
            sop("index="+index);
        }
        public static int halfSearch2(List<String>list,String key,Comparator cmp)
        {
            int max,min,mid;
            max = list.size()-1;
            min = 0;
            while (min<=max)
            {
                mid = (max+min)>>1;//相当于/2
                String str = list.get(mid);
                int num = cmp.compare(str,key);
                if (num>0)
                    max = mid - 1;
                else if(num < 0)
                    min = mid + 1;
                else
                    return mid;
            }
            return - min -1;
        }
    
        public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
    class StrLenComparator1 implements Comparator<String>
    {
        public int compare(String s1,String s2)
        {
            int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
            if (num == 0)
                return s1.compareTo(s2);
            return num;
        }
    }
    //输出
    [z, qq, zz, aaa, abcd, kkkkk]
    index=-6
    
    • fill(List<? super T> list, T obj)方法
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class CollectionsDemo3 {
        public static void main(String[] args) {
            List<String> list = new ArrayList<String>();
            list.add("abcd");
            list.add("aaa");
            list.add("z");
            list.add("kkkkk");
            list.add("qq");
            list.add("zz");
            Collections.sort(list);
            sop(list);
            Collections.fill(list,"pp");
            sop(list);
        }
        public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
    //输出
    [aaa, abcd, kkkkk, qq, z, zz]
    [pp, pp, pp, pp, pp, pp]
    

    替换集合中部分元素

    import java.util.*;
    
    public class CollectionsDemo3 {
        public static void main(String[] args) {
            fillDemoFromTo(0,2);
        }
        public static void fillDemoFromTo(int start,int end)
        {
            List<String> list = new ArrayList<String>();
            list.add("abcd");
            list.add("aaa");
            list.add("z");
            list.add("kkkkk");
            list.add("qq");
            list.add("zz");
            Collections.sort(list);
            sop(list);
            ListIterator<String> it = list.listIterator();
            while (it.hasNext())
            {
                String str = it.next();
                int ind = list.indexOf(str);
                if (ind >= start && ind <= end)
                    list.set(ind,"pp");
            }
            sop(list);
        }
        public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
    //输出
    [aaa, abcd, kkkkk, qq, z, zz]
    [pp, pp, pp, qq, z, zz]
    
    • replaceAll(List<T> list, T oldVal, T newVal)
    import java.util.*;
    
    public class CollectionsDemo3 {
        public static void main(String[] args) {
              replaceAllDemo();
        }
        public static void replaceAllDemo()
        {
            List<String> list = new ArrayList<String>();
            list.add("abcd");
            list.add("aaa");
            list.add("z");
            list.add("kkkkk");
            list.add("qq");
            list.add("zz");
            Collections.sort(list);
            sop(list);
            Collections.replaceAll(list,"aaa","pp");
            sop(list);
        }
        public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
    //输出
    [aaa, abcd, kkkkk, qq, z, zz]
    [pp, abcd, kkkkk, qq, z, zz]
    
    • reverse(List<?> list)
    import java.util.*;
    
    public class CollectionsDemo3 {
        public static void main(String[] args) {
            reverseDemo();
        }
        public static void reverseDemo()
        {
            List<String> list = new ArrayList<String>();
            list.add("abcd");
            list.add("aaa");
            list.add("z");
            list.add("kkkkk");
            list.add("qq");
            list.add("zz");
            Collections.sort(list);
            sop(list);
            Collections.reverse(list);
            sop(list);
        }
        public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
    //输出
    [aaa, abcd, kkkkk, qq, z, zz]
    [zz, z, qq, kkkkk, abcd, aaa]
    
    • reverseOrder()
    import java.util.*;
    public class CollectionsDemo3 {
        public static void main(String[] args) {
            orderDemo();
        }
        public static void orderDemo()
        {
            TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder());
            ts.add("abcde");
            ts.add("aaa");
            ts.add("kkk");
            ts.add("zz");
            ts.add("ccc");
            Iterator<String> it = ts.iterator();
            while (it.hasNext())
            {
                System.out.println(it.next());
            }
        }
    }
    //输出
    zz
    kkk
    ccc
    abcde
    aaa
    

    reverseOrder(Comparator<T> cmp)传入比较器返回比较器

    import java.util.*;
    class StrLenComparator2 implements Comparator<String>
    {
        public int compare(String s1,String s2)
        {
            int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
            if (num == 0)
            {
                return s1.compareTo(s2);
            }
            return num;
        }
    }
    public class CollectionsDemo3 {
        public static void main(String[] args) {
              orderDemo0();
        }
        public static void orderDemo0()
        {
            TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder(new StrLenComparator2()));
            ts.add("abcde");
            ts.add("aaa");
            ts.add("kkk");
            ts.add("zz");
            ts.add("ccc");
            ts.add("b");
            Iterator<String> it = ts.iterator();
            while (it.hasNext())
            {
                System.out.println(it.next());
            }
        }
    }
    
    
    • swap(List<?> list, int i, int j)交换对应角标位置上的元素
    import java.util.*;
    public class CollectionsDemo3 {
        public static void main(String[] args) {
            swapDemo();
        }
        public static void swapDemo()
        {
            List<String> list = new ArrayList<String>();
            list.add("abcd");
            list.add("aaa");
            list.add("z");
            list.add("kkkkk");
            list.add("qq");
            list.add("zz");
    
            sop(list);
            Collections.swap(list,1,2);
            sop(list);
        }
       public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
    //输出
    [abcd, aaa, z, kkkkk, qq, zz]
    [abcd, z, aaa, kkkkk, qq, zz]
    
    • shuffleDemo(List<?> list)将list按照随机顺序重新排列。
    import java.util.*;
    public class CollectionsDemo3 {
        public static void main(String[] args) {
            shuffleDemo();
        }
        public static void shuffleDemo()
        {
            List<String> list = new ArrayList<String>();
            list.add("abcd");
            list.add("aaa");
            list.add("z");
            list.add("kkkkk");
            list.add("qq");
            list.add("zz");
    
            sop(list);
            Collections.shuffle(list);
            sop(list);
        }
       public static void sop(Object obj)
        {
            System.out.println(obj);
        }
    }
    //输出
    [abcd, aaa, z, kkkkk, qq, zz]
    [kkkkk, zz, abcd, z, qq, aaa]
    

    相关文章

      网友评论

        本文标题:Java Collections

        本文链接:https://www.haomeiwen.com/subject/mjhzyctx.html