Check for duplicate key in hashmap
715138Jun 15 2012 — edited Jun 18 2012Hi All,
I have a String array which has underscore separated values.
e.g :String str[]=new String[]{"batchId+bemId_only_bentype_ben_id","batcytrd_only_bentype_ben_id"};
now we have to traverse this array and and store the value before underscore as a key for hashmap and rest as value for hashmap.
Here "batchId+bemId" will be as a key and "only_bentype_ben_id" will be value .similarly for second array element the key will be "batcytrd" and value will be "only_bentype_ben_id".
How to achive this .I am not able to split the element as key and value .
My sample code is like this :This is not complete code :
import java.util.StringTokenizer;
public class DuplicateCheck {
public static void main(String[] args) {
String str[]=new String[]{"batchId+bemId_only_bentype_ben_id","batchId+bemId_only_bentype_ben_id"};
String str1;
StringTokenizer st2;
for (int i=0;i<str.length;i++){
str1=str;
st2 = new StringTokenizer(str1, "_");
while(st2.hasMoreTokens()) {
String key = st2.nextToken();
System.out.println(key );
}
}
}
}
Thanks
Sumit