简体   繁体   中英

How to sort an ArrayList of Doubles contained within a MultiValuedMap

I have a MultiValuedMap (Apache commons), with keys of String and values stored in an ArrayList of Doubles, read from a file. I don't need to sort the keys, but I do need to sort the ArrayList associated with each key. Is there a way to do this?

I've tried Collections.sort but that doesn't seem to work with the MultiValuedMap. I haven't tried creating a Comparator yet but I wasn't sure how to begin one for something inside a Map.

//Read file of data to be placed in MultiValuedMap 
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line = "";
String csvSplitBy = ",";


//Create the MultiValuedMap    
MultiValuedMap<String, Double> mvMap = new ArrayListValuedHashMap<>();

//Read data into MultiValuedMap
while ((line = br.readLine()) != null) {
       String[] cols = line.split(csvSplitBy);
       mvMap.put(cols[4], Double.parseDouble(cols[3]));  

//This gives an error
Collections.sort(mvMap);

The error I get from the Collections.sort is "no suitable method for sort(MultiValuedMap)"

I'm not looking to sort the keys, just the values. So an expected output should be something like:

44 => [284.5, 289.35, 300.53, 534.0] and 41 => [311.82, 368.45, 400.2]

Collections.sort expects a Collection that can be sorted. You're passing it a MultiValuedMap which can't be sorted. Instead what you need to do is call Collections.sort for each of the individual ArrayLists.

Something like this should work (I haven't seen the actual lib, but this is following map conventions)

for(String s: mvMap.keySet()){
    Collections.sort(mvMap.get(s));
}

That iterates through each strings and sorts it with the list given

According to java docs MultiValuedMap implements Map 's get. So the compiler is assuming the type of value we get from MultiValuedMap.get() is a Double

I would do a workaround for Collections.sort(mvMap); with something like

for (String key : mvMap.keySet()) 
    mvMap.put(key, Collections.sort((List<Double>)mvMap.get(key));
}

So far I've tried all the suggestions (thank you!) but all have received some sort of error or another for incompatible types.

The only thing I could do is create a workaround and create a new regular HashMap and pull the ArrayList from mvMap into an ArrayList, sort it, and then put it into a new HashMap. This isn't super efficient but it's what I was able to come up with, and it works. If you have any other suggestions, though, please let me know and I will continue to try them. I appreciate the help!

Here's what I added:

//Declared with the other variables
HashMap<String, List<Double>> sortedMap = new HashMap<String, List<Double>>();

//Added after the while loop
for (String key : mvMap.keySet()) {
    List<Double> list = new ArrayList<Double>(mvMap.get(key));
    Collections.sort(list);
    sortedMap.put(key, list);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM