
一种工具,放在java.util包中
单列集合的最顶层接口,定义了所有单列集合的共性方法
| 方法 | 描述 | 
|---|---|
| boolean add(E e) | 把给定的对象添加到当前集合中 | 
| boolean isEmpty() | 如果此集合不包含元素,则返回 true 。 | 
| boolean remove(E e) | 从该集合中删除指定元素的单个实例(如果存在) | 
| int size() | 返回此集合中的元素数。 | 
| boolean contains(E e) | 如果此集合包含指定的元素,则返回 true 。 | 
| Object[] toArray() | 返回一个包含此集合中所有元素的数组。 | 
| void clear() | 从此集合中清空所有元素 | 
创建集合对象时,使用多态,面向接口编程
比如Collection<String> list = new ArrayList<String>();
package collection;
import java.util.ArrayList;
import java.util.Collection;
/**
 * 面向接口编程
 * 增、删、判空、包含、统计、清空、转数组
 */
public class TestCollection1 {
    public static void main(String[] args) {
        // 换了后面的new类型 不影响引用类型的使用
        Collection<String> list = new ArrayList<String>();
        // Collection<String> list = new HashSet<>();
        // 重写toString方法
        System.out.println(list);
        System.out.println(list.add("A"));
        System.out.println(list.add("D"));
        System.out.println(list.add("A"));
        System.out.println(list.add("B"));
        System.out.println(list.add("C"));
        System.out.println(list);
        //删除第一个匹配到的
        System.out.println(list.remove("A"));
        System.out.println(list);
        System.out.println(list.remove("A"));
        System.out.println(list);
        Object[] array = list.toArray();
        System.out.println(list.contains("A"));
        System.out.println(list.contains("B"));
        System.out.println(list.size());
        list.clear();
        System.out.println(list);
        System.out.println(list.isEmpty());
        for (Object o : array) {
            System.out.print(o + "\t");
        }
    }
}
[]
true
true
true
true
true
[A, D, A, B, C]
true
[D, A, B, C]
true
[D, B, C]
false
true
3
[]
true
D	B	C
Iterator主要用于遍历Collection中的元素
迭代:判断集合中有没有元素,有就取出,继续判断,反复执行,直到集合中的元素全部取出
| 方法 | 描述 | 
|---|---|
| boolean hasNext() | 如果迭代具有更多元素,则返回 true | 
| E next() | 返回迭代中的下一个元素 | 
| default void remove() | 从底层集合中删除此迭代器返回的最后一个元素 | 
package cn;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
/**
 * 迭代器是一个接口,需要对应实现类对象
 * 从Collection集合中的iterator方法获取该集合的迭代器
 * 常用方法: hasNext 、 next
 */
public class TestIteration {
    public static void main(String[] args) {
        Collection<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println(list);
        
        System.out.println("=====加强for遍历======");
        for (Integer integer : list) {
            System.out.print(integer+"\t");
        }
        
        System.out.println("\n=====迭代器遍历======");
        // 开始时,指向-1下标
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next()+"\t");
        }
    }
}
[1, 1, 2, 3]
=====下标遍历======
1	1	2	3	
=====迭代器遍历======
1	1	2	3	
在平时的使用中还是更喜欢用增强for遍历
Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型。
泛型分为两种,一种表示属性,一种表示类
ArrayList在定义时,并不知道存储什么样的数据类型
public class ArrayList<E>{
	public boolean add(E e){};
	public E get(int index){};
}
但在声明时添加泛型可指定存储某种数据类型的数据
ArrayList<String> list = new ArrayList<String>();
// 相当于把数据类型当中参数赋值给E
public class ArrayList<String>{
	public boolean add(String e){};
	public String get(int index){};
}
可用于接口、类、方法
模拟ArrayList集合,自定义泛型的类
public class MyGeneric<E> {
    private E element;
    public E getElement() {
        return element;
    }
    public void setElement(E element) {
        this.element = element;
    }
}
定义时不确定具体数据类型,创建对象时才确定
只能作为方法的参数使用,不能创建对象使用!
泛型上限限定:? extends E —> E类型的子类或本身,自己就是上限
泛型下限限定:? super E —> E类型的父类或本身,自己是下限
技巧:
在创建集合时,也可以不指定泛型
使用泛型的好处
原文:https://www.cnblogs.com/1101-/p/13264105.html