转载自:http://peiquan.blog.51cto.com/7518552/1290180
一、Queue接口
public interface Queue<E> extends Collection<E> { E element(); boolean offer(E e); E peek(); E poll(); E remove(); }
public class Counter { public static void main(String[] args) throws Exception { int time = 10;//十秒倒数 Queue<Integer> queue = new LinkedList<Integer>(); //初始化queue for (int i = time; i >= 0; i--){ queue.add(i); } while (!queue.isEmpty()) { System.out.println(queue.remove()); Thread.sleep(1000); } } }
public class PriorityQueueTest { static <E> List<E> heapSort(Collection<E> c) { Queue<E> queue = new PriorityQueue<>(c); List<E> result = new ArrayList<>(); while (!queue.isEmpty()) { result.add(queue.remove()); } return result; } public static void main(String[] args) { String[] str = { "d", "b", "a", "c" }; List<String> list = new ArrayList<>(); for (String s : str) { list.add(s); } System.out.println(heapSort(list)); } }
Map接口
public interface Map<K,V> { //基本操作 V put(K key, V value); V get(Object key); V remove(Object key); boolean containsKey(Object key); boolean containsValue(Object value); int size(); boolean isEmpty(); // 批量操作 void putAll(Map<? extends K, ? extends V> m); void clear(); // Collection视图 public Set<K> keySet(); public Collection<V> values(); public Set<Map.Entry<K,V>> entrySet(); //为entrySet元素提供的接口 public interface Entry { K getKey(); V getValue(); V setValue(V value); } }
Map的基本操作
public class Frequency { public static void main(String[] args) { String[] str = { "a", "c", "b", "d", "a", "d", "a" }; Map<String, Integer> map = new HashMap<>(); // 遍历str,取得频数分布表 for (String a : str) { Integer count = map.get(a); map.put(a, (count == null) ? 1 : count + 1); } System.out.println(map.size() + " 不同的单词"); System.out.println(map); } }
Map的批量操作
static <K, V> Map<K, V> newAttributeMap(Map<K, V>defaults, Map<K, V> overrides) { Map<K, V> result = new HashMap<K, V>(defaults); result.putAll(overrides); return result; }
Collection视图
keySet
— 由Key组成Set视图values
— 由value组成Collection视图,但又与Set视图有区别,因为一个value可以映射到多个Key上(即可能有重复的元素)entrySet
— 由key-value组成的Set视图。在Map接口里内嵌里一个小接口Map.Entry
//foreach方法 for (KeyType key : m.keySet()){ System.out.println(key); } //使用iterator接口 for (Iterator<Type> it = m.keySet().iterator(); it.hasNext(); ){ if (it.next().isBogus()){ it.remove(); } }
for (Map.Entry<KeyType, ValType> e : m.entrySet()) { System.out.println(e.getKey() + ": " + e.getValue()); }
Collection视图的应用:Map代数
if (m1.entrySet().containsAll(m2.entrySet())) {
...
}
if (m1.keySet().equals(m2.keySet())) {
...
}
static <K, V> boolean validate(Map<K, V> attrMap, Set<K> requiredAttrs, Set<K>permittedAttrs) { boolean valid = true; Set<K> attrs = attrMap.keySet(); if (! attrs.containsAll(requiredAttrs)) {//没有全包含必须有的值 Set<K> missing = new HashSet<K>(requiredAttrs); missing.removeAll(attrs); System.out.println("缺少的值有:" + missing); valid = false; } if (! permittedAttrs.containsAll(attrs)) { Set<K> illegal = new HashSet<K>(attrs); illegal.removeAll(permittedAttrs); System.out.println("不合法的值有: " + illegal); valid = false; } return valid; }
Set<KeyType> commonKeys = new HashSet<>(m1.keySet());
commonKeys.retainAll(m2.keySet());
或:m1.keySet().removeAll(m2.keySet());
或:m1.values().removeAll(m2.values());
Set<Employee> individualContributors = new HashSet<>(managers.keySet());
individualContributors.removeAll(managers.values());
Employee manager = ... ;//某个经理对象
managers.values().removeAll(Collections.singleton(manager));
Map<Employee, Employee> m = new HashMap<>(managers);//复制managers对象 m.values().removeAll(managers.keySet());//移除职位是上司(或上司的上司)的对象 Set<Employee> slackers = m.keySet();//剩下的就是一线员工了
public class MapTest { public static void main(String[] args) { Map<String, String> m1=new HashMap<String, String>(); Map<String, String> m2=new HashMap<String, String>(); m1.put("1", "1"); m1.put("2", "1"); m1.put("3", "2"); m2.put("3", "1"); m2.put("1", "1"); m2.put("4", "1"); m1.entrySet().removeAll(m2.entrySet());// A // m1.keySet().removeAll(m2.keySet()); // B // m1.values().removeAll(m2.keySet()); // C // m1.values().removeAll(m2.keySet()); //D System.out.println(m1); } }