2016-02-22 39 views
0

我很新TreeMapTreeSet等,并想知道如何按值排序数据结构?我意识到使用TreeSet可以自动将它按字母顺序排序,但我希望它通过值进行排序?任何想法如何做到这一点?如何按值对TreeSet进行排序?

目前,它打印像...

  • AAA:29
  • aaahealthart:30
  • AB:23
  • 修道院:14
  • abdomin:3
  • 仔:29
  • aberdeenuni:20

当我想它打印像...

  • aaahealthart:30
  • AAA:29
  • 仔:29
  • AB:23
  • aberdeenuni:20
  • 修道院:14
  • abdomin:3

这里是我的方法这里...

ArrayList<String> fullBagOfWords = new ArrayList<String>(); 
public Map<String, Integer> frequencyOne; 

public void termFrequency() throws FileNotFoundException{ 
    Collections.sort(fullBagOfWords); 
    Set<String> unique = new TreeSet<String>(fullBagOfWords); 
    PrintWriter pw = new PrintWriter(new FileOutputStream(frequencyFile)); 
    pw.println("Words in Tweets : Frequency of Words"); 
    for (String key : unique) { 
     int frequency = Collections.frequency(fullBagOfWords, key); 

     System.out.println(key + ": " + frequency); 
     pw.println(key + ": " + frequency); 
     } 
    pw.close(); 
    } 

感谢所有帮助球员。

+0

你不能。使用HashMap存储单词及其频率,然后将映射转换为列表,并按值排序该列表。 –

回答

1

TreeMap按键排序,我不认为你可以使用相同的实现按值排序。但你可以用稍微不同的方法来完成任务:

public Map<String, Integer> countWords(List<String> words) { 
    Map<String, Integer> result = new Map<>(); 
    for (String word : words) { 
     if (result.containsKey(word)) { 
      // the word is already in the map, increment the count 
      int count = result.get(word) + 1; 
      result.put(word, count); 
     } else { 
      result.put(word, 1); 
     } 
    } 

    return result; 
} 

然后你只需要对结果图的元素进行排序。您可以通过以下方式做到这一点:

public List<Map.Entry<String, Integer> sortMap(Map<String, Integer> map) { 
    List<Map.Entry<String, Integer> elements = new LinkedList<>(map.entrySet()); 
    Collections.sort(elements, new Comparator<Map.Entry<String, Integer>>() { 

     public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { 
      return o1.getValue().compareTo(o2.getValue()); 
     } 

    }); 
} 

所以你使用第一种方法计算的词频和第二通过它来进行排序。

+0

@以及如何将两者结合?我在'sortMap()'方法内调用'result'吗? –

+0

@JohnLewis是的,您需要从'countWords()'方法中获取结果并将其作为参数传递给'sortMap()'方法。 –

+0

@Dialial Alexiev - 我认为计数单词不能正常工作,因为我注意到单词数量高于单词出现的数量? –

1

尝试这样:

创建Comparator

class EntryComparator implements Comparator<Map.Entry<String,Integer>>{ 
    public int compare(Map.Entry<String,Integer> first, Map.Entry<String,Integer> second) { 
     return first.getValue().compareTo(second.getValue) 
    } 

    public boolean equals(Map.Entry<String,Integer> that) { 
     return this.equals(that); 
    } 
} 

用它来排序条目:

Set<Map.Entry<String,Integer>> sorted = 
    new TreeSet<Map.Entry<String,Integer>>(frequencyOne.entrySet(), new EntryComparator()); 

这应该给你想要的东西。

1

您可以创建一个ArrayList和存储这样在它的每个条目:

Collections.sort(list , new Comparator<Map.Entry<String, Integer>>() { 

     public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) { 
      return o1.getValue().compareTo(o2.getValue()); 
     } 

    }); 

和:

ArrayList<Map.Entry<String, Integer> list = new new ArrayList(map.entrySet()); 

那么你可以使用自己的价值,该条目进行比较的比较排序的ArrayList那么你可以打印来自arrayList的条目

相关问题