java - get second array list without duplicates and add units from duplicates to single one -
package com.mkyong.util; import java.io.bufferedreader; import java.io.filenotfoundexception; import java.io.filereader; import java.io.ioexception; import java.util.arraylist; import java.util.collections; import java.util.hashset; import java.util.list; public class readfromcvs { static arraylist<datas> datalist = new arraylist<datas>(); public static void main(string[] args) { readfromcvs obj = new readfromcvs(); obj.run(); obj.printdatalist(readfromcvs.datalist); } public void run() { string csvfile = "c:\\users\\user\\downloads\\salesdata.csv"; bufferedreader br = null; string csvsplitby = ","; string line = ""; try { br = new bufferedreader(new filereader(csvfile)); br.readline(); while ((line = br.readline()) != null) { // split on comma string[] datas = line.split(csvsplitby); // crate data object store values datas dataobject = new datas(); // add values csv file data object dataobject.setproductid(integer.parseint(datas[0])); dataobject.setunits(integer.parseint(datas[1])); // adding data object list datalist.add(dataobject); } // print values stored in datalist collections.sort(datalist); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { if (br != null) { try { br.close(); } catch (ioexception e) { e.printstacktrace(); } } } } public void printdatalist(list<datas> datalisttoprint) { (int = 0; < datalisttoprint.size(); i++) { system.out.println("product id: " + datalisttoprint.get(i).getproductid() + " units: " + datalisttoprint.get(i).getunits()); } } }
class datas implements comparable<datas> { private int productid; private int units; public int getproductid() { return productid; } public void setproductid(int productid) { this.productid = productid; } public int getunits() { return units; } public void setunits(int units) { this.units = units; } @override public int compareto(datas d) { if (this.getproductid() > d.getproductid()) { return 1; } else if (this.getproductid() < d.getproductid()) { return -1; } else { return 0; } } }
i want move objects 1 array list 1 without duplicates when need add units duplicate product id single product id
here of out after sorting first arraylist:
product id: 10001 units: 5 product id: 10001 units: 8 product id: 10001 units: 7 product id: 10002 units: 4 product id: 10002 units: 10 product id: 10002 units: 6 product id: 10003 units: 8 product id: 10003 units: 6 product id: 10003 units: 3 product id: 10003 units: 7 product id: 10003 units: 4 product id: 10004 units: 6 product id: 10004 units: 9 product id: 10004 units: 3 product id: 10004 units: 7
instead of list
, might easier map
. parse input file, store products in hashmap<integer,datas>
productid
. process new instances of datas
input, check map see if have productid
, , if so, update units
of instance.
map<integer,datas> products = new hashmap<>(); ... // parse input file. each record: int productid = integer.parseint(record[0]); int units = integer.parseint(record[1]); if (products.containskey(productid)) { datas existingrecord = products.get(productid); existingrecord.setunits(existingrecord.getunits() + units); } else { datas d = new datas(); d.setproductid(productid); d.setunits(units); products.put(productid, d); }
by way, datas
class name not follow java conventions. capitalize first letter, , try name more meaningful. maybe product
?
Comments
Post a Comment