?
Solved

overriding entries in Map

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

Technology Partners: 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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 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