1

I have a code snippet that is not sorting correctly. I need to sort a HashMap by keys using TreeMap, then write out to a text file. I have looked online on sorting and found that TreeMap can sort a HashMap by keys but I am not sure if I am utilizing it incorrectly. Can someone please take a look at the code snippet and advise if this is incorrect?

public void compareDICOMSets() throws IOException
    {
        FileWriter fs;
        BufferedWriter bw;
        fs = new FileWriter("dicomTagResults.txt");
        bw = new BufferedWriter(fs);
        Map<String, String> sortedMap = new TreeMap<String, String>();
        for (Entry<String, String> entry : dicomFile.entrySet())
        {       
            String s = entry.toString().substring(0, Math.min(entry.toString().length(), 11));
            if(dicomTagList.containsKey(entry.getKey()))
            {
                sortedMap.put(s, entry.getValue());
                Result.put(s, entry.getValue());
                bw.write(s + entry.getValue() + "\r\n");                
            }
        }
        bw.close();
        menu.showMenu();
    }
}

UPDATE:

This is what I get for results when I do a println:

(0008,0080)
(0008,0092)
(0008,1010)
(0010,4000)
(0010,0010)
(0010,1030)
(0008,103E)
(0008,2111)
(0008,1050)
(0012,0040)
(0008,0094)
(0010,1001)

I am looking to sort this numerically. I have added String s to trim the Key down just to the tags as it was displaying a whole string of stuff that was unnecessary.

14
  • 1
    Already answered here stackoverflow.com/questions/922528/… Commented Mar 18, 2015 at 15:57
  • @abarisone, i took a look at this and I actually did refer to this before I asked the question. But i am almost convinced I am doing something wrong with my code. Care to take a look at it? Commented Mar 18, 2015 at 16:16
  • 1
    what is the problem in sorting? Commented Mar 18, 2015 at 16:20
  • @rns, when I write it out to a text document, it is not sorted by the keys as it should be. Commented Mar 18, 2015 at 16:20
  • This code is very confusing to me. For every entry in the dicomFile map you create a new sortedMap containing all the Result entries, then add all Result entries again, and then write the key to bw. Then, you're not using the sortedMap for anything. Maybe you just want to add your entry to a single sortedMap, that will be sorted in the end, and print/write that one out? Commented Mar 18, 2015 at 17:02

1 Answer 1

2

You should first order your results, and then print them.

For Java 8:

Map<String, String> Result = ...;

// This orders your Result map by key, using String natural order
Map<String, String> ordered = new TreeMap<>(Result);

// Now write the results
BufferedWriter bw = new BufferedWriter(new FileWriter("dicomTagResults.txt"));
ordered.forEach((k, v) -> bw.write(k + v + "\r\n");

bw.close();

For pre Java 8:

Map<String, String> Result = ...;

// This orders your Result map by key, using String natural order
Map<String, String> ordered = new TreeMap<>(Result);

// Now write the results
BufferedWriter bw = new BufferedWriter(new FileWriter("dicomTagResults.txt"));
for (Map.Entry<String, String> entry : ordered.entrySet()) {
    String k = entry.getKey();
    String v = entry.getValue();
    bw.write(k + v + "\r\n");
}

bw.close();
Sign up to request clarification or add additional context in comments.

2 Comments

the -> wont work, I am writing this in a slightly older version of Java (Java 6)
@ryekayo OK, I'll add a pre java8 example.

Your Answer

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.