Java Dakedly HashSet泛型问题

时间:2013-11-15 22:11:22

标签: java generics data-structures hashmap hashset

作为我现已结束的问题的后续行动:Java Two-Way Linked Set data-structure。我对我所描述的数据结构实施了一种非常幼稚的解释。

以下是上一个问题的视觉辅助:

enter image description here

输出:

Set<Object> set1 = s.get("C", RIGHT);
Set<Object> set2 = s.get(1, LEFT);

[2, 3, 4]
[A, B]

正如您所看到的,我正确检索了每个查询的关联集,但我对如何实现get()函数以智能确定用户是否通过类型U感到困惑。或T不使用旗帜。

由于类型擦除,我无法在DoublyHashSet类中执行以下操作:

public Set<U> get(T t) {
    return leftSet.get(t);
}

public Set<T> get(U u) {
    return rightSet.get(u);
}

我认为那些方法签名是不同的......


代码

DoublyHashSet.java

package Graph;

import java.util.Set;

public class DoublyHashSet<T, U> {
    public static final int LEFT = 0;
    public static final int RIGHT = 1;
    private NodeSet<T, U> leftSet;
    private NodeSet<U, T> rightSet;

    public DoublyHashSet() {
        leftSet  = new NodeSet<>();
        rightSet = new NodeSet<>();

        leftSet.setCompliment(rightSet);
        rightSet.setCompliment(leftSet);
    }

    public void add(T t, U u) {
        leftSet.add(t, u);
        rightSet.add(u, t);
    }

    public Set<Object> get(Object o, int side) {
        boolean value = (side & 1) == 0;

        if (value) {
            return (Set<Object>) leftSet.get((T) o);
        } else {
            return (Set<Object>) rightSet.get((U) o);
        }
    }

    public static void main(String[] args) {
        DoublyHashSet<Integer, String> s = new DoublyHashSet<Integer, String>();

        s.add(1, "A");
        s.add(1, "B");
        s.add(2, "A");
        s.add(2, "C");
        s.add(2, "D");
        s.add(3, "C");
        s.add(4, "C");

        Set<Object> set1 = s.get("C", RIGHT);
        Set<Object> set2 = s.get(1, LEFT);

        System.out.println(set1);
        System.out.println(set2);
    }
}

NodeSet.java

package Graph;

import java.util.HashSet;
import java.util.Set;

public class NodeSet<T, U> extends HashSet<Node<T, U>> {
    private static final long serialVersionUID = 1L;
    private NodeSet<U, T> compliment;

    public NodeSet() {
        this(null);
    }

    public NodeSet(NodeSet<U, T> compliment) {
        super();

        this.compliment = compliment;
    }

    public NodeSet<U, T> getCompliment() {
        return compliment;
    }

    public void setCompliment(NodeSet<U, T> compliment) {
        this.compliment = compliment;
    }

    public Node<T, U> find(T data) {
        for (Node<T, U> node : this) {
            if (node.getData() == data) {
                return node;
            }
        }

        return null;
    }

    public void add(T t, U u) {
        Node<T, U> tNode = find(t);
        Node<U, T> uNode = null;

        if (tNode == null) {
            tNode = new Node<T, U>(t);
        }

        if (compliment != null) {
            uNode = compliment.find(u);

            if (uNode == null) {
                uNode = new Node<U, T>(u);
            }
        }

        tNode.getConnections().add(uNode);
        this.add(tNode);
    }

    public Set<U> get(T t) {
        Set<U> set = null;
        Node<T, U> node = find(t);

        if (node != null) {
            set = new HashSet<U>();
            for (Node<U, T> connection : node.getConnections()) {
                if (connection != null)
                    set.add(connection.getData());
            }
        }

        return set;
    }
}

Node.java

package Graph;

import java.util.HashSet;
import java.util.Set;

public class Node<T, U> implements Comparable<Node<T, U>> {
    private T data;
    private Set<Node<U, T>> connections;

    public Node() {
        this(null);
    }

    public Node(T data) {
        this(data, new HashSet<Node<U, T>>());
    }

    public Node(T data, Set<Node<U, T>> connections) {
        this.data = data;
        this.connections = connections;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Set<Node<U, T>> getConnections() {
        return connections;
    }

    public void setConnections(Set<Node<U, T>> connections) {
        this.connections = connections;
    }

    @Override
    public int compareTo(Node<T, U> other) {
        return this.getData().toString().compareTo(other.getData().toString());
    }

    @Override
    public String toString() {
        return "Node [data=" + data + "]";
    }
}

0 个答案:

没有答案