-1

I have a HashMap which needs to get sorted by value and not key, in descending order. To get the hashmap, I determine if the given input line is an sql statement, find its complexity and then create a hashmap with key as the sql statement and value as the complexity. Though I have written the right code, I suppose, to sort the HashMap, the output does not show a sorted hashmap. Here's the code:

public <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort( list, new Comparator<Map.Entry<K, V>>() {
    @Override
    public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
        return (o1.getValue()).compareTo(o2.getValue());
    }
});

Map<K, V> result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list) {
    result.put(entry.getKey(), entry.getValue());
}
return result;

}

public void method2() throws Exception{


try (FileInputStream inputStream = new FileInputStream(fileName);
        Scanner sc = new Scanner(inputStream, "UTF-8")){

        while (sc.hasNextLine()) {
            String line = sc.nextLine();
           // System.out.println("THIS IS METHOD 2 EXECUTING");
            SQLChecker checker = new RegexSQLChecker();

            if(checker.isSQLStatement(line)){
                SQLFreq sqlf=new PrintFreqMap();
              //Trim the line to start with a SQL keyword
            String line1=   sqlf.trimString(line);

                 //*******FIND_QUERY_COMPLEXITY*******

                ComplexityScore cs=new ComplexityScorer();

                   int complexity= cs.findComplexity(line1);
                  map1 = new HashMap<String, Integer>();
                  map1.put(line1,complexity);
                  Map<String, Integer> sorted_map4 = sqlf.sortByValue(map1);


                  if(outputFormat.equals("txt"))
                  o.writeMapToTextFile(map1, p2);
                  if(outputFormat.equals("csv"))
                  o.writeHashMapToCsv(map1,p5);
                  sqlf.printMap(sorted_map4);
                  if(outputFormat.equals("html"))
                        o.writeMapToHTML(sorted_map4,p7); 

            }


        }
        //SQLFreq sqlf=new PrintFreqMap();

}

}


public void printMap(Map<String, Integer> map)
{
    for (Map.Entry<String, Integer> entry : map.entrySet()) {
        System.out.println("Key :" + entry.getKey() + " Value : "
            + entry.getValue());
    }
}

enter code here

Apoorva
  • 1
  • 3
  • 2
    Possible duplicate of [Sort a Map by values](https://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values) – Ben Feb 27 '18 at 06:31

1 Answers1

0

You are creating a new map each time with one entry inside your loop, sorting it and printing it. Move the sorting and printing of the contents of the map outside the loop.

while (sc.hasNextLine()) {
   //construct the map
}
Map<String, Integer> sorted_map = sqlf.sortByValue(map1);
sqlf.printMap(sorted_map);
Thiyagu
  • 17,362
  • 5
  • 42
  • 79