java 7 collection 详解(二)
来源:互联网 发布:武汉ui知乎 编辑:程序博客网 时间:2024/06/06 20:43
转载自: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>();//初始化queuefor (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);}}
0 0
- java 7 collection 详解(二)
- java 7 collection 详解(一)
- JAVA 集合(二)--collection
- 对象排序 — java 7 collection详解(三)
- 【Java】容器类学习之路(二)Collection详解:List、Set和Queue
- Java集合框架详解(二)——Collection源码分析
- JAVA第十六弹《Collection(二)》
- GC (二)Garbage Collection in Java
- JAVA基础---集合(二)--Collection
- Java--容器(Collection)(二)
- java 容器详解(collection,map)
- Java集合详解(一):Collection
- Java数据结构详解(一)-Collection接口
- java collection详解
- Java Collection框架详解
- Java Collection框架详解
- Java Collection框架详解
- Collection(二)Iterator
- C++(2)基本数据类型
- 浅谈java设计模式之单例模式
- java 7 collection 详解(一)
- CSU 1325: A very hard problem 莫比乌斯反演
- ios 读取大文件
- java 7 collection 详解(二)
- 对象排序 — java 7 collection详解(三)
- HDU 5358 First One(枚举+尺举法)
- zoj 1113 u Calculate e(小数点精度保留)
- linux下vim命令详解
- volley
- HDU 4686 Arc of Dream(矩阵快速幂)
- 网络超时设置connectionTimeout和SoTimeout的区别
- jsp+servlet+tomcat简单的数据库查询项目