基于多组值返回键

时间:2015-06-19 17:07:02

标签: java

我基本上试图将数据存储到这种格式并检索它

我想要下面显示的内容

  Key               Value

    B1       -->   payerName  ----> "wpn", "wpfnb", "dgeft", "xbthy"


    B2      -->    payerName  ----> "SSSwpn", "wpfSSSnb", "GGGdgeft", "xbtYYYYhy"

所以B1有payerName,类似于B2,直到现在我已经创建了一个数据结构,用于映射payerName和" wpn"," wpfnb"," dgeft"," xbthy"和另一个付款人姓名" SSSwpn"," wpfSSSnb"," GGGdgeft"

到目前为止,我创建的以下自定义数据结构反映了payerName与" wpn"," wpfnb"," dgeft",&#34之间的映射; xbthy"另一个payerName与" SSSwpn"," wpfSSSnb"," GGGdgeft"," xbtYYYYhy"但现在我想将它们与B1和B2相关联,也相应地

伙计们请告知我们如何修改并用Key附加它们,这样最终B1将与付款人姓名进一步关联" wpn"," wpfnb"," dgeft&# 34;," xbthy"和其他情况类似,其中B2与payerName相关联,最终与值相关联" SSSwpn"," wpfSSSnb"," GGGdgeft"," xbtYYYYhy& #34;

以下是我创建的自定义数据结构,但在下面的数据结构B1和B2不是关联的,所以请告诉我如何修改我的下面的自定义数据结构,我可以最终关联B1和B2

 class DictionaryNode 
{
    DictionaryNode next[];
    String data;
    DictionaryNode()
    {
        next=new DictionaryNode[128];
        for(int i=0;i<next.length;i++)
        {
            next[i]=null;
        }
        data=null;
    }
}

 class Dictionary 
{
    DictionaryNode root;
    Dictionary()
    {
        root = new DictionaryNode();
    }
    public boolean add(String key,String data)
    {
        char[]ch=key.toCharArray();
        DictionaryNode current=root;
        for(int i=0;i<ch.length;i++)
        {
            if(current.next[ch[0]]==null)
            {
                current.next[ch[0]]=new DictionaryNode();
            }
            current=current.next[ch[0]];
        }
        if(current.data==null)
        {
            current.data=data;
            return true;
        }
        else
        {
            return false;
        }
    }
    public String search(String key)
    {
        char[]ch=key.toCharArray();
        DictionaryNode current=root;
        for(int i=0;i<ch.length;i++)
        {
            if(current.next[ch[0]]==null)
            {
                return null;
            }
            else
            {
                current=current.next[ch[0]];
            }
        }
        return current.data;
    }
}

public class main
{
    public static void main(String []args)
    {
        Dictionary d=new Dictionary();
        d.add("wpn", "AAA");
        d.add("wpfnb", "AAA");
        d.add("dgeft", "AAA");
        d.add("dgeft", "BBB");
        d.add("SSSwpn", "AAA");
        d.add("wpfSSSnb", "BBB");
        d.add("GGGdgeft", "BBB");
        d.add("xbtYYYYhy", "BBB");
        System.out.println(d.search("wpn"));
        System.out.println(d.search("SSSwpn"));


    }
}

1 个答案:

答案 0 :(得分:0)

您可能需要查看java.util.Map界面。该接口的常见实现是HashMap

以下是使用这些工具完成所需工作的一种方法。我将值添加到java.util.List,但还有很多其他方法可以做到这一点。

我正在使用@Test注释,因为我正在使用Junit快速运行它,但您可以像在示例中一样通过main()方法调用它。

由于任何值都可能与多个键相关联,因此您可能希望返回具有该值的所有键。这就是下面的第二种方法。

@Test
public void hashMapExample() {
    Map payerNames = new HashMap<>(8); //if you're using Java 7 or higher

    String[] b1Values = {"wpn", "wpfnb","dgeft","xbthy"};
    payerNames.put("B1", Arrays.asList(b1Values));

    String[] b2Values = {"SSSwpn", "wpfSSSnb", "GGGdgeft", "xbtYYYYhy"};
    payerNames.put("B2", Arrays.asList(b2Values));

    System.out.println("everything in the map="+payerNames);

    List b1List = (List)payerNames.get("B1");

    System.out.println("just b1 values=" + b1List);
    System.out.println("all keys with wpn in them=" + getKeysByValue(payerNames, "wpn"));

}

private static <T, E> Set<T> getKeysByValue(Map<T, E> map, E value) {
    Set<T> keys = new HashSet<>();
    for (Map.Entry<T, E> entry : map.entrySet()) {

        List<String> keysList = (List)entry.getValue();
        for (String valueWithKey : keysList) {
            if (Objects.equals(value, valueWithKey)) {
                keys.add(entry.getKey());
            }
        }
    }
    return keys;
}

输出:

everything in the map={B2=[SSSwpn, wpfSSSnb, GGGdgeft, xbtYYYYhy], B1=[wpn, wpfnb, dgeft, xbthy]}
just b1 values=[wpn, wpfnb, dgeft, xbthy]
all keys with wpn in them=[B1]

第二种方法改编自Vitali Fedorenko提供的answer