1

I try to refresh my Java skills. But now I stuck at a hashmap. I don't get the right object back. Here is my example code:

public class Sample {
 private static Map<String, Map<String, String>> peaks = new HashMap<String, Map<String, String>>();
 private final String name;
 public Sample(String name) {
 this.name = name;
 this.peaks = new HashMap<String, Map<String, String>>();
 }
 public static Map<String, Map<String, String>> getPeaks() {
 return peaks;}
 public static void addPeak(String peakName, String value) {
 Map<String, String> peak = new HashMap<String, String>();
 peak.put("value", value);
 peaks.put(peakName, peak);
 }
}
public class Main {
 static Map<Integer, Sample> sample = new HashMap<Integer, Sample>();
 public static void main(String[] args) {
 Sample sam = new Sample("Test1");
 sample.put(1, sam);
 sample.get(1).addPeak("A", "1");
 sam = new Sample("Test2");
 sample.put(2, sam);
 sample.get(2).addPeak("B", "123");
 System.out.println(sample.get(1).getPeaks().toString()); 
 System.out.println(sample.get(2).getPeaks().toString()); 
 System.out.println(sample.get(4).getPeaks().toString()); 
 }
}

EVERYTIME THE OUTPUT IS: {B={value=123}}

I don't know which part is wrong. Did I miss something?

Michael Laffargue
10.3k6 gold badges46 silver badges76 bronze badges
asked Sep 25, 2013 at 12:11
5
  • what are you getting? please paste your output. Commented Sep 25, 2013 at 12:14
  • @freak It's there, buried. Joko, please post the output as a separate block instead of hiding it inside a comment. Commented Sep 25, 2013 at 12:14
  • o just see :D @chrylis Commented Sep 25, 2013 at 12:18
  • If I only remove static I get much more errors (but this is another topic then). I will read about static... Commented Sep 25, 2013 at 12:20
  • "sample.get(4).getPeaks()" would throw an NPE Commented Sep 25, 2013 at 12:21

3 Answers 3

1

This map is static :

private static Map<String, Map<String, String>> peaks = new HashMap<String, Map<String, String>>();

Hence, every call to Sample.getPeaks() points to the same Map.

//getPeaks() returns the same map in both cases.
// In other words : sample.get(1).getPeaks() == sample.get(2).getPeaks()
System.out.println(sample.get(1).getPeaks().toString()); 
System.out.println(sample.get(2).getPeaks().toString());
// This is equivalent to
Map<String, Map<String, String>> myStaticMap = Sample.getPeaks();
System.out.println(myStaticMap.toString()); 
System.out.println(myStaticMap.toString()); 
answered Sep 25, 2013 at 12:21
Sign up to request clarification or add additional context in comments.

Comments

1

The methods getPeaks and addPeaks as well as the map peaks are static! That means they only exist once (per class), not once per instance of Sample.

So everytime you create a new Sample instance, you're overwriting the static field peaks in the constructor! Remove the static keyword!

answered Sep 25, 2013 at 12:21

Comments

0

Perhaps you were trying to learn how to use nested Maps, but for this implementation, couldn't you simply create a Map<String, String>? The reason I'm asking is because in addPeak you've made a call to peak.put("value", value), with the hardcoded string "value", which may be unnecessary. Here's some code which may satisfy what you're interested in:

public class WhyMapOfMap {
 // Simpler map
 private Map<String, String> peaks;
 private String name;
 public WhyMapOfMap(String name) {
 this.name = name;
 peaks = new HashMap<>();
 }
 public Map<String, String> getPeaks() {
 return peaks;
 }
 public void addPeak(String peakName, String value) {
 peaks.put(peakName, value);
 }
}

Then you may access the map normally sample.get(key).getPeaks().

answered Sep 25, 2013 at 17:35

2 Comments

Thank you for your answer! Maybe there is an easier way to do so. But in my complete sourcecode I save more the than only 'value'.
Ah I understand. I sometimes find it difficult to model the data I need into standard collections. In those cases I'll favor an approach of object composition. A guiding principles I look to is "smart data and dumb algorithms". Good luck!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.