开发者

how to get the each word which has the maximum value using java program

开发者 https://www.devze.com 2023-01-06 22:13 出处:网络
My input file will be look like this போதும்1 போதும்2 போதும்3 போதும்4

My input file will be look like this

போதும்  1
போதும்  2
போதும்  3
போதும்  4
போதும்  5
போதும்  6
போதும்  7
போதும்  8
போதும开发者_JS百科்  9
போதும்  10
போதும்  11
போட்டால்    1
போட்டால்    2
பொன்    1
பொன்    2
பொன்    3
பொன்    4
பொன்    5

and my output want to be as

போதும்  11
போட்டால்    2
பொன்    5

How to select the each word with its maximum value using java program. Pls suggest me any ideas. Thanks in advance.


Something along these lines:

  • Create a HashMap<String, Integer>
  • For each line in your input:
    • Split it into the word and number, and parse the number part (as Matthew comments below, using Scanner can help with this part)
    • See if there's anything in your map for that word
      • If there isn't, use map.put(word, value)
      • If there is, compare the current value with the new one, and replace the current value if the new one is higher
  • When you've read everything in the file, your map will contain (word / maximum value) pairs. You can iterate over the map's entries to get at everything you need.


Check this program: http://leepoint.net/notes-java/data/collections/maps/ex-wordfreq.html It could be helpful to you.

0

精彩评论

暂无评论...
验证码 换一张
取 消