Reputation: 1579
I have two list object, I want combine them, this is my class
public class DailyIncome {
private String date;
private String total;
}
and I have two list, this is list A
and this is list B
and my expectation result like this
is possible to realize my expectation?
Upvotes: 2
Views: 93
Reputation: 12
I liked above two solutions! I think it can be done this way too if you are using java (by looking at provided example of DailyIncome):
Please try this: You will Just need to create list of DailyIncome instead of Integer. Collection id = Arrays.asList(2316, 2317, 2318); Collection existingId = Arrays.asList(1004, 1762, 1892, 1342, 1942, 2316);
Collection<Integer> similar = new HashSet<Integer>( id );
similar.addAll( existingId );
System.out.println("Similar:"+similar);
Upvotes: -1
Reputation: 11075
"is possible to realize my expectation?"
Yes, it is. But you'd better write down what you have tried so far before you ask a question in SO.
1, Your DailyIncome
doesn't have Getter and Setter and the field is decorated with private
. How would you expect it to be visited from other class?
2, For your scenario, I think Map
should be used.
Please try the code below for your reference.
public class DailyIncome {
public String date;
public String total;
public DailyIncome(String date, String total){
this.date = date;
this.total = total;
}
}
And also the Test
class.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Test {
private List<DailyIncome> shopA = new ArrayList<DailyIncome>();
private List<DailyIncome> shopB = new ArrayList<DailyIncome>();
private List<List<String>> shop = new ArrayList<List<String>>();
public void initialize(){
shopA.add(new DailyIncome("20-08-16", "4"));
shopA.add(new DailyIncome("21-08-16", "8"));
shopA.add(new DailyIncome("23-08-16", "3"));
shopB.add(new DailyIncome("20-08-16", "6"));
shopB.add(new DailyIncome("21-08-16", "7"));
shopB.add(new DailyIncome("22-08-16", "8"));
}
public void calculate(){
Map<String, String> mapA = new HashMap<String, String>();
Map<String, String> mapB = new HashMap<String, String>();
for(DailyIncome income: shopA){
mapA.put(income.date, income.total);
}
for(DailyIncome income: shopB){
mapB.put(income.date, income.total);
}
Set<String> keySet = new HashSet<String>();
keySet.addAll(mapA.keySet());
keySet.addAll(mapB.keySet());
Object keyArr[] = keySet.toArray();
for(int i = 0; i < keyArr.length; i++){
String key = (String)keyArr[i];
String totalA = mapA.containsKey(key) ? mapA.get(key):"0";
String totalB = mapB.containsKey(key) ? mapB.get(key):"0";
List<String> itemList = new ArrayList<String>();
itemList.add(key);
itemList.add(totalA);
itemList.add(totalB);
shop.add(itemList);
}
/*
* print
*/
for(List<String> itemList: shop){
System.out.print(itemList.get(0) + " ");
System.out.print(itemList.get(1) + " ");
System.out.print(itemList.get(2));
System.out.println();
}
}
public static void main(String args[]){
Test obj = new Test();
obj.initialize();
obj.calculate();
}
}
Upvotes: 2
Reputation: 462
As @Tibrogargan said, it would be easier to combine them together if you didn't stock the data as a class, but as a Map. This is because Java would automatically read the data as a List of Dates, which then is associated with multiple Integers (income). I have done this many times, and have found that for your "issue", it would be easier to do what you want this way instead of by the way you first presented.
However, you should have your Map like this:
Map<Date, HashMap<ListId, Long>>
As you would be able to store the income for each list, and then if the results of a "get(ListId)" returns null, you know that no income will have came in for that list.
Hope this helps. Sneling.
Upvotes: 1