List的一些使用技巧_2019-10-11

去除List的null元素

import java.util.*;

/**
 * @author NOknow
 * @version 1.0
 * @CreateDate 2019/10/11
 * @Desc list去除null
 */
class RemoveAllNullInList {
    public static void main (String[] args)	{
        List<String> stringList = new ArrayList<>(Arrays.asList("1", "2", null, "3", null));
        System.out.println("before removing, stringList = " + stringList);
        //before removing, stringList = [1, 2, null, 3, null]
        stringList.removeAll(Collections.singletonList(null));//去除所有null值
        System.out.println("after removing, stringList = " + stringList);
        //after removing, stringList = [1, 2, 3]
    }
}

List去重

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * @author NOknow
 * @version 1.0
 * @CreateDate 2019/10/11
 * @Desc list去重
 */
public class RemoveListDuplicates {
    public static void main(String[] args){
        List<String> list = Arrays.asList("a", "b", "a", "c", "c", "c");
        System.out.println("before---list = " + list);
        //before---list = [a, b, a, c, c, c]
        list = new ArrayList<>(new HashSet<>(list));
        System.out.println("after---list = " + list);
        //after---list = [a, b, c]
    }
}

這裏使用HashSet來去重,所以需要特別注意的是,去重的對象需要重寫equals和hashCode方法,這裏之所以能使用HashSet去重是因爲String已經重寫了equals和hashCode方法

二分法查詢

package studyjava;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author NOknow
 * @version 1.0
 * @CreateDate 2019/10/11
 * @Desc
 */
public class BinarySearchOfList {
    public static void main(String[] args) {
        Student targetStudent = new Student("Xiao Ming", 17);
        List<Student> list = Arrays.asList(
                new Student("Xiao Ming", 15),
                new Student("Wang Five", 20),
                targetStudent,
                new Student("Zhao Third", 21));
                
        //方法1:讓Student實現Comparable接口,並重寫compareTo方法
        int index = Collections.binarySearch(list, targetStudent);
        System.out.println("method 1, index = " + index);//method 1, index = 2
                
        //方法2:Student可以不用實現Comparable接口,改爲傳入一個Comparator並重寫其compare方法
        index = Collections.binarySearch(list, targetStudent, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int v = o1.getName().compareTo(o2.getName());
                return v == 0 ? o1.getAge() - o2.getAge() : v;
            }
        });
        /*JDK8及以上可以使用lamda表達式,更加簡潔
        index = Collections.binarySearch(list, targetStudent, (o1, o2) -> {
            int v = o1.getName().compareTo(o2.getName());
            return v == 0 ? o1.getAge() - o2.getAge() : v;
        });
        */
        System.out.println("method 2, index = " + index);//method 2, index = 2
    }
}

class Student implements Comparable<Student> {

    private String name;
    private Integer age;

    @Override
    public int compareTo(Student o) {
        int v = this.name.compareTo(o.getName());
        return v == 0 ? age - o.getAge() : v;
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
    
    //getter和setter方法

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(name, student.name) &&
                Objects.equals(age, student.age);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章