JAVA Key=Key模型(一对一双向唯一)

来源:互联网 发布:2016年中国老龄化数据 编辑:程序博客网 时间:2024/06/05 11:53

闲的无聊,自己琢磨了下一对一双向唯一的简易结构,很简单的东西,自己记录下,说不定以后有用。
代码如下:

(一) 定义接口

public interface OTO<K1,K2> {    /** 得到数据大小 */    int size();    /** 判断是否为空 */    boolean isEmpty();    /** 判断是否存在key,双向判断 */    boolean contains(Object key);    /** 判断 K1 是否存在key */    boolean containsKey1(K1 key);    /** 判断 K2是否存在key */    boolean containsKey2(K2 key);    /** 通过key(K1或者K2)得到值 */    Object get(Object key);    /** 通过K1得到K2值 */    K2 getK2(K1 key);    /** 通过K2得到K1值 */    K1 getK1(K2 key);    /** 添加数据 */    void put(K1 k1, K2 k2);    /** 添加数据,如果数据重复将覆盖 */    void putWithExist(K1 k1, K2 k2);    /** 添加多个数据 */    void putAll(OTO<? extends K1,? extends K2> oto);    /** 通过key(K1或K2)删除对象 */    void remove(Object key);    /** 通过K1删除对象 */    void removeByKey1(K1 k1);    /** 通过K2删除对象 */    void removeByKey2(K2 k2);    /** 清空数据 */    void clear();    /** 重写toString */    String toString();    /** 得到遍历对象Iterator */    Iterator</*? extends */K1,/*? extends*/ K2> getIterator();    /** 遍历对象Iterator,内部借口 */    interface Iterator<K1,K2> {        /** 是否存在下一个对象 */        boolean hasNext();        /** 跳到下一个对象 */        int next();        /** 跳到index的下一个对象 */        int next(int index);        /** 得到K1 */        K1 getK1();        /** 得到K2 */        K2 getK2();        /** 得到当前遍历的位置 */        int getIndex();        /** 设置遍历位置 */        void setIndex(int index);        /** 重写hashCode */        int hashCode();    }}

(二)接口实现

import java.util.ArrayList;import java.util.Objects;public class OTOTable<K1,K2> implements OTO<K1,K2> {    private ArrayList<K1> key_1 = null;    private ArrayList<K2> key_2 = null;    public OTOTable2() {        this.key_1 = new ArrayList<K1>();        this.key_2 = new ArrayList<K2>();    }    @Override    public int size() {        return key_1.size();    }    @Override    public boolean isEmpty() {        if(key_1.size() == 0){            return true;        }        return false;    }    @SuppressWarnings("unchecked")    @Override    public boolean contains(Object key) {        if(containsKey1((K1)key) || containsKey2((K2)key)) {            return true;        }        return false;    }    @Override    public boolean containsKey1(K1 key) {        boolean flag = false;        for (int i = 0; i < key_1.size(); i++) {            if(key_1.get(i).equals(key)){                flag = true;            }        }        return flag;    }    @Override    public boolean containsKey2(K2 key) {        boolean flag = false;        for (int i = 0; i < key_2.size(); i++) {            if(key_2.get(i).equals(key)){                flag = true;            }        }        return flag;    }    @SuppressWarnings("unchecked")    @Override    public Object get(Object key) {        if(getK2((K1)key) != null) {            return getK2((K1)key);        } else if(getK1((K2)key) != null){            return getK1((K2)key);        }        return null;    }    @Override    public K2 getK2(K1 key) {        for (int i = 0; i < key_1.size(); i++) {            K1 obj = (K1)key_1.get(i);            if(obj.equals(key)){                return (K2)key_2.get(i);            }        }        return null;    }    @Override    public K1 getK1(K2 key) {        for (int i = 0; i < key_1.size(); i++) {            K2 obj = (K2)key_2.get(i);            if(obj.equals(key)){                return (K1)key_1.get(i);            }        }        return null;    }    @Override    public void put(K1 k1, K2 k2) {        boolean flag = true;        for (int i = 0; i < key_1.size(); i++) {            if(key_1.get(i).equals(k1) || key_2.get(i).equals(k2)){                flag = false;            }        }        if(flag){            key_1.add(k1);            key_2.add(k2);        }    }    @Override    public void putWithExist(K1 k1, K2 k2) {        for (int i = 0; i < key_1.size(); i++) {            if(key_1.get(i).equals(k1)){                key_2.set(i, k2);            } else if(key_2.get(i).equals(k2)) {                key_1.set(i, k1);            }        }    }    @SuppressWarnings({ "rawtypes", "unchecked" })    @Override    public void putAll(OTO<? extends K1, ? extends K2> oto) {        OTO.Iterator iter = oto.getIterator();        while(iter.next() != -1) {            key_1.add((K1)iter.getK1());            key_2.add((K2)iter.getK2());        }    }    @SuppressWarnings("unchecked")    @Override    public void remove(Object key) {        removeByKey1((K1)key);        removeByKey2((K2)key);    }    @Override    public void removeByKey1(K1 k1) {        for (int i = 0; i < key_1.size(); i++) {            if(key_1.get(i).equals(k1)){                key_1.remove(i);                key_2.remove(i);            }        }    }    @Override    public void removeByKey2(K2 k2) {        for (int i = 0; i < key_2.size(); i++) {            if(key_2.get(i).equals(k2)){                key_1.remove(i);                key_2.remove(i);            }        }    }    @Override    public void clear() {        this.key_1.clear();        this.key_2.clear();    }    @Override    public OTO.Iterator</*? extends*/ K1, /*? extends*/ K2> getIterator() {        return new Iterator<K1, K2>();    }    @SuppressWarnings("hiding")    class Iterator<K1,K2> implements OTO.Iterator<K1, K2> {        private int index;        private K1 k1 = null;        private K2 k2 = null;        public Iterator(){            this.index = 0;        }        public Iterator(int index){            this.index = index;        }        @Override        public boolean hasNext() {            if(this.index == key_1.size()) {                return false;            }            return true;        }        @SuppressWarnings("unchecked")        @Override        public int next() {            if(this.index == key_1.size()) {                return -1;            }            this.k1 = (K1) key_1.get(index);            this.k2 = (K2) key_2.get(index);            this.index ++;            return 0;        }        @Override        public int next(int index) {            this.index = index;            return next();        }        public K1 getK1() {            return k1;        }        public K2 getK2() {            return k2;        }        public int getIndex() {            return index;        }        public void setIndex(int index) {            this.index = index;        }        public int hashCode() {            return Objects.hashCode(getK1()) ^ Objects.hashCode(getK2());        }    }    public String toString(){        StringBuffer buffer = new StringBuffer();        buffer.append("{");        for (int i = 0; i < key_1.size(); i++) {            buffer.append(key_1.get(i) + "=" + key_2.get(i));            if(i + 1 != key_1.size()) {                buffer.append(",");            }        }        buffer.append("}");        return buffer.toString();    }}

(三)TEST

class Test{    public static void main(String[] args) {        test();    }    public static void test() {        OTO<Integer,String> oto = new OTOTable<Integer,String>();        //TEST size        System.out.println("size: " + oto.size());        //TEST put        oto.put(1, "北京");        oto.put(2, "上海");        oto.put(1, "北京南");        ////TEST putWithExist        oto.putWithExist(1, "北京南");        //TEST size        System.out.println("size: " + oto.size());        //TEST get        System.out.println("K2: " + oto.getK2(1));        System.out.println("K1: " + oto.getK1("北京"));        System.out.println("value: " + oto.get(true));        System.out.println("value: " + oto.get("上海"));        //TEST toString        System.out.println("toString: " + oto.toString());        //TEST containsKey1        System.out.println("containsKey1(true): " + oto.containsKey1(1));        System.out.println("containsKey1(true): " + oto.containsKey1(2));        System.out.println("containsKey1(false): " + oto.containsKey1(3));        //TEST containsKey2        System.out.println("containsKey2(false): " + oto.containsKey2("北京"));        System.out.println("containsKey2(true): " + oto.containsKey2("上海"));        System.out.println("containsKey2(true): " + oto.containsKey2("北京南"));        //TEST contains        System.out.println("contains(false): " + oto.contains("北京"));        System.out.println("contains(true): " + oto.contains("上海"));        System.out.println("contains(true): " + oto.contains("北京南"));        System.out.println("contains(true): " + oto.contains(1));        System.out.println("contains(true): " + oto.contains(2));        System.out.println("contains(false): " + oto.contains(3));        //TEST Iterator        OTO.Iterator<Integer,String> iter = oto.getIterator();        while(iter.hasNext()) {            iter.next();            System.out.println(iter.getK1() + " = " + iter.getK2());        }        OTO.Iterator<Integer,String> iter1 = oto.getIterator();        while(iter1.next() != -1) {            System.out.println(iter1.getK1() + " = " + iter1.getK2());        }        //TEST putAll        OTO<Integer,String> oto2 = new OTOTable<Integer,String>();        oto2.putAll(oto);        System.out.println(oto2.toString());        //TEST remove        oto2.put(3, "北京1");        oto2.put(4, "上海1");        oto2.put(5, "北京南1");        oto2.removeByKey1(1);        oto2.removeByKey2("上海");        oto2.remove(3);        System.out.println(oto2.toString());        //TEST clear        oto.clear();        System.out.println(oto.toString());    }}

console打印结果如下:
size: 0
size: 2
K2: 北京南
K1: null
value: null
value: 2
toString: {1=北京南,2=上海}
containsKey1(true): true
containsKey1(true): true
containsKey1(false): false
containsKey2(false): false
containsKey2(true): true
containsKey2(true): true
contains(false): false
contains(true): true
contains(true): true
contains(true): true
contains(true): true
contains(false): false
1 = 北京南
2 = 上海
1 = 北京南
2 = 上海
{1=北京南,2=上海}
{4=上海1,5=北京南1}
{}

0 0
原创粉丝点击