Solved

overriding entries in Map

Posted on 2014-01-09
4
155 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
  • 3
4 Comments
 
LVL 35

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 35

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 35

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

Suggested Solutions

Title # Comments Views Activity
Java Restore security prompts not working 10 133
Crystal Reports Licensing Questions 4 61
spring maven example issues 3 42
Java array 21 84
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

749 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