Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

My input file will be look like this

போதும்  1
போதும்  2
போதும்  3
போதும்  4
போதும்  5
போதும்  6
போதும்  7
போதும்  8
போதும்  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.

like image 869
Karthi Avatar asked Apr 09 '26 17:04

Karthi


1 Answers

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.
like image 166
Jon Skeet Avatar answered Apr 12 '26 06:04

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!