-2

I am basically trying to store the data into this format and retrieving it

I want something like this shown below

  Key               Value

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


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

so for B1 there is payerName and similarly for B2 till now i have created a data structure that do the mapping of payerName with "wpn", "wpfnb", "dgeft", "xbthy" and another payer name with "SSSwpn", "wpfSSSnb", "GGGdgeft"

till now the below custom data structure that i have created reflects the mapping between payerName with "wpn", "wpfnb", "dgeft", "xbthy" and another payerName with "SSSwpn", "wpfSSSnb", "GGGdgeft", "xbtYYYYhy" but now i want to link them with B1 and B2 also correspondingly also

folks please advise how can we modify and attach them with Key so finally B1 will be associatedwith payer name further with values "wpn", "wpfnb", "dgeft", "xbthy" and similarly other case in which B2 is associated with payerName and which is finally associated with values "SSSwpn", "wpfSSSnb", "GGGdgeft", "xbtYYYYhy"

so below is the custom data structure i have created but in this below data structure B1 nad B2 is not associate so please advise how can i modify my below custom data structure in which i can associate B1 and B2 also finally

 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"));


    }
}
riddle_me_this
  • 8,575
  • 10
  • 55
  • 80
  • Confused by your question - what do you mean by "associate B1 and B2"? Are you writing that when you search for a value from key B1, you also want to retrieve the key B2 if it contains the same value? – riddle_me_this Jun 19 '15 at 18:20
  • I just want to say that when I retriever B1 I should get set of associated values with it that is wpn", "wpfnb", "dgeft", "xbthy – sdsdf dfdg Jun 19 '15 at 18:26
  • Similarly when I retrieve B2 I should get set of values SSSwpn", "wpfSSSnb", "GGGdgeft", "xbtYYYYhy" – sdsdf dfdg Jun 19 '15 at 18:27

1 Answers1

0

You may want to take a look at the java.util.Map interface. A common implementation of that interface is a HashMap.

Here is one way to accomplish what you need using these tools. I am adding the values to a java.util.List, but there are many other ways you can do this.

I am using the @Test annotation as I am quickly running it with Junit, but you can call it through a main() method as you do in your example.

Since it may be possible that any value can be associated with more than one key, you likely want to return all the keys that have the value. This is what the second method below does.

@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;
}

Output:

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]

The second method is adapted from this answer provided by Vitali Fedorenko.

Community
  • 1
  • 1
riddle_me_this
  • 8,575
  • 10
  • 55
  • 80