java - How to remove duplicate Key-Value pair in Hashmap ? - Not just duplicate Key or Value -


i have hashmap in java retreaving apis of softwares system. so, have this:

[softwareid, softwareapi]

when ask apis software systems, receive:

[ [softwareid, softwareapi], [softwareid, softwareapi], [softwareid, softwareapi] ]

but have problem, need remove duplicates softwareapis per software.

for example when iterate on hashmap get,

[ [0, a], [0, b], [0, a], [0, a] ];  [ [1, a], [1, b], [1, b], [1, c] ];  [ [2, a], [2, b], [2, a] ]; 

but need remove duplicated pairs, this:

[ [0, a], [0, b] ];  [ [1, a], [1, b], [1, c] ];  [ [2, a], [2, b] ] 

just add code information here part of code:

// hashmap apis per user/systems hashmap<integer, set<api>> apispersystem = new hashmap<integer, set<api>>();  /**  * stores api in data model  * @param system user  * @param api item  * @return newly added api  */ public api addapis(int system, string api) {     api r = new api(system,api);     apis.add(r);     set<api> systemapis = apisperuser.get(system);     if (systemapis == null) {         systemapis = new hashset<api>();     }     apisperuser.put(system, systemapis);     systemapis.add(r);     systems.add(system);     apislist.add(api);     return r; }  // apis per systemfrom outside. public hashmap<integer, set<api>> getapispersystem() {     return apispersystem; } 

from java's set method add documentation:

adds specified element e set if set contains no element e2 such (e==null ? e2==null : e.equals(e2))

when add elements set, not considered equal.

you need check hashcode , equals method of api object, , override them.

this done in tdd.

hashcode used when using hashset (which case).

also see this question hashset , equals methods


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -