Below is the program I have for reading the .csv file and printing the output, but I am not able to achive the below functionality as mentioned in my input and output. I basically want to have the user ID listed separately if it’s avaialble in 2 different groups.
My input would be something like:
User ID Group
ABC Group1
DEF Group2
ABC Group3
GHI Group4
Since ABC is available in 2 different groups, I need the output as:
ABC Group1
ABC Group3
Can you please help?
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;
public class dumpRead {
public static void main(String[] args) {
try {
//csv file containing data
String strFile = "C:/Tracker/read/data.csv";
//create BufferedReader to read csv file
BufferedReader br = new BufferedReader( new FileReader(strFile));
String strLine = "";
StringTokenizer st = null;
int lineNumber = 0, tokenNumber = 0;
//read comma separated file line by line
while( (strLine = br.readLine()) != null){
lineNumber++;
//break comma separated line using ","
st = new StringTokenizer(strLine, ",");
while(st.hasMoreTokens()){
//display csv values
tokenNumber++;
System.out.println("Token # " + tokenNumber
+ ", Token : "+ st.nextToken());
}
//reset token number
tokenNumber = 0;
}
}
catch(Exception e) {
System.out.println("Exception while reading csv file: " + e);
}
}
}
Parse all the user id’s and put it in a HashMap>
For each userid, check if it is already there in the hashmap, if it is already there, then get the corresponding Set and add it to the end of the list.
if hashmap does not contain the user, then create a new HashSet and add the group
Note: The idea behind HashSet, is it will ignore duplicates
The psuedo code will be something like this,