Load data from csv into a HashSet
in
Programming Questions
•
1 year ago
hi
i have a dataset with 3 cols and ~20.000 lines that contains long/lat coonrdinates like this:
Country,longitude,latitude
Italy,523452,64236
Italy,5435,645654
Sweden,4364,673657
Sweden,325,265
etc
i.e there are 20 countries so each ,for example, Italy is repeated 1000 times which means that i have 1000 point coordinates and so on.
so i `ve written this
i have a dataset with 3 cols and ~20.000 lines that contains long/lat coonrdinates like this:
Country,longitude,latitude
Italy,523452,64236
Italy,5435,645654
Sweden,4364,673657
Sweden,325,265
etc
i.e there are 20 countries so each ,for example, Italy is repeated 1000 times which means that i have 1000 point coordinates and so on.
so i `ve written this
- private String []data;
private String [][] csv;
private String filename="/home/user/coordinates.csv";
private final int cols=3;
private String delimiter=","; - PrintWriter output;
Map < String,ArrayList<PVector>> coords;
//List<Map<String, PVector>> coords = new ArrayList<Map<String, PVector>>();
List<Integer> t=new ArrayList<Integer>(); - public void loadData(){
- output=pa.createWriter("/home/user/points.txt");
- data=pa.loadStrings(filename);
- ArrayList<PVector> tempPoints=new ArrayList<PVector>(data.length);
- // Set<String> tempNames=new HashSet <String> ();
- coords=new HashMap<String,ArrayList<PVector>>(data.length);
- csv=new String[data.length][cols];
- for (int i=0; i<data.length; i++){
- for (int j=0; j<cols; j++){
- String [] tempGeo=pa.split(data[i], delimiter);
- csv[i][j]=tempGeo[j];
- }//FOR J
- tempPoints.add(new PVector(Float.parseFloat(csv[i][1]),Float.parseFloat(csv[i][2])));
- // tempNames.add( csv[i][0]);
- coords.put(new String (csv[i][0]),tempPoints);
- }//FOR I
- for (String s: coords.keySet()){
- output.println(s);
- for (PVector pv:coords.get(s))
- output.println(pv);
- }
i get the names correctly by removing duplicates due to Set`s functionality, but (obviously) it assigns *all* the points to each country and i end up with a file of 20.000(lines)x20(countries)..
how can i assign the coordinates correctly to each country? i.e to take an ArrayList of PVectors for each country that it`s size is equal to the number of times this country appears in the first column?
how can i assign the coordinates correctly to each country? i.e to take an ArrayList of PVectors for each country that it`s size is equal to the number of times this country appears in the first column?
i tried a couple of ifs by matching Strings but had no luck and i cannot figure out something else to try
thanks
thanks
1