Java 集合小结

具体操作,参考官方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 object
SortedSet<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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值