Solved

overriding entries in Map

Posted on 2014-01-09
4
156 Views
Last Modified: 2014-01-10
hi guys

I have

1.

public class CacheData{
private String id;
private String name;
private String key;

set() methods;
get() methods;
}

Open in new window


2.
public class CacheImpl{
private void cacheImp{
String webserviceId = getWebserviceId(); //returns a uniqe webserviceId ;
Map<String,CacheData> cacheMap = new HashMap<String,CacheData>();
CacheData cdata = new CacheData();
cdata.setId("XYZ");
cacheMap.put(webserviceId , cdata);
Map cache = construct(cacheMap);
}


private Map construct(Map cacheMap){
CacheData newCData = //make webserivice call and construct CacheData;
 cacheMap.put(webserviceId , newCData); //same webserviceId key is put again  the second time in cacheMap , So now does the cacheMap contain only one enrty 
with webserviceId  as the key and newCData as the value.
Does it override the 'put' which was made earlier?
}

}//end class

Open in new window


Thanks.
0
Comment
Question by:royjayd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 36

Expert Comment

by:mccarl
ID: 39770040
The answer is: YES.

If you do multiple .put() calls with the same "key" value, then the earlier entries get "replaced" by the last one.
0
 
LVL 36

Accepted Solution

by:
mccarl earned 500 total points
ID: 39770043
For example:
Map<String, String> testMap = new HashMap<String, String>();
testMap.put("abc", "123");
testMap.put("abc", "456");
testMap.put("abc", "789");

System.out.println("The map is: " + testMap);

Open in new window

The map will only have one entry, and it's value will be "789"
0
 

Author Comment

by:royjayd
ID: 39770061
ok.
Do i have to override the equals() and hashcode() in my CacheData class?

Thanks.
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39770069
From just the above code... No. You can but it's not required. You are only using CacheData in as "values" in the map. If you were to use that class as the "key" to the map, then yes you should implement your own equals and hashcode.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question