Collection工具类

Collection工具类

  • 基本介绍

Collection工具类

 

  •  排序操作(均为static)

Collection工具类

 

 

package collection;

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

/**
 * @author 紫英
 * @version 1.0
 * @discription
 */
public class Collection01 {
    public static void main(String[] args) {
       List list = new ArrayList();
       list.add("a1");
       list.add("c22");
       list.add("b333");
       list.add("f4444");
        System.out.println("=====原list:=====");
        System.out.println(list);
        System.out.println("=====反转list:=====");
        Collections.reverse(list);
        System.out.println(list);
        System.out.println("=====随机排序list:=====");//可以抽奖啥的
        for (int i = 0; i < 5; i++) {
            Collections.shuffle(list);
            System.out.println(list);
        }
        System.out.println("=====自然排序list:=====");
        Collections.sort(list);
        System.out.println(list);
        System.out.println("=====指定compartor:=====");
        Collections.sort(list, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                //按照字符串长度排序
                return ((String)o1).length()-((String) o2).length();
            }
        });
        System.out.println(list);
        System.out.println("=====交换list:=====");
        Collections.swap(list,0,1);
        System.out.println(list);

    }
}
Collection工具类Collection工具类Collection工具类
  • 查找和替换

Collection工具类

 

 

package collection;

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

/**
 * @author 紫英
 * @version 1.0
 * @discription
 */
public class Collections02 {
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add("tom");
        list.add("jack");
        list.add("recorder");
        list.add("tom");
        System.out.println("=====原list:=====");
        System.out.println(list);
        System.out.println("=====list自然排序最大值:=====");
        System.out.println(Collections.max(list));
        System.out.println("=====list最大值(比较器):=====");
        Object max = Collections.max(list, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                //按照字符串长度排序
                return ((String)o1).length()-((String) o2).length();
            }
        });
        System.out.println(max);
        System.out.println("=====list自然排序最小值:=====");
        System.out.println(Collections.min(list));
        System.out.println("=====list排序最小值(比较器):=====");
        Object min = Collections.max(list, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                //按照字符串长度排序
                return ((String)o1).length()-((String) o2).length();
            }
        });
        System.out.println(min);
        System.out.println("=====list指定元素出现次数:=====");
        System.out.println(Collections.frequency(list,"tom"));
        System.out.println("=====复制的dest:=====");
        List dest = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            //为了保持两list大小一致来满足copy的条件
            // if (srcSize > dest.size())
            //            throw new IndexOutOfBoundsException("Source does not fit in dest");
            dest.add("");
        }
        Collections.copy(dest,list);
        System.out.println(dest);
        System.out.println("=====元素替换:=====");
        Collections.replaceAll(list,"tom","汤姆");
        System.out.println(list);
    }
}
Collection工具类

 


 

 

 

上一篇:java中Collection集合、Iterator迭代器接口、for each循环遍历集合或对象


下一篇:Collection常见工具类--CollectionUtil