Home > Enterprise >  Add to private HashMap<Integer, HashSet<exampleObject>> test in java
Add to private HashMap<Integer, HashSet<exampleObject>> test in java

Time:05-30

I'm exploring hash set and hash map, I'm looking to add to this:

private HashMap<Integer, HashSet<exampleObject>> test;

test = new HashMap<Integer, HashSet<exampleObject>>();

for example the exampleObject is made up from int, String, String,

I've tried (as well as other things)

test.put(1, exampleObject.add(new exampleObject(1,"a","b")));

Any help would be appreciated

CodePudding user response:

Your add() method return boolean and can't be used in your example. And you need to create a data structure HashSet. For example:

private static HashMap<Integer, HashSet<exampleObject>> test = new HashMap<>();
private static HashSet<exampleObject> hashSet = new HashSet<>(); 

public static void main(String[] args) {
    hashSet.add(new exampleObject("key", "value")); // <-- returns boolean
    test.put(1, hashSet);
    for (exampleObject element : test.get(1)) {
        System.out.println(element);
    }
}

P.S. exampleObject have 2 String fields here.

  • Related