1.Collection详解
2.Collections详解
3.Collection和Collections的区别
1.Collection详解
1.1Collection简介
java.util.Collection
.Collection是所有单列集合(一次存一个元素,是单列集合)的父接口,因此定义了单列集合(List和Set)通用的一些方法,这些方法适用于所有的单列集合
Collection集合主要有List和Set两大接口
List:有序(元素存入集合的顺序和取出的顺序一致),元素都有索引。元素可以重复。
Set:无序(存入和取出顺序有可能不一致),不可以存储重复元素。必须保证元素唯一性。
1.2Collection子类常用共性方法
方法名 | 方法作用 |
---|---|
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() | 把集合中元素,存储到数组中 |
下面我们拿它的子集合Arraylist举例:
package untl;
import java.util.ArrayList;
import java.util.Collection;
public class MyCollection {
public static void main(String[] args) {
Collection<String> collection=new ArrayList();
for (int i=0;i<5;i++)
{
collection.add("张"+(i+1));
}
System.out.println(collection);
System.out.println("----------------测试remove方法-----------------------");
collection.remove("张1");
System.out.println(collection);
collection.remove("王五");
System.out.println(collection);
System.out.println("----------------测试contains方法-----------------------");
boolean bool=collection.contains("张5");
System.out.println(bool);
bool=collection.contains("王五");
System.out.println(collection);
System.out.println("-----------------测试isEmpty方法----------------------");
bool=collection.isEmpty();
System.out.println(bool);
System.out.println("-----------------测试size方法----------------------");
int len=collection.size();
System.out.println(len);
System.out.println("-----------------测试toArray方法----------------------");
Object arr[]= collection.toArray();
for (Object a:arr
) {
System.out.println(a);
}
System.out.println("-----------------测试clear方法----------------------");
collection.clear();
System.out.println(collection);
}
}
运行结果:
[张1, 张2, 张3, 张4, 张5]
----------------测试remove方法-----------------------
[张2, 张3, 张4, 张5]
[张2, 张3, 张4, 张5]
----------------测试contains方法-----------------------
true
[张2, 张3, 张4, 张5]
-----------------测试isEmpty方法----------------------
false
-----------------测试size方法----------------------
4
-----------------测试toArray方法----------------------
张2
张3
张4
张5
-----------------测试clear方法----------------------
[]
Process finished with exit code 0
2.Collections详解
2.1Collections简介:
Java提供了一个操作Set、List和Map等集合的工具类:Collections,该工具类里
提供了大量方法对集合元素 进行排序、查询和修改 等操作,还提供了 对集合对象实现
同步控制 等方法。这个类不需要创建对象,内部提供的都是静态方法:
2.2常用方法:
方法 | 方法作用 |
---|---|
public static boolean addAll(Collection c, T… elements) | 往集合中添加一些元素。 |
public static void shuffle(List<?> list) | 打乱集合顺序。 |
public static void sort(List list) | 将集合中元素按照默认规则排序。 |
public static void sort(List list,Comparator<? super T> ) | 将集合中元素按照指定规则排序。 |
使用例子:
package untl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class MyCollections {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
Collections.addAll(list,"bbb","aaa","ccc");
System.out.println(list);
Collections.sort(list);
System.out.println(list);
Collections.shuffle(list);
System.out.println(list);
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.charAt(0)-o1.charAt(0);//按照第一个字母降序排列
}
});
System.out.println(list);
}
}
运行结果:
[bbb, aaa, ccc]
[aaa, bbb, ccc]
[aaa, ccc, bbb](由于这个是打乱后的所以不固定)
[ccc, bbb, aaa]
3.Collection和Collections的区别
1.Collections是个
java.util
下的类,是针对集合类的一个工具类,提供一系列静态方法,实现对集合的查找、排序、替换、线程安全化(将非同步的集合转换成同步的)等操作。
2.Collection是个java.util
下的接口,它是各种集合结构的父接口,继承于它的接口主要有Set和List,提供了关于集合的一些操作,如插入、删除、判断一个元素是否其成员、遍历等。