Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

overriding entries in Map

Posted on 2014-01-09
4
Medium Priority
?
158 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

636 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