目录
- 去除List的null元素
- List去重
- 二分法查询
去除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);
}
}