跳至主要內容

自定义Map类

wangdx大约 2 分钟

自定义 Map 类

CURD

案例
package com.yix.map;

/**
 * @author wangdx
 */
public interface IMap<K, V> {
    public V put(K key, V value);

    public V get(K key);

    public int size();

    public static <K, V> IMap<K, V> getInstance() {
        return new BinaryTreeMapImpl<K, V>();
    }

    ;
}

class BinaryTreeMapImpl<K, V> implements IMap<K, V> {
    //树结构定义,必须把key和value进行一次包装
    private class Entry<K, V> implements Comparable<Entry<K, V>> {
        private K key;
        private V value;

        public Entry(K key, V value) {
            this.key = key;
            this.value = value;
        }

        @Override
        public int compareTo(Entry<K, V> o) {
            return ((Comparable) this.key).compareTo(o.key);
        }
    }

    private class Node {
        private Entry<K, V> data;
        private Node left;
        private Node right;

        public Node(Entry<K, V> data) {
            this.data = data;
        }

        public V addNode(Node newNode) {
            if (this.data.compareTo(newNode.data) < 0) {
                if (this.right == null) {
                    this.right = newNode;
                } else {
                    return this.right.addNode(newNode);
                }
            } else if (this.data.compareTo(newNode.data) > 0) {
                if (this.left == null) {
                    this.left = newNode;
                } else {
                    return this.left.addNode(newNode);
                }
            } else {
                V old = this.data.value;
                this.data.value = newNode.data.value;
                return old;
            }
            return null;
        }

        public V getNode(K key) {
            if (this.data.key.equals(key)) {
                return this.data.value;
            } else {
                if (((Comparable) this.data.key).compareTo(key) < 0) {
                    if (this.right != null) {
                        return this.right.getNode(key);
                    } else {
                        return null;
                    }
                } else {
                    if (this.left != null) {
                        return this.left.getNode(key);
                    } else {
                        return null;
                    }
                }
            }
        }
    }

    private Node root;
    private int count;

    @Override
    public V put(K key, V value) {
        if (key == null || value == null) {
            throw new NullPointerException("保存数据的key和value不允许为空!");
        }
        if (!(key instanceof Comparable)) {
            throw new ClassCastException("作为key必须实现Comparable");
        }
        Entry<K, V> entry = new Entry<>(key, value);
        Node newNode = new Node(entry);
        V result = null;
        if (this.root == null) {
            this.root = newNode;
        } else {
            result = this.root.addNode(newNode);
        }
        if (result == null) {
            this.count++;
        }
        return result;
    }

    @Override
    public V get(K key) {
        if (key == null) {
            throw new NullPointerException("查询数据的key不允许为空!");
        }
        if (!(key instanceof Comparable)) {
            throw new ClassCastException("作为key必须实现Comparable");
        }
        return this.root.getNode(key);
    }

    @Override
    public int size() {
        return this.count;
    }
}

class Book implements Comparable<Book> {
    private String title;
    private String author;
    private double price;

    public Book(String title, String author, double price) {
        this.title = title;
        this.author = author;
        this.price = price;
    }

    @Override
    public String toString() {
        return "【Book】图书名称:" + this.title + "、图书作者" + this.author + "、图书价格:" + this.price;
    }

    @Override
    public int compareTo(Book o) {
        if (this.price > o.price) {
            return 1;
        } else if (this.price < o.price) {
            return -1;
        } else {
            return 0;
        }
    }
}

class Test {
    public static void main(String[] args) {
        IMap<String, Book> map = IMap.getInstance();
        System.out.println("【保存数据】" + map.put("java", new Book("Java入门到精通", "李兴华", 99.8)));
        System.out.println("【保存数据】" + map.put("Python", new Book("Python入门到精通", "李兴华", 199.8)));
        System.out.println("【保存数据】" + map.put("java", new Book("Java入门到精通二版", "李兴华", 299.8)));
        System.out.println("【获取数据】" + map.get("java"));
        System.out.println("【获取数据】" + map.get("Python"));
        System.out.println("【获取数据】" + map.get("go"));
        System.out.println("【数据大小】" + map.size());
    }
}
上次编辑于: