设为首页 加入收藏

TOP

java -- Map接口和可变参数(二)
2023-07-25 21:32:22 】 浏览:83
Tags:java Map
允许null值

/*
    java.util.TreeMap
        底层数据结构是红黑树  键 排序 具有唯一性  不允许null键 允许null值
        构造方法:
            public TreeMap() 空参构建, 集合中的键必须实现自然排序接口 Comparable 的方法 CompareTo
            public TreeMap(Comparator<? super K> comparator) 需传入比较器对象
 */
public class Demo {
    public static void main(String[] args) {
        TreeMap<Person, String> map = new TreeMap<>();
        map.put(new Person("张三",12),"bj");
        map.put(new Person("张三",14),"sh");
        map.put(new Person("李四",15),null);
        map.put(new Person("王五",11),"gz");
        map.put(new Person("宫本",19),"jp");
        // 不允许null键
        // map.put(null,"us");
        System.out.println(map);
        // 获取第一个键
        System.out.println(map.firstKey());
        // 获取最后一个键
        System.out.println(map.lastKey());
        // 获取第一个键值对
        System.out.println(map.firstEntry());
        // 获取最后一个键值对
        System.out.println(map.lastEntry());
        // 获取 按排序方法 取索引 >= 指定键的最小键(>=指定键并距离最近)
        System.out.println(map.ceilingKey(new Person("李四", 13)));
        // 获取 按排序方法 取索引 >= 指定键的最小键值对(>=指定键并距离最近)
        System.out.println(map.ceilingEntry(new Person("李四", 13)));
        // 获取 按排序方法 取索引 <= 指定键的最小键(<=指定键并距离最近)
        System.out.println(map.floorKey(new Person("李四", 13)));
        // 获取 按排序方法 取索引 <= 指定键的最小键值对(<=指定键并距离最近)
        System.out.println(map.floorEntry(new Person("李四", 13)));
        System.out.println("=================");

        TreeMap<Person, String> treeMap = new TreeMap<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                if (o1.age == o2.age) {
                    return o2.name.compareTo(o1.name);
                }
                return o1.age - o2.age;
            }
        });
        treeMap.put(new Person("张三",12),"bj");
        treeMap.put(new Person("张三",14),"sh");
        treeMap.put(new Person("李四",15),null);
        treeMap.put(new Person("王五",11),"gz");
        treeMap.put(new Person("宫本",19),"jp");
        System.out.println(treeMap);


    }
}


class Person implements Comparable<Person> {
    String name;
    int age;

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

    @Override
    public int compareTo(Person o) {
        if (o.age == this.age) {
            return o.name.compareTo(this.name);
        }
        return o.age - this.age;
    }

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

HashMap

/*
    java.util.HashMap
        底层数据结构是哈希表 允许null键和null值  键是无序的 具有唯一性
        先比较hashCode
            不同 元素不相同
            相同 继续比较equals
                相同
                不同
        因此必须重写hashCode和equals方法
 */
public class Demo {
    public static void main(String[] args) {
        Map<Person, String> map = new HashMap<Person, String>();
        map.put(new Person("张三",12),"bj");
        map.put(new Person("李四",12),"sh");
        map.put(new Person("王五",12),"gz");
        map.put(new Person("宫本",12),"jp");
        map.put(null,"us");

        Set<Person> peopleSet = map.keySet();
        for (Person person : peopleSet) {
            if (person == null) {
                System.out.println(person + " " + map.get(person));
                continue;
            }
            System.out.println(person.getName() + " " + person.getAge() + " " + map.get(person));
        }

        Set<Map.Entry<Person, String>> entries = ma
首页 上一页 1 2 3 4 下一页 尾页 2/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇JVM:并发的可达性分析 下一篇once do, do it well

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目