JAVAAPI学习之TreeMap类

package util;

import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

/**
 *
 * @author yjmao
 * @deprecated TreeMap的常用方法小结(继承于NavigableMap<K,V>接口)
 * @version V1.0.0
 * @see 使用键的自然顺序构造一个新的、空的树映射。插入该映射的所有键都必须实现 Comparable 接口
 */
public class LearnTreeMap {
    //TreeMap以下方法没有实现
    //firstKey
    //lastKey
    //firstEntry
    //lastEntry
    //pollFirstEntry
    //pollLastEntry
    //lowerEntry
    //lowerKey
    //floorEntry
    //floorKey
    //ceilingEntry
    //ceilingKey
    //higherEntry
    //higherKey
    //navigableKeySet
    //descendingKeySet
    //descendingMap
    //subMap
    //headMap
    //tailMap
    //subMap
    //headMap
    //tailMap

    public static void main(String[] args) {
        //easyMethod();
        //hardMethod();

        Map<String, Object> map = new TreeMap<String, Object>();

        //isEmpty():判断集合是否为空
        boolean isEmpty = map.isEmpty();
        System.err.println("map是否为空:" + isEmpty);

        //put(K key,V value):将指定值与此映射中的指定键进行关联。
        //如果该映射以前包含此键的映射关系,那么将替换旧值。
        map.put("aaa", "aaa键");
        map.put("ddd", "ddd键");
        map.put("bbb", "bbb键");
        map.put("ccc", "ccc键");

        //size():返回此映射中的键-值映射关系数。
        int size = map.size();
        System.err.println("map中元素的个数:" + size);


        //containsKey(Object key):如果此映射包含指定键的映射关系,则返回 true。 
        boolean conK = map.containsKey("aaa");
        System.err.println("map集合是否包含aaa键:" + conK);
        boolean conKF = map.containsKey("aassa");
        System.err.println("map集合是否包含aassa键:" + conKF);


        //containsValue(Object value):如果此映射为指定值映射一个或多个键,则返回 true。
        boolean conV = map.containsValue("aaa键");
        System.err.println("map集合是否包含aaa键对应的值:" + conV);
        boolean conVF = map.containsValue("abbaa键");
        System.err.println("map集合是否包含abbaa键对应的值:" + conVF);


        //get(Object key):返回指定键所映射的值,如果对于该键而言,此映射不包含任何映射关系,则返回 null。 
        String key = (String) map.get("aaa");
        System.err.println("aaa键所对应的值:" + key);

        Map<String, Object> mapAll = new TreeMap<String, Object>();
        mapAll.put("fff", "fff键");
        //putAll(Map<? extends K,? extends V> map):将指定映射中的所有映射关系复制到此映射中。
        mapAll.putAll(map);
        mapAll.put("ddd", "eee键");

        System.err.print("map中包含的键值:");
        // keySet():返回此映射包含的键的 Set 视图。public Set<K> keySet()
        Set<String> set = map.keySet();
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            String s = iterator.next();
            System.err.print(s + ",");
        }
        
        /******************倒序排列*************************************/
        System.err.print("\ntreemap中包含的键值:");
        Map<String,Object> treeMap = new TreeMap<String,Object>(new Comparator<String>() {
            public int compare(String d1, String d2) {
                return d2.compareTo(d1);
            }
        });
        treeMap.put("aaa", "aaa键");
        treeMap.put("ddd", "ddd键");
        treeMap.put("bbb", "bbb键");
        treeMap.put("ccc", "ccc键");
        Set<String> setTree = treeMap.keySet();
        Iterator<String> iteratorTree = setTree.iterator();
        while (iteratorTree.hasNext()) {
            String s = iteratorTree.next();
            System.err.print(s + ",");
        }
        
        
        System.err.print("\nmapAll中包含的键值:");
        Set<String> setAll = mapAll.keySet();
        Iterator<String> iteratorAll = setAll.iterator();
        while (iteratorAll.hasNext()) {
            String s = iteratorAll.next();
            System.err.print(s + ",");
        }

        System.err.print("\nmapAll中包含的值:");
        //values():返回此映射包含的值的 Collection 视图。public Collection<V> values()
        Collection<Object> con = mapAll.values();
        Iterator it = con.iterator();
        while (it.hasNext()) {
            String s = (String) it.next();
            System.err.print(s + ",");
        }

        //entrySet():返回此映射中包含的映射关系的 Set 视图。public Set<Map.Entry<K,V>> entrySet()
        System.err.println("\nentry输出map中的值:");
        Set<Entry<String, Object>> en = mapAll.entrySet();
        Iterator<Entry<String, Object>> iten = en.iterator();
        while (iten.hasNext()) {
            Entry<String, Object> enm = iten.next();
            String keyValue = enm.getKey();
            String value = (String) enm.getValue();
            System.err.println(keyValue + ":" + value);
        }

        System.err.print("移出aaa键之后的mapAll集合:");
        mapAll.remove("aaa");
        Set<String> setAllR = mapAll.keySet();
        Iterator<String> iteratorAllR = setAllR.iterator();
        while (iteratorAllR.hasNext()) {
            String s = iteratorAllR.next();
            System.err.print(s + ",");
        }

        //clear():从此映射中移除所有映射关系。在此调用返回之后,映射将为空。 
        mapAll.clear();
        System.err.println("\nmapAll是否为空:" + mapAll.isEmpty());

    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值