- Collection:单列集合类的根接口,用于存储一系列符合某种规则的元素,它有两个重要的子接口,分别是
java.util.List
和java.util.Set
。List
的特点是元素有序、元素可重复。List
接口的主要实现类有java.util.ArrayList
和java.util.LinkedList
,Set
的特点是元素无序,而且不可重复。Set
接口的主要实现类有java.util.HashSet
和java.util.TreeSet
。
常用方法
public boolean add(E e) 把给定的对象添加到当前集合中 。
public void clear() 清空集合中所有的元素。
public boolean remove(E e) 把给定的对象在当前集合中删除。
public boolean contains(E e) 判断当前集合中是否包含给定的对象。
public boolean isEmpty() 判断当前集合是否为空。
public int size() 返回集合中元素的个数。
public Object[] toArray() 把集合中的元素,存储到数组中。
例
import java.util.ArrayList;
import java.util.Collection;
public class Demo1Collection {
public static void main(String[] args) {
// 创建集合对象
// 使用多态形式
Collection<String> coll = new ArrayList<String>();
//Collection<String> coll = new HashSet<String>(); 也是可以使用,并且不需要更改下面的代码
// 使用方法
// 添加功能 boolean add(String s)
coll.add("小李广");
coll.add("扫地僧");
coll.add("石破天");
System.out.println(coll);
// boolean contains(E e) 判断o是否在集合中存在
System.out.println("判断 扫地僧 是否在集合中"+coll.contains("扫地僧"));
//boolean remove(E e) 删除在集合中的o元素
System.out.println("删除石破天:"+coll.remove("石破天"));
System.out.println("操作之后集合中元素:"+coll);
// size() 集合中有几个元素
System.out.println("集合中有"+coll.size()+"个元素");
// Object[] toArray()转换成一个Object数组
Object[] objects = coll.toArray();
// 遍历数组
for (int i = 0; i < objects.length; i++) {
System.out.println(objects[i]);
}
// void clear() 清空集合
coll.clear();
System.out.println("集合中内容为:"+coll);
// boolean isEmpty() 判断是否为空
System.out.println(coll.isEmpty());
}
}
静态方法: 直接使用 Collections 调用
public static <T> boolean addAll(Collection<T> c, T... elements) 往集合中添加一些元素。
public static void shuffle(List<?> list) 打乱顺序`:打乱集合顺序。
public static <T> void sort(List<T> list) 将集合中元素按照默认规则排序。
当使用自定义对象时,使用 sort 方法必须在类中实现 Comparable 接口,重写 CompareTo 方法
升序:this.get方法 - o.get方法
public static <T> void sort(List<T> list,Comparator<? super T> ) 将集合中元素按照指定规则排序。
升序 o1 -o2
定义一个 Person 类
省略 构造方法以及 get/set方法(实际代码中需要写)
public class Person implement comperable<Person>{
private name;
private age;
//重写comperTo
@Override
public int compareTo(Person o) {
return this.getAge() - o.getAge();
}
}
main 方法
public class CollectionsDemo {
public static void main(String[] args) {
//定义一个ArrayList<Integer>集合,Collecton 是一个接口,ArrayList 属于他的实现类
ArrayList<Integer> arrayListInt = new ArrayList<>();
//调用Collections.addAll
Collections.addAll(arrayListInt,1, 2, 3, 4, 5, 6,7);
System.out.println(arrayListInt);
//调用Collections.shuffle()
Collections.shuffle(arrayListInt);
System.out.println(arrayListInt);
//调用Collections.sort,排序整型List
Collections.sort(arrayListInt);
System.out.println(arrayListInt);
//调用Collections.sort,排序Person list
ArrayList<Person> arrayListPerson = new ArrayList<>();
arrayListPerson.add(new Person("张三",15));
arrayListPerson.add(new Person("李四",18));
arrayListPerson.add(new Person("王五",16));
System.out.println("排序前:" + arrayListPerson);
Collections.sort(arrayListPerson);
System.out.println("排序后:" + arrayListPerson);
//扰乱排序,测试 comperator 方法
Collections.shuffle(arrayListPerson);
System.out.println("扰乱后:" + arrayListPerson);
Collections.sort(arrayListPerson, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.getAge() - o2.getAge();
}
});
System.out.println("comperator方法排序后:" + arrayListPerson);
}
}