happs
happs

Reputation: 417

Mapping of String values to Integer for large files

I want to map my user ids in text file to unique integer values using java code. I have a large text file of around 50MB, doing it the traditional way will take me a lot of time e.g.

     3245 4567
      3245 8726
      4567 8363
      4567 7363
      4567 5267 etc
  After mapping I want my output file to look like
     1 2
     1 3
     2 4
     2 5
     2 5

Upvotes: 0

Views: 78

Answers (1)

Zong
Zong

Reputation: 6230

Try using a HashMap<String, Integer> (see HashMap) where the integer is a counter incremented every time you encounter a new ID. You will literally have a mapping of ID strings to integers, so it should be very easy to process the text from there. Thus your expected running time is O(n).

Alternatively, you can also try using the hash code given by String.getHashCode(). If you have around 10000 IDs there is only a 1% chance of a collision. If you have less the probability decreases dramatically.

Since you've additionally stated that you want to preserve ordering, then the LinkedHashMap is perfect. When you iterate over it, the order is the same as the order of insertion.

Upvotes: 2

Related Questions