Collection 关系图
Collection 隶属于 java.util 包
Collection源码
public interface Collection<E> extends Iterable<E> {
//定义size方法,应返回size大小,最大值为Integer.MAX_VALUE:2147483639
int size();
//定义isEmpty方法,用于返回是否为空
boolean isEmpty();
//定义contains方法,判断一个obj是否属于此集合
boolean contains(Object o);
//定义迭代器方法,返回一个迭代器对象
Iterator<E> iterator();
//定义转换为转换Obj数组的方法,返回一个obj数组
Object[] toArray();
//定义转换为泛型T数组的方法,返回一个指定泛型的数组
<T> T[] toArray(T[] a);
//定义add方法,添加一个元素e,并返回添加成功标志
boolean add(E e);
//定义remove方法,移除一个元素obj,并返回移除元素成功标志
boolean remove(Object o);
//定义containsAll方法,判断是否包含集合实例对象c
boolean containsAll(Collection<?> c);
//定义addAll方法,添加集合实例对象c到本实例中
boolean addAll(Collection<? extends E> c);
//定义removeAll方法,从本实例中移除集合实力对象c
boolean removeAll(Collection<?> c);
/**
* jdk8新增
* 大意为:
* 定义removeIf方法,该方传递参数为函数式,传递内容是一个接口类型,该接口类型
* 定义为一个filter函数,该函数用于传递给Objects.requireNonNull判断,然后调用
* 迭代器,将满足该定义的filter的本实例中的元素移除
* @since 1.8
*/
default boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
final Iterator<E> each = iterator();
while (each.hasNext()) {
if (filter.test(each.next())) {
each.remove();
removed = true;
}
}
return removed;
}
//定义retainAll方法,取本例与集合实例c的交集,判断交集元素数量是否大于0
boolean retainAll(Collection<?> c);
//定义clear方法,清除本例集合中的所有元素
void clear();
// Comparison and hashing 原注释,不明意义
//不解释
boolean equals(Object o);
//定义hashCode方法,获取hashCode标记的方法
int hashCode();
/**
*jdk8新增
*大意为:
*将迭代器拆分,重写自Iterable接口,从已知元素数量的本例中返回拆分后的迭代器
*此方法返回的为本类本身
*子类可能重写迭代器拆分,将一个集合的迭代拆分为多个集合的迭代交由多个线程
* @since 1.8
*/
@Override//重写父类
default Spliterator<E> spliterator() {
return Spliterators.spliterator(this, 0);
}
/**
* jdk8新增
* 大意为:
* 将默认的迭代器拆分后的结果,聚合为流,该流并非io中的流,
* 是一种类似于数据排序操作流
* @since 1.8
*/
default Stream<E> stream() {
return StreamSupport.stream(spliterator(), false);
}
/**
* jdk8新增
* 同上,但是具体其他的实现中,因为可并行,所以可能是线程不安全的
*
* @since 1.8
*/
default Stream<E> parallelStream() {
return StreamSupport.stream(splitera
Collection vs Collections
Collection java.util 包下的接口,Collections 是 java.util 包下的,是服务于Collection
常用的方法:
Collections.toAll ---一次性添加
Collections.shuffle ----乱序
Collections.sort ----排序(1.Comparable 接口的 compareTo;2.利用匿名内部类Compatator )
Collections.reverse ---反转
Collections.binarySearch ---二分法查找:
List personList = new ArrayList();
Collections.addAll(personList, new Person("黎明",21), new Person("胡歌",2),
new Person("刘德华",24),new Person("古天乐",18));
Person person = new Person("袁弘",29);
personList.add(person);
int index = Collections.binarySearch(personList,person);
System.out.println(index)
Collections.max ---返回某项最大值:
List personList = new ArrayList();
Collections.addAll(personList, new Person("黎明",21), new Person("胡歌",2),
new Person("刘德华",24),new Person("古天乐",18));
Person person = new Person("袁弘",9);
personList.add(person);
Person max = Collections.max(personList, new Comparator<Person>(
) {
@Override
public int compare(Person person, Person t1) {
return person.age-t1.age;
}
});
System.out.println(max)
Comparable VS Comparator
Comparable 需要调用自己(this) 和别人比较,自己需要实现接口Comparable,重写compareTo 方法。
Comparable
demo 演示
package com.compare;
public class Person implements Comparable<Person> {
String name;
int age;
public Person(){}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString(){
return "Person{" +
"name='"+name +'\'' +
", age=" + age +
"}";
}
public int compareTo(Person person) {
return this.age - person.age; ---升序
//return person.age - this.age; //-
package com.compare;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ComparatorDemo {
public static void main(String[] args) {
List personList = new ArrayList();
Collections.addAll(personList, new Person("黎明",21),
new Person("王红",22),new Person("里红",2));
Collections.sort(personList);
System.out.println(personList.toString());
}
运行结果
[Person{name='里红', age=2}, Person{name='黎明', age=21}, Person{name='王红', age=24}]
Comparator
Comparator 相当于找一第三方的裁判
demo 演示
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ComparatorDemo {
public static void main(String[] args) {
List personList = new ArrayList();
Collections.addAll(personList, new Person("黎明",21),
new Person("王红",22),new Person("里红",2));
Collections.sort(personList, new Comparator<Person>(
) {
@Override
public int compare(Person t1, Person t2) {
//return t1.age - t2.age ; ---升序
return t2.age - t1.age ; //---降序
}
});
System.out.println(personList.toString());
运行结果
[Person{name='王红', age=22}, Person{name='黎明', age=21}, Person{name='里红', age=2}]