0

Im new to Java so please forgive my ignorance,I am trying my best to not ask a stupid question.

I need to write a method that takes two arguments, one being the key for my map (String) the other is the value (my map has values that are sets of a user defined object called Object that has three string values itself).

so far I have:

public void addMapObject(String aKey, Object anObject)//what would the anObject arguement look like?

{ Set objects = new HashSet<>(); objects.add(new Object("","",""); //how do I initialize these values? MyMap.put(aKey, objects); }

Apologies if this is not clear I will be happy to clarify if needed.

Thank you very much for the help it has clicked now.

Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
WillD
  • 151
  • 1
  • 10
  • This is not clear. Why can't you just do `myMap.put(aKey, anObject)`? – Oliver Charlesworth Mar 26 '16 at 12:40
  • @OliverCharlesworth So my method argument would look something like addMapObject("Key1", what do I put here to initialise the object state?) – WillD Mar 26 '16 at 12:45
  • I don't know - what do you mean by "the object state"? It's really unclear what type of objects you're talking about here. – Oliver Charlesworth Mar 26 '16 at 12:47
  • This question is really unclear. You declared a class `Object`? If you did, you should really choose another name for the class, since `java.lang.Object` is already a well known class in Java. Where is the declaration of your class `Object`? Also, what is `addMapObject` supposed to do? – Stefan Dollase Mar 26 '16 at 12:52

2 Answers2

1

You first have to create an object, like so:

pubic class MyObject{
  private String something;
  private Integer something2;
  private Double something3;
  //or whatever data types you want

  //Generate getters and setters
}

So you now have an object that holds something.

After that, you have to initialize a map to hold your object so in your method above or as a global "field" in that same class you need to:

Map<String,MyObject> map = new HashMap<String,MyObject>();

Your method signature should then include MyObject instead of Object and you can use:

map.put(aKey, anObject)
neal
  • 880
  • 6
  • 11
  • Sorry if it wasn't clear guys, I have got up to this point! I explained it badly I apologise. Now from here how do I actually put an object into the map? – WillD Mar 26 '16 at 12:58
  • As Oliver Charlesworth said: map.put(aKey,anObject). If you want to initialize an object while putting it in the map use: map.put(aKey, new MyObject()); – neal Mar 26 '16 at 12:59
-1

Class Object is java defined class and is part of java.lang package. Each class is sub class of this class. I feel you should create a separate class. You can create a class like below

public class Bean{
    String valueOne;
    String valueTwo;
    String valueThree;
    public Bean(String valueOne, String valueTwo, String valueThree) {
        super();
        this.valueOne = valueOne;
        this.valueTwo = valueTwo;
        this.valueThree = valueThree;
    }

}

You need to change the signature of the method

public void addMapObject(String aKey, Bean anObject) {
    Set<Object> objects = new HashSet<>();
    objects.add(anObject);
    MyMap.put(aKey, objects);
}

And then you asked as how to initialize, you would do it in the caller method as below

public void theCallerMethod(){
    addMapObject("YourKey", new Bean("YourActualvalueOne","YourActualvalueTwo","YourActualvalueThree"));
}

But once you do this you should also take care of the equals() and hashcode() methods as you need to add the objects of your class in an HashSet. These methods are defined in base class Object.java. Details as why you need to do this, I suggest you to study this and can refer below link Why do I need to override the equals and hashCode methods in Java?

Your class bean would finally look like below

public static class Bean {
    String valueOne;
    String valueTwo;
    String valueThree;

    public Bean(String valueOne, String valueTwo, String valueThree) {
        super();
        this.valueOne = valueOne;
        this.valueTwo = valueTwo;
        this.valueThree = valueThree;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((valueOne == null) ? 0 : valueOne.hashCode());
        result = prime * result + ((valueThree == null) ? 0 : valueThree.hashCode());
        result = prime * result + ((valueTwo == null) ? 0 : valueTwo.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Bean other = (Bean) obj;
        if (valueOne == null) {
            if (other.valueOne != null)
                return false;
        } else if (!valueOne.equals(other.valueOne))
            return false;
        if (valueThree == null) {
            if (other.valueThree != null)
                return false;
        } else if (!valueThree.equals(other.valueThree))
            return false;
        if (valueTwo == null) {
            if (other.valueTwo != null)
                return false;
        } else if (!valueTwo.equals(other.valueTwo))
            return false;
        return true;
    }
}
Community
  • 1
  • 1
nits.kk
  • 5,204
  • 4
  • 33
  • 55