Java 集合小结

来源:互联网 发布:js代码压缩工具 编辑:程序博客网 时间:2024/05/16 23:35

具体操作,参考官方API文档

队列

interface Queue<E> {  E head;  E tail;  void add(E element);  E remove();  int size();}

实现

  • 循环数组 ArrayDeque
  • 链表 LinkedList

    图1:
    queue

集合和遍历器接口

public interface Collection<E> {  Iterator<E> iterator();  boolean add(E element);  int size();  boolean contains(E obj);  boolean remove(E obj);  void clear();  E[] toArray();}

注意
- 遍历器: 通过next()描述了集合的位置信息,

public interface Iterator<E> {  E next(); //return NoSuchElementException if no element(null)  boolean hasNext();  void remove();}
  • 最佳实践
while(iterator.hasNext()) {  println(iterator.next())}
public class test {  public static void main(String[] args) {    PriorityQueue<Integer> q = new PriorityQueue<Integer>();    q.add(2);    q.add(1);    q.add(3);    q.add(8);    q.add(6);    System.out.println(q); //[1, 2, 3, 6, 8]    q.remove();    q.remove(); // It's ok.    System.out.println(q); // [3, 6, 8]    Iterator<Integer> i = q.iterator();    @scenario 1@    i.next();  // skip over the first element    i.remove(); // now remove it    //i.next();  // if ignore this code    i.remove();  // IllegalStateExeception    System.out.println(q); //[8]    @scenario 2@    i.next();  // skip over the first element    i.next();  // skip over the first element    i.remove();  // remove 6    System.out.println(q);//[3, 8]  }}

具体的集合

图2:
collection
图3:
interface

LinkedList 、ArrayList、priorityQueue

  • ListIterator: 注意CocurrentModificationExeption
    Notice
  • 在遍历器中没有add()方法 , 因为 set 不需要位置信息。可以用ListIterator代替 ,使用 add()方法。
LinkedListTest example

HashSet 、TreeSet

  • hash table:由数组实现,每一个数组元素都是一个链表, 如同在桶排序使用到的数据结构.
  • HashSet、TreeSet(红黑树实现): 没有重复的对象。

对象比较

// 方法一:public interface Comparable<T> {  int compareTo(T other);}class Item implements Comparable<Item> {  private int partNumber;  public Item(int val) { partNumber = val;}  public int compareTo(Item other) {    return partNumber - other.partNumber;  }}// 方法二:public interface Comparator<T> {  int compare(T a, T b);}class ItemComparator implements Comparator<Item>{  public int compare(Item a,Item b){    String descrA = a.getDescrA();    String descrB = b.getDescrB();    return descrA.compareTo(descrB);  }}ItemComparator comp = new ItemComarator();SortedSet<Item> sortByDescription = new TreeSet<>(comp);// 等价的方式:function objectSortedSet<Item> sortedByDescription = new TreeSet<>(new    Comparator<Item>() {      public int compare(Item a,Item b){        return a.getDescrA().compareTo(b.getDescrB());      }    });//例子:实现字符串按长度排序String[] words = {"hello", "word", "how", "are", "you"};Arrays.sort(words,new Comparator<String>() {            public int compare(String a, String b) {                return -( a.length() - b.length() );            }        });

HashMap and TreeMap

  • Map:不属于Java集合框架, 但是可以通过以下操作转换为Java集合:

    • set<k> keySet()

    • collection<K> values()

    • set<Map.Entry<K, V> entrySet() 的方法:

    • K getKey()
    • V getValue()
    • old_V setValue(V)
  • 其他方法: containsKey(K), containsValue(V),get(K),put(K,V)

1 0