一种工具,放在java.util
包中java
单列集合的最顶层接口,定义了全部单列集合的共性方法程序员
方法 | 描述 |
---|---|
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在定义时,并不知道存储什么样的数据类型this
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类型的父类或自己,本身是下限
技巧:
在建立集合时,也能够不指定泛型
使用泛型的好处