Java Collections collection tools summary

Collections Tools

Collections is an operating Collection (Set, List) and a set of tools and the like Map

Collection and Collections of the difference?

Collection of data is used to store single set of interfaces, sub-interfaces have common and Set List

Collections are tools Collection of operation.

Common method

Sort operation:

  • reverse (List): Reverse the order of the elements in List
  • shuffle (List): List of randomly ordered collection element
  • sort (List): according to the natural order of the elements of the specified collection element List Sort Ascending
  • sort (List, Comparator): List sort collection element according to the order specified Comparator produced
  • swap (List, int, int): element j at the i element of the list specifies a collection and exchange

Find and Replace:

  • Object max (Collection): according to the natural order of the elements, it is returned to the maximum element of the given set
  • Object max (Collection, Comparator): Comparator according to the order specified, returns the given set of the largest element
  • Object min(Collection)
  • Object min(Collection, Comparator)
  • int frequency (Collection, Object): Returns the specified number of occurrences of the specified element in the collection
  • void copy (List dest, List src): Copy the contents of src to dest.
  • boolean replaceAll (List list, Object oldVal, Object newVal): List all the old values ​​replaced by the new value of the object

For example:

reverse inversion method

@Test
public void test3() {
    // 测试reverse方法
    List list = new ArrayList();

    list.add(2121);
    list.add(-1);
    list.add(21);
    list.add(0);
    list.add(45);

    //反转
    Collections.reverse(list);
    System.out.println(list);//[45, 0, 21, -1, 2121]
}

sort method

@Test
public void test2() {
    // 测试sort方法
    List list = new ArrayList();

    list.add(2121);
    list.add(-1);
    list.add(21);
    list.add(0);
    list.add(45);

    //按升序排序
    Collections.sort(list);
    System.out.println(list);//[-1, 0, 21, 45, 2121]
}

Decommitment

int compareTo(T t)Method Description

Definition: Compare this object with the specified object

Returns: a negative integer, zero or a positive integer. If the object is less than, equal to or greater than the specified object, a negative integer, zero or a positive integer.

@Test
public void test1() {
    // 测试sort方法
    List list = new ArrayList();

    list.add(2121);
    list.add(-1);
    list.add(21);
    list.add(0);
    list.add(45);
    // 降序
    Collections.sort(list, new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            System.out.println(o2 - o1);
            return o2 - o1; // 此时o2-o1是大于0的
        }
    });
    System.out.println(list);//[-1, 0, 21, 45, 2121]
}

Sort of custom objects

Method a: compareTo method implemented by sorting the Comprable

// 实现Comparable接口
class User implements Comparable<User> {

    private String name;

    private Integer age;

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public int compareTo(User o) {

        //首先比较年龄,如果年龄相同,则比较名字
        int flag = this.getAge().compareTo(o.getAge());
        if (flag == 0) {
            return this.getName().compareTo(o.getName());
        } else {
            return flag;
        }
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

Test Methods

// 自定义对象的排序,比如对User对象按照年龄排序,再按照name排序
@Test
public void testSort() {
    User f1 = new User("tony", 19);
    User f2 = new User("jack", 16);
    User f3 = new User("tom", 80);
    User f4 = new User("jbson", 44);
    User f5 = new User("jason", 44);
    User f6 = new User("tom", 12);

    List<User> list = new ArrayList<User>();
    list.add(f1);
    list.add(f3);
    list.add(f4);
    list.add(f2);
    list.add(f5);
    list.add(f6);
    Collections.sort(list);

    for (User o : list) {
        System.out.println(o.getAge() + "-->" + o.getName());
    }
}

Output:

12-->tom
16-->jack
19-->tony
44-->jason
44-->jbson
80-->tom

copy method

List dest = Arrays.asList(new Object[list.size()]);

@Test
public void testCopy(){
    List list = new ArrayList();

    list.add(2121);
    list.add(-1);
    list.add(21);
    list.add(0);
    list.add(45);

    //错误写法:此时dest集合为长度为0,报错IndexOutOfBoundsException: Source does not fit in dest
//        List dest = new ArrayList();
//        Collections.copy(dest,list);
//        System.out.println(dest.size());
//        System.out.println(dest);
    // 正确写法:先造一个值为null,长度为list长度的集合。再拷贝
    List dest = Arrays.asList(new Object[list.size()]);
    System.out.println(dest.size()); // 5
    Collections.copy(dest,list);
    System.out.println(dest);
}

Guess you like

Origin www.cnblogs.com/benjieqiang/p/11442667.html