android - In java How to merge two different size arraylist and make a new Arraylist? -
i have 2 arraylist name prebusinesslist, businesslist. in business list have data server, , in prebusinesslist local one. in lists have id, count value betterly demonstrate below
now wanted make newbusinesslist this
how can in java, please me solve
assumming understood problem correctly (big if...):
also, assume each element in lists pair - looks data (just dumb wrapper class holds 2 integers). if other class you'll need adjust code.
private map<integer,integer> finalvalues = new hashmap<integer,integer>(); (pair<integer,integer> entry : prebusinesslist) { finalvalues.put(entry.getfirst(), entry.getsecond()); } //2nd list overwrites values 1st (anything not overwritten remains) (pair<integer,integer> entry : businesslist) { finalvalues.put(entry.getfirst(), entry.getsecond()); } arraylist<pair<integer,integer>> finallist = new arraylist<>(); (map.entry<integer,integer> entry : finalvalues) { finallist.add(new pair(entry.getkey(), entry.getvalue()); } //and sort list collections.sort(finallist, new comparator<pair<integer,integer>> { int compare(pair<integer,integer> a, pair<integer,integer>b) { return a.getfirst.compareto(b.getfirst()); //compare 1st number in pair } });
Comments
Post a Comment