I have a hashmap which contains student id as key and some string as value.
Map<Integer, String> data = new HashMap<Integer, String>();
it contains data like
1 a
2 b
3 a
4 c
5 b
6 a
i want to find the duplicate values in map and replace them with integer values. i.e. i want a map like
1 1
2 2
3 1
4 3
5 2
6 1
i.e. map shud pick first value(a), find all keys for that value and then replace value of those keys with 1. Then pick second value(b) find all keys and replace them with 2 and so on. The file i am reading is too large so i cannot replace all keys manually by specifying each key. So, what i have tried so far is
Map<Integer,Integer> finalmap = new HashMap<Integer,Integer>();
int a=0;
List mapkey = new ArrayList(data.keySet());
List mapval = new ArrayList(data.values());
Iterator valit = mapval.iterator();
while(valit.hasNext()){
a=a+1;
Object valuet = valit.next();
Iterator keyit = mapkey.iterator();
while(keyit.hasNext()){
Object keyt = keyit.next();
String comp1 = data.get(keyt).toString();
String comp2 = valuet.toString();
if(comp1.equals(comp2)){
finalmap.put((String)keyt,a);
}
}
}
but this is not giving me correct output. it doesnt start with a=1. I think probably it first calculates all the incremented values of a. I have a text file with 1000 records. And the output i get is
1 1000
2 987
3 1000
4 298
5 987
6 1000
I dont know where i m wrong. Please help me regarding this. Thank You
HashMap