博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Map
阅读量:7164 次
发布时间:2019-06-29

本文共 3960 字,大约阅读时间需要 13 分钟。

package ds.collections.maps;import java.util.Collections;import java.util.Map;import java.util.TreeMap;import java.util.HashMap;import java.util.Iterator;import java.util.Map.Entry;import java.util.LinkedHashMap;/** * Map  * @author Hust * @Time 2011-10-23 */public class MapTest {    public static void main(String[] args) {        //testHashMap();        //hashMapTest();        //treeMapTest();        linkedHashMapTest();    }        /**     * 测试HashMap     */    public static void testHashMap(){        //无序,hashMap本身的实现不是同步的即不是线程安全的        //如想设为线程安全,初始化时加上:Collections.synchronizedMap        Map
thMap = Collections.synchronizedMap(new HashMap
(32)); thMap.put(1232, "12323321"); thMap.put(1, "1233421"); thMap.put(4, "12323321"); thMap.put(123, "23455"); //System.out.println(thMap.hashCode()+"_"+thMap.size()); //thMap.remove(123); thMap.put(123, "234"); thMap.put(null, "234"); //System.out.println(thMap.size()); //System.out.println(thMap.isEmpty()); //System.out.println(thMap.containsKey(123)); //System.out.println(thMap.containsValue("123321")); //System.out.println(thMap.get(123)); //System.out.println(thMap.values()); for(Entry
b : thMap.entrySet()) { System.out.print(b.getKey()+"_");//获取键 System.out.println(b.getValue());//获取值 } //thMap.clear(); System.out.println("=thMap.size() "+thMap.size()); //Map.Entry--Map的内部类,描述Map中的按键/数值对。 Map
linkMap = new LinkedHashMap
(thMap); for(Entry
b : linkMap.entrySet()) { System.out.print(b.getKey()+"_");//获取键 System.out.println(b.getValue());//获取值 } } /** * 无序 适合 插入、删除和定位元素容量小时具有很快的访问速度 * 容量很大,实际数据较少时,遍历起来可能会比LinkedHashMap慢 */ public static void hashMapTest() { System.out.println("------hashMapTest------"); Map
map = new HashMap
(); map.put("1", "Value 1"); map.put("2", "Value 2"); map.put("3", "Value 3"); map.put("4", "Value 4"); map.put("F", "Value F"); map.put("Q", "Value Q"); //必要时初始化时控制Map大小 //其负载因子默认为0.75,容量(大小)默认为16, 如果(负载因子)*(容量)〉map大小,则调整Map大小 //8 * 0.75 = 6 Map
map2 = new HashMap
(8); map2.putAll(map); //可对之进行迭代,Entry,Map的内部类,描述Map中的按键/数值对。 //Map.entrySet返回map的collection视图 Iterator
> it = map2.entrySet().iterator(); while (it.hasNext()) { Map.Entry
e = (Map.Entry
) it.next(); System.out.println(e.getKey() + " _ " + e.getValue()); } } /** * TreeMap取出来的是排序后的键值对。 * 它是sortedMap的唯一实现 */ public static void treeMapTest() { System.out.println("------treeMapTest------"); Map
map = new TreeMap
(); map.put("1", "Value 1"); map.put("F", "Value F"); map.put("Q", "Value Q"); map.put("3", "Value 2"); map.put("5", "Value 5"); map.put("4", "Value 4"); Iterator
> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry
e = (Map.Entry
) it.next(); System.out.println(e.getKey() + " _ " + e.getValue()); } System.out.println("------treeMapTest2------"); //TreeMap取出来的是排序后的键值对。它是sortedMap的唯一实现。 //默认按键对象升序排列,如想降序排列,实现方式为将Collections.reverseOrder()作为TreeMap的构造方法。 Map
tmap = new TreeMap
(Collections.reverseOrder()); tmap.putAll(map); Iterator
> tmapit = tmap.entrySet().iterator(); while (tmapit.hasNext()) { Map.Entry
e = (Map.Entry
) tmapit.next(); System.out.println( e.getKey() + " _ " + e.getValue()); } } /** * 保存了记录的插入顺序,即先插入的先遍历到 * 也可以在构造时用带参数,按照应用次数排序。 */ public static void linkedHashMapTest() { System.out.println("------linkedHashMapTest------"); Map
map = new LinkedHashMap
(); map.put("1", "Value 1"); map.put("2", "Value 2"); map.put("5", "Value 3"); map.put("4", "Value 4"); map.put("F", "Value F"); map.put("Q", "Value Q"); Iterator
> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry
e = (Map.Entry
) it.next(); System.out.println(e.getKey() + " _ " + e.getValue()); } } }

 

转载于:https://www.cnblogs.com/znsongshu/p/6282589.html

你可能感兴趣的文章