20201008

学习总结:

1.Collections

java.util.Collections 是 java 提供的一个集合操作的工具类。

如果提供的集合和对象是null都会空指针异常

static final List EMPTY_LIST;
//空List,不可变 //public static final List EMPTY_LIST = new EmptyList<>(); 
static final Map EMPTY_MAP ; 
//空Map,不可变 //public static final Map EMPTY_MAP = new EmptyMap<>(); 
static final Set EMPTY_SET ; 
//空Set,不可变 //public static final Set EMPTY_SET = new EmptySet<>();

image-20201009222553988

public static void main(String[] args) {
    List list = new ArrayList(); Collections.addAll(list,1,2,3,4,5,6,7,8,9,10,11); System.out.println(list.size()); 
    List list1 = new ArrayList(); 
    list1.add(10); 
    list1.add(9); 
    list1.add(8); 
    list1.add(7); 
    list1.add(6); 
    list1.add(5); 
    list1.add(4); 
    list1.add(3); 
    list1.add(2); 
    list1.add(1);
    list1.add(0); Collections.copy(list1, list); System.out.println(list); System.out.println(list1); System.out.println("================="); List list2 = new ArrayList(); list2.add(20); list2.add(null); boolean flag = Collections.disjoint(list, list2); System.out.println(flag); // 
    Collections.fill(list1, 6); // 
    System.out.println(list1); 
    List list3 = new ArrayList();
    list3.add(new Person("赵四2", 23));               list3.add(new Person("赵四", 20));
    list3.add(new Person("赵四3", 23)); 
    list3.add(new Person("赵四1", 22));
    Object object = Collections.max(list3);         Object object1 = Collections.max(list3,new AgeComparator());
    System.out.println(object); System.out.println(object1); System.out.println("==============="); Collections.replaceAll(list2, null, 100); System.out.println(list2); Collections.reverse(list1); System.out.println(list1); Collections.shuffle(list1); System.out.println(list1); System.out.println("============"); System.out.println(list3); Collections.sort(list3); System.out.println(list3); System.out.println("============="); Collections.sort(list3, new AgeComparator()); System.out.println(list3); 
} public static void main(String[] args) { 
    List list = new ArrayList(); Collections.addAll(list, 2,5,1,0,8); Collections.sort(list); Collections.reverse(list); int index = Collections.binarySearch(list, 0); System.out.println(index); //0
}

2.泛型

泛型是在事先不知道要传入什么类型的参数时用T K E 代替

泛型在编译时会被擦除

在调用方法是又隐士转换回来

标签

评论

this is is footer