how to read the arraylist for each key in hashmap in java?
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
//import java.util.Map.Entry;
public class hashmapexample {
// creating a HashMap
HashMap<String, ArrayList<String>> items = new HashMap<String,
ArrayList<String>>();
public synchronized void addToList(String mapKey, String myItem) {
List<String> itemsList = items.get(mapKey);
// if list does not exist create it
if(itemsList == null) {
itemsList = new ArrayList<String>();
itemsList.add(myItem);
items.put(mapKey, (ArrayList<String>) itemsList);
} else {
// add if item is not already in list
if(!itemsList.contains(myItem)) itemsList.add(myItem);
}
//Iterator<Entry<String, ArrayList<String>>>
iterator=items.entrySet().iterator();
//while(iterator.hasNext()){
// Entry<String, ArrayList<String>> e = iterator.next ( ) ;
// System.out.println("key: "+e.getKey());
// Iterator<String> iterator1=itemsList.listIterator();
// while(iterator1.hasNext()){
// System.out.println(" value: "+iterator1.next());
// }
// }
for (int i = 0; i < items.size();i++) {
System.out.println(items.get(mapKey));
//System.out.println("the length of the array
:"+(items.get(mapKey)).size());
//System.out.println("the elements are
:"+items.get(mapKey).toArray());
}
//return null;
}
public static void main(String[] args){
//ArrayList<String> items = new ArrayList<String>();
//System.out.println("the values :"+addToList(null, null));
hashmapexample example = new hashmapexample();
try {
// getting the field Keyword from the csv
String csvfile="/Users/dray/Downloads/ReseedingDBRandomKeywords.csv";
BufferedReader br =new BufferedReader(new FileReader(csvfile));
StringTokenizer st = null;
String line="";
int linenumber=0;
int columnnumber;
// initializing the parameter for each column
int free = 0;
int free1 = 0;
// create the ArrayList
ArrayList<String> Keyword = new ArrayList<String>();
ArrayList<String> Alternate = new ArrayList<String>();
// reading through the csv file
while((line=br.readLine())!=null){
System.out.println("read line: "+line);
linenumber++;
columnnumber = 0;
st = new StringTokenizer(line,",");
while(st.hasMoreTokens()){
columnnumber++;
String token = st.nextToken();
if("Keyword".equals(token)){
free=columnnumber;
System.out.println("The value of free :"+free);
}else if ("Alternate".equals(token)){
free1=columnnumber;
System.out.println("The value of free1 :"+free1);
}
if(linenumber>1){
if (columnnumber==free)
{
Keyword.add(token);
}else if (columnnumber==free1){
Alternate.add(token);
}
}
}
}
// converting the keyword ArrayList to an array
String[] keyword = Keyword.toArray(new String[Keyword.size()]);
for(int i=0;i<keyword.length;i++){
System.out.println(" The value of the keyword is :"+keyword[i]);
}
// converting the alternate ArrayList to an array
String[] alternate = Alternate.toArray(new String[Alternate.size()]);
for(int i=0;i<alternate.length;i++){
System.out.println("The value of the alternate is :"+alternate[i]);
}
for(int i=0;i<keyword.length;i++){
example.addToList(keyword[i], alternate[i]);
}
}catch (Exception e){
System.out.println("There is an error :"+e);
}
}
}
How to read the arraylist corresponding to each key, in sense I want to
get the arraylist as a whole corresponding to each key and convert each
arraylist to an array.
the csv file is :
Keyword,Alternate
ego kit,hello
ego kit,elite
ego kit,sample
ego kit,blue
ego kit,codeblue
ego kit,same
samsung,welcome
samsung,martin
samsung,galaxy tab
samsung,joined
And the output should be like :
array1={hello,elite,sample,blue,codeblue,same}
array2={welcome,martin,galaxy tab,joined}
And in sense for any number of elements. Please help me.
No comments:
Post a Comment