1. Collection接口是Java集合框架的基本接口,所所有集合都继承该接口。
1.1 方法 :
public interface Collection<E> extends Iterable<E> { //向集合添加元素,成功返回true,失败返回false boolean add(E e); //用于返回一个实现了Iterator接口的对象。 Iterator<E> iterator(); ... }
1.2 Iterator 迭代器
public interface Iterator<E> { //如果迭代器对象还有很多元素访问,返回true, boolean hasNext(); //逐个访问集合中的每个元素,达到集合的末尾, //会抛出NoSuchElementException E next(); default void remove() { throw new UnsupportedOperationException("remove"); } default void forEachRemaining(Consumer<? super E> action) { Objects.requireNonNull(action); while (hasNext()) action.accept(next()); } }
1.3 遍历集合
方法1:用迭代器
Collection<String> c = ...; Iterator<String> iterator = c.iterator(); while (iterator.hasNext()){ //iterator.next() 越过一个元素,并返回刚刚越过的那个元素 String element = iterator.next(); //在调用过next方法之后,才可以调用remove()方法, // 来删除刚刚越过的那个元素 iterator.remove(); ... }
方法2:用for each
Collection<String> c = ...; for (String element : c){ do something with "element"... }
方法3:java8 lambda表达式 forEachRemaning()
Collection<String> c = ...; Iterator<String> iterator = c.iterator(); iterator.forEachRemaining( ele -> { do something with "element"... });
※ 在删除元素时还可以调用一个更简单的方法:
Collection<String> c = ...; c.removeIf(ele -> { if (ele ...){ return true; }else { return false; } });
2. List 有序集合
遍历元素的方法:
2.1 迭代器访问 :适用链表结构集合
2.2 整数索引访问——又称随机访问 :数组结构的集合
2.3 ArrayList 数组结构 从中删除一个元素,之后所有的元素都要向前移动,开销很大,增加元素同理。
2.3 LinkedList 链表结构 ,每个元素存储在独立的节点中,每个节点都有指向前一个元素和后一个元素的引用。
3.Set集
3.1 HashSet 散列集,实现了基于散列表的集 ,位置随机,无序集合。
add()
contains() 用来查看某个元素是已经存在集中。
3.2 TreeSet 树集 ,有序集合。树结构(红黑树 red-black tree)
import java.util.*; /** * This program sorts a set of item by comparing their descriptions. * @version 1.12 2015-06-21 * @author Cay Horstmann */ public class TreeSetTest { public static void main(String[] args) { SortedSet<Item> parts = new TreeSet<>(); parts.add(new Item("Toaster", 1234)); parts.add(new Item("Widget", 4562)); parts.add(new Item("Modem", 9912)); System.out.println(parts); NavigableSet<Item> sortByDescription = new TreeSet<>( Comparator.comparing(Item::getDescription)); sortByDescription.addAll(parts); System.out.println(sortByDescription); } }
import java.util.*; /** * An item with a description and a part number. */ public class Item implements Comparable<Item> { private String description; private int partNumber; /** * Constructs an item. * * @param aDescription * the item's description * @param aPartNumber * the item's part number */ public Item(String aDescription, int aPartNumber) { description = aDescription; partNumber = aPartNumber; } /** * Gets the description of this item. * * @return the description */ public String getDescription() { return description; } public String toString() { return "[description=" + description + ", partNumber=" + partNumber + "]"; } public boolean equals(Object otherObject) { if (this == otherObject) return true; if (otherObject == null) return false; if (getClass() != otherObject.getClass()) return false; Item other = (Item) otherObject; return Objects.equals(description, other.description) && partNumber == other.partNumber; } public int hashCode() { return Objects.hash(description, partNumber); } public int compareTo(Item other) { int diff = Integer.compare(partNumber, other.partNumber); return diff != 0 ? diff : description.compareTo(other.description); } }