java - Returning keys based on multiple sets of values -


i trying store data format , retrieving

i want shown below

  key               value      b1       -->   payername  ----> "wpn", "wpfnb", "dgeft", "xbthy"       b2      -->    payername  ----> "ssswpn", "wpfsssnb", "gggdgeft", "xbtyyyyhy" 

so b1 there payername , b2 till have created data structure mapping of payername "wpn", "wpfnb", "dgeft", "xbthy" , payer name "ssswpn", "wpfsssnb", "gggdgeft"

till below custom data structure have created reflects mapping between payername "wpn", "wpfnb", "dgeft", "xbthy" , payername "ssswpn", "wpfsssnb", "gggdgeft", "xbtyyyyhy" want link them b1 , b2 correspondingly also

folks please advise how can modify , attach them key b1 associatedwith payer name further values "wpn", "wpfnb", "dgeft", "xbthy" , other case in b2 associated payername , associated values "ssswpn", "wpfsssnb", "gggdgeft", "xbtyyyyhy"

so below custom data structure have created in below data structure b1 nad b2 not associate please advise how can modify below custom data structure in can associate b1 , b2 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"));       } } 

you may want take @ java.util.map interface. common implementation of interface hashmap.

here 1 way accomplish need using these tools. adding values java.util.list, there many other ways can this.

i using @test annotation running junit, can call through main() method in example.

since may possible value can associated more 1 key, want return keys have value. 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 map="+payernames);      list b1list = (list)payernames.get("b1");      system.out.println("just b1 values=" + b1list);     system.out.println("all keys wpn in them=" + getkeysbyvalue(payernames, "wpn"));  }  private static <t, e> set<t> getkeysbyvalue(map<t, e> map, e value) {     set<t> keys = new hashset<>();     (map.entry<t, e> entry : map.entryset()) {          list<string> keyslist = (list)entry.getvalue();         (string valuewithkey : keyslist) {             if (objects.equals(value, valuewithkey)) {                 keys.add(entry.getkey());             }         }     }     return keys; } 

output:

everything in map={b2=[ssswpn, wpfsssnb, gggdgeft, xbtyyyyhy], b1=[wpn, wpfnb, dgeft, xbthy]} b1 values=[wpn, wpfnb, dgeft, xbthy] keys wpn in them=[b1] 

the second method adapted answer provided vitali fedorenko.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -