当前位置: 首页 > 工具软件 > Treemap > 使用案例 >

案例之TreeMap

彭仲卿
2023-12-01

自定义对象作为TreeMap的key

快速搜索类:Ctrl+Shift+N

package MONA.demo01;

import sun.reflect.generics.tree.Tree;

import java.util.Comparator;
import java.util.TreeMap;
//快速搜索类:Ctrl+Shift+N
/**
 * 自定义对象作为TreeMap的key
 * 1.自定义对象必须实现Comparable
 * 2.通过构造方法传入比较器(Comparator)
 */
public class Demo06 {
    public static void main(String[] args) {

        Comparator comparator = new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                //TODO 判断有问题,年龄相同会覆盖
                return o1.age - o2.age;
            }
        };
        TreeMap<Teacher,String> treeMap = new TreeMap<>(comparator);
        treeMap.put(new Teacher("李四",55),"aaa");
        treeMap.put(new Teacher("王五",60),"bbb");
        treeMap.put(new Teacher("赵六",55),"ccc");
        System.out.println("treeMap"+treeMap);
    }
}
class Teacher{
    String name;
    int age;

    public Teacher(String name,int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

TreeMap统计字符串出现次数

package MONA.demo01;

import java.util.ArrayList;
import java.util.Set;
import java.util.TreeMap;

/**
 * TreeMap统计字符串出现次数
 * aababcabcdabcde
 * 要求输出结果:
 * a(5) b(4) c(3)
 *
 * 1.利用TreeMap key唯一特性
 * 2.循环获取每一个字符,根据获取到的字符作为key从map中取值
 *      1.如果为null,表示之前没有存过值
 *      2.不为null,表示之前存过值了,值++
 */

public class Demo01 {

    public static void main(String[] args) {
        String s = "abcdabcdbacd" ;
        TreeMap<Character,Integer> treeMap = new TreeMap<>();

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            Integer value = treeMap.get(c);
            if(value != null){
                //put('a',1)
                //之前存过值,那么value需要++
                value++;
                treeMap.put(c,value);//再次传入
                //put('a',2)
            }else{
                //之前没有存过值(第一次出现)
                treeMap.put(c,1);
            }
        }
        System.out.println(treeMap);

        StringBuffer sb = new StringBuffer();

        //a(5)  b(4)  c(3)
        Set<Character> keySet = treeMap.keySet();
        for(Character key : keySet){
            sb.append(key).append("(").append(treeMap.get(key)).append(")    ");
        }
        System.out.println(sb.toString());
    }
}

Map嵌套Map

package MONA.demo02_练习题;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 *模拟存储班级信息
 *   2017
 *       JF001 李四
 *       JF002 张三
 *       JF003 王五
 *   2016
 *       JF001 赵六
 *       JF002 田七
 */
public class Demo03 {
    public static void main(String[] args) {
        //存2017的学生信息
        HashMap<String,String> hashMap17 = new HashMap<>();
        hashMap17.put("jf001","刘德华");
        hashMap17.put("jf002","张学友");
        hashMap17.put("jf003","黎明");

        HashMap<String,String> hashMap16 = new HashMap<>();
        hashMap16.put("jf001","小红");
        hashMap16.put("jf002","小明");
        hashMap16.put("jf003","小白");

        HashMap<String,HashMap<String,String>> allHasMap = new HashMap<>();
        allHasMap.put("2017",hashMap17);
        allHasMap.put("2016",hashMap16);

        //遍历allHasMap

        //键找值
        Set<String> keySet =  allHasMap.keySet();
        for (String key : keySet) {
            HashMap<String,String> child = allHasMap.get(key);

//            System.out.println("value:" + allHasMap.get(key));
            Set<String> childKeySet = child.keySet();
            for (String chlidkey : childKeySet) {
                System.out.println("childKey = " + chlidkey + "  chlidValue = " + child.get(chlidkey));

            }
        }
        System.out.println("===============================================");
        //获取键值对
        Set<Map.Entry<String, HashMap<String, String>>> entrySet = allHasMap.entrySet();
        for(Map.Entry<String, HashMap<String, String>> entry : entrySet){
            String key = entry.getKey();
            System.out.println("key = " + key);
            HashMap<String,String> childHashMap = entry.getValue();
            for(Map.Entry<String,String> childEntry : childHashMap.entrySet()){
                String childKey = childEntry.getKey();
                String childvalue = childEntry.getValue();

                System.out.println("childKey="+childKey + "   childvalue="+childvalue);
            }
        }
    }
}
ArrayList集合嵌套HashMap集合
package MONA.demo02_练习题;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 第一个班:
 *      HashMap<String,String>
 *          "jf001","小红"
 *          "jf002","小明"
 *          "jf003","小李"
 *          "jf004","小白"
 * 第二个班:
 *      HashMap<String,String>--------
 *          "jf001","梅西"
 *          "jf002","C罗"
 *          "jf003","内马尔"
 *          "jf004","帕托"
 */
public class demo04 {
    public static void main(String[] args) {
        HashMap<String,String> hashMap17 = new HashMap<>();
        hashMap17.put("jf001","刘德华");
        hashMap17.put("jf002","张学友");
        hashMap17.put("jf003","黎明");

        HashMap<String,String> hashMap16 = new HashMap<>();
        hashMap16.put("jf001","小红");
        hashMap16.put("jf002","小明");
        hashMap16.put("jf003","小白");

        ArrayList<HashMap<String,String>> list = new ArrayList<>();
        list.add(hashMap16);
        list.add(hashMap17);

        System.out.println(list);

        //fori
        for (int i = 0; i < list.size(); i++) {
            HashMap<String, String> hashMap = list.get(i);
            Set<Map.Entry<String,String>> entrySet = hashMap.entrySet();

            for (Map.Entry<String, String> entry : entrySet) {
                System.out.println(entry.getKey() + "    " + entry.getValue());
            }
        }
        System.out.println("===============================================");
        //for
        for (HashMap<String, String> hashMap : list) {
            Set<Map.Entry<String, String>> entrySet = hashMap.entrySet();
            for (Map.Entry<String, String> entry : entrySet) {
                System.out.println(entry.getKey() + "    " + entry.getValue());
            }
        }
    }
}

 类似资料: