moc 发表于 2018-12-26 12:15:03

Java-019集合框架之Collection、Collections

本帖最后由 moc 于 2018-12-26 22:41 编辑

1、Collection简介
集合框架设计目标:
        ① 框架必须是高性能的。基本集合(动态数组,链表,树,哈希表)的实现也必须是高效的。
        ② 框架允许不同类型的集合,以类似的方式工作,具有高度的互操作性。
        ③ 对一个集合的扩展和适应必须是简单的。
Java 集合框架体系:
        ① 集合框架的类和接口均在java.util包。
        ② 任何对象没有使用泛型之前会自动转换Object类型,使用泛型之后不用强制转换。
集合根接口Collection:
Collection是集合的顶层接口,它的子体系有重复的,有唯一的,有有序的,有无序的。
迭代器接口 - Iterator:
集合的专用遍历方式,拥有三个方法:
boolean hasNext()---------------------->如果仍有元素可以迭代,则返回 true。
E next()---------------------------------->返回迭代的下一个元素。
void remove()--------------------------->从迭代器指向的 collection 中移除迭代器返回的最后一个元素.
2、Collection的方法
1. 添加功能
boolean add(Object obj)------------------->添加一个元素
boolean addAll(Collection c) --------------->添加一个集合的元素
2. 删除功能
void clear()----------------------------------->移除所有元素
boolean remove(Object o)----------------->移除一个元素
boolean removeAll(Collection c)-----------> 移除一个集合的元素
3. 判断功能
boolean contains(Object o)---------------->判断集合中是否包含指定的元素
boolean containsAll(Collection c)---------->判断集合中是否包含指定的集合元素(所有)
boolean isEmpty() -------------------------->判断集合是否为空
4. 获取功能
Iterator<E> iterator()---------------------->返回迭代器
5. 长度功能
int size()   -------------------------------------->元素的个数
6. 交集功能
boolean retainAll(Collection c)   ------------->两个集合都有的元素,返回的boolean为集合是否改变
7. 把集合转换为数组
Object[] toArray()   --------------------------->返回包含此 collection 中所有元素的数组。
3、方法测试
collection为一个接口,不能直接实例化,需要通过实现该接口的类来实例化,以下通过实现Collection接口的ArrayList类进行测试。
// 创建集合1
Collection c = new ArrayList();
c.add("abc1");   // 添加一个元素
c.add("abc2");
c.remove("abc2");// 移除一个元素
c.clear();    // 移除所有元素
c.add("hello");
c.add("world");
c.contains("hello")// 判断集合中是否包含指定的元素
c.isEmpty()   // 判断集合是否为空
c.size()    // 获取集合元素的个数
Object[] objs = c.toArray();   // 把集合转成数组

Iterator it = c.iterator();   // while + 迭代器
while (it.hasNext()) {
        String s = (String) it.next();
        // NoSuchElementException    不要多次使用it.next()方法
        System.out.println(s);
}

for(Iterator it = c.iterator();it.hasNext();){   // for + 迭代器
        String s = (String) it.next();
        System.out.println(s);
}4、Collections工具类
Collections是针对集合进行操作的工具类,都是静态方法。
常用方法:
public static <T> void sort(List<T> list) ---------------------------------->排序 默认情况下是自然顺序。
public static <T> int binarySearch(List<?> list,T key)----------------->二分查找
public static <T> T max(Collection<?> coll)---------------------------->最大值
public static void reverse(List<?> list)----------------------------------->反转
public static void shuffle(List<?> list) ------------------------------------>随机置换
List<Integer> list = new ArrayList<Integer>();// 创建集合对象
list.add(30);// 添加元素
list.add(20);
list.add(50);
list.add(10);
list.add(40);
System.out.println("list:" + list);

// Collections.sort(list);
// System.out.println("list:" + list);

// System.out
// println("binarySearch:" + Collections.binarySearch(list, 30));
// System.out.println("binarySearch:" + Collections.binarySearch(list, 300));

// System.out.println("max:"+Collections.max(list));

// Collections.reverse(list);
// System.out.println("list:" + list);
               
Collections.shuffle(list);
System.out.println("list:" + list);
页: [1]
查看完整版本: Java-019集合框架之Collection、Collections