Reputation: 197
I have a requirement in which I am iterating a list using a for loop and getting the values of a device like model number(3-digit number),directory number etc. The model number which I got should be compared/search with a CSV file(it has 2 columns-compared with one column-3-digit model number) and need to find its corresponding value in the other column.Once it finds the value it should display that value in the list instead of the 3-digit number.I think I create a hashmap after reading the csv file and pull the output from the hashmap.
Can you let me know if my approach is correct and if so can I get a sample code as how I need to do this? thank you so much in advance. Language-Java
Upvotes: 0
Views: 4202
Reputation: 418
Yes if you have one CSV file has a mapping of the model number to something you can load that into a HashMap with the key being the model number and the value whatever it is you want to show instead.
To initialize a HashMap;
HashMap<String, String> hashMap = new HashMap<String, String>();
To add to it when you read your first CSV file;
hashMap.put(key, value);
And to retrieve the value based on the key when handling the other CSV file.
String value = hashMap.get(key);
For more info on HashMap; http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
Upvotes: 2