Solved

overriding entries in Map

Posted on 2014-01-09
4
150 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
Comment Utility
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
Comment Utility
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
Comment Utility
ok.
Do i have to override the equals() and hashcode() in my CacheData class?

Thanks.
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now