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