Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将这三个订单列表合并到一个同名列表项合并到一行?;_Java_Arraylist_Hashmap - Fatal编程技术网

Java 如何将这三个订单列表合并到一个同名列表项合并到一行?;

Java 如何将这三个订单列表合并到一个同名列表项合并到一行?;,java,arraylist,hashmap,Java,Arraylist,Hashmap,我有三份订单,都是顾客从商店买的东西。 以下是订单上的行的模型 public class ShopOrderItem { private String name; private String itemcode; private int count; private double price; private String taxRate; public ShopOrderItem() { super(); } public ShopOrderItem(String name,

我有三份订单,都是顾客从商店买的东西。 以下是订单上的行的模型

public class ShopOrderItem {

private String name;
private String itemcode;
private int count;
private double price;
private String taxRate;



public ShopOrderItem() {
    super();
}
public ShopOrderItem(String name, String itemcode, int count, double price,
        String taxRate) {
    super();
    this.name = name;
    this.itemcode = itemcode;
    this.count = count;
    this.price = price;
    this.taxRate = taxRate;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getItemcode() {
    return itemcode;
}
public void setItemcode(String itemcode) {
    this.itemcode = itemcode;
}
public int getCount() {
    return count;
}
public void setCount(int count) {
    this.count = count;
}
public double getPrice() {
    return price;
}
public void setPrice(double price) {
    this.price = price;
}
public String getTaxRate() {
    return taxRate;
}
public void setTaxRate(String taxRate) {
    this.taxRate = taxRate;
}
}

下面是如何获得这3份订单

public class testList {

ShopOrderItem a = new ShopOrderItem("apple","001",2,(double)4.00,"0.17");
ShopOrderItem b = new ShopOrderItem("banana","002",1,(double)2.00,"0.17");
ShopOrderItem c = new ShopOrderItem("light","003",1,(double)30.00,"0.17");
ShopOrderItem d = new ShopOrderItem("apple","001",5,(double)4.00,"0.17");
ShopOrderItem e = new ShopOrderItem("light","003",10,(double)30.00,"0.17");
ShopOrderItem f = new ShopOrderItem("apple","001",1,(double)4.00,"0.17");
ShopOrderItem g = new ShopOrderItem("pen","004",3,(double)3.00,"0.17");

List<ShopOrderItem> orderA = new ArrayList<ShopOrderItem>();
orderA.add(a);
orderA.add(b);
orderA.add(c);

List<ShopOrderItem> orderB = new ArrayList<ShopOrderItem>();
orderB.add(d);
orderB.add(e);

List<ShopOrderItem> orderC = new ArrayList<ShopOrderItem>();
orderC.add(f);
orderC.add(g);
公共类测试列表{
ShopOrderItem a=新的ShopOrderItem(“苹果”,“001”,2,(双)4.00,“0.17”);
ShopOrderItem b=新的ShopOrderItem(“香蕉”,“002”,1,(双)2.00,“0.17”);
ShopOrderItem c=新的ShopOrderItem(“轻”、“003”、1、(双)30.00、“0.17”);
ShopOrderItem d=新的ShopOrderItem(“苹果”,“001”,5,(双精度)4.00,“0.17”);
ShopOrderItem e=新的ShopOrderItem(“轻”、“003”、10、(双)30.00、“0.17”);
ShopOrderItem f=新的ShopOrderItem(“苹果”,“001”,1,(双)4.00,“0.17”);
ShopOrderItem g=新的ShopOrderItem(“笔”,“004”,3,(双)3.00,“0.17”);
List orderA=new ArrayList();
订单a.添加(a);
订单a.添加(b);
订单a.添加(c);
List orderB=new ArrayList();
订单b.添加(d);
订单b.添加(e);
List orderC=new ArrayList();
订单c.添加(f);
订单c.添加(g);
}

我想将这3个列表合并为一个列表。 如果名称相同,则添加计数。 例如,我的新列表将有一行,名称是apple,计数是三个订单的apple总和。
如何合并这三个列表?

您可以将这三个列表添加到一个列表中,对其进行排序,然后将它们放入地图中。然后把地图变成一个列表

    List<ShopOrderItem> merged = new ArrayList<ShopOrderItem>();
    merged.addAll(orderA);
    merged.addAll(orderB);
    merged.addAll(orderC);

    Collections.sort(merged, new Comparator<ShopOrderItem>() {

        @Override
        public int compare(ShopOrderItem o1, ShopOrderItem o2) {
            return (Integer.parseInt(o1.getItemcode()) - Integer.parseInt(o2.getItemcode()));

        }
    });
    HashMap<String,Integer> map = new HashMap<String,Integer>();
    for (ShopOrderItem shopOrderItem : merged) {
        if(map.containsKey(shopOrderItem.getItemcode())){
            int current = map.get(shopOrderItem.getItemcode());
            current+=shopOrderItem.getCount();
            map.remove(shopOrderItem.getItemcode());
            map.put(shopOrderItem.getItemcode(), current);
        }else{
            map.put(shopOrderItem.getItemcode(), shopOrderItem.getCount());
        }
    }
    List<ShopOrderItem> result = new ArrayList<ShopOrderItem>();
    Set<Entry<String,Integer>> set = map.entrySet();
    for (Entry<String, Integer> entry : set) {
        if(entry.getKey().equals(a.getItemcode())){
            ShopOrderItem t = new ShopOrderItem(a.getName(), a.getItemcode(), entry.getValue(), a.getPrice(), a.getTaxRate());
            result.add(t);  
        }else if(entry.getKey().equals(b.getItemcode())){
            ShopOrderItem t = new ShopOrderItem(b.getName(), b.getItemcode(), entry.getValue(), b.getPrice(), b.getTaxRate());
            result.add(t);  
        }
        else if(entry.getKey().equals(c.getItemcode())){
            ShopOrderItem t = new ShopOrderItem(c.getName(), c.getItemcode(), entry.getValue(), c.getPrice(), c.getTaxRate());
            result.add(t);  
        }
        else if(entry.getKey().equals(g.getItemcode())){
            ShopOrderItem t = new ShopOrderItem(g.getName(), g.getItemcode(), entry.getValue(), g.getPrice(), g.getTaxRate());
            result.add(t);  
        }

    }
List merged=new ArrayList();
合并后的.addAll(orderA);
合并后的.addAll(orderB);
合并后的.addAll(orderC);
Collections.sort(合并,新的Comparator(){
@凌驾
公共整数比较(ShopOrderItem o1、ShopOrderItem o2){
返回(Integer.parseInt(o1.getItemcode())-Integer.parseInt(o2.getItemcode());
}
});
HashMap=newHashMap();
用于(ShopOrderItem ShopOrderItem:合并){
if(map.containsKey(shopOrderItem.getItemcode()){
int current=map.get(shopOrderItem.getItemcode());
当前+=shopOrderItem.getCount();
remove(shopOrderItem.getItemcode());
put(shopOrderItem.getItemcode(),当前);
}否则{
put(shopOrderItem.getItemcode(),shopOrderItem.getCount());
}
}
列表结果=新建ArrayList();
Set=map.entrySet();
用于(条目:集合){
if(entry.getKey().equals(a.getItemcode())){
ShopOrderItem t=新的ShopOrderItem(a.getName()、a.getItemcode()、entry.getValue()、a.getPrice()、a.getTaxRate());
结果:添加(t);
}else if(entry.getKey().equals(b.getItemcode())){
ShopOrderItem t=新的ShopOrderItem(b.getName()、b.getItemcode()、entry.getValue()、b.getPrice()、b.getTaxRate());
结果:添加(t);
}
else if(entry.getKey().equals(c.getItemcode())){
ShopOrderItem t=新的ShopOrderItem(c.getName()、c.getItemcode()、entry.getValue()、c.getPrice()、c.getTaxRate());
结果:添加(t);
}
else if(entry.getKey().equals(g.getItemcode())){
ShopOrderItem t=new ShopOrderItem(g.getName()、g.getItemcode()、entry.getValue()、g.getPrice()、g.getTaxRate());
结果:添加(t);
}
}

用法:
groupbyname和sumcount(orderA、orderB、orderC)

静态列表GroupByName和SumCount(列表…订单){
列表合并=新建ArrayList();
对于(列表顺序:订单){
合并。添加全部(订单);
}
//按名称分组,因为这是我们的不变量
HashMap grouped=新建HashMap();
对于(ShopOrderItem项目:合并){
字符串名称=item.getName();
if(分组。容器(名称)){
ShopOrderItem当前=分组的.get(名称);
//总数
current.setCount(current.getCount()+item.getCount());
}否则{
分组。放置(名称、项目);
}
}
//重用以减少内存占用
合并。清除();
//准备结果
for(字符串键:grouped.keySet()){
merged.add(grouped.get(key));
}
返回合并;
}
希望有帮助,先生

使用Java 8:

Collection<ShopOrderItem> result = Stream.of(orderA, orderB, orderC)
                                            .flatMap(Collection::stream)
                                            .collect(Collectors.groupingBy(
                                                        ShopOrderItem::getItemcode, 
                                                        Collectors.reducing(new ShopOrderItem(), Use::merge)
                                                    ))
                                            .values();

private static ShopOrderItem merge(ShopOrderItem s1, ShopOrderItem s2)
{
    return new ShopOrderItem(s2.getName(), s2.getItemcode(), s1.getCount() + s2.getCount(), s2.getPrice(), s2.getTaxRate());
}
Collection result=Stream.of(orderA、orderB、orderC)
.flatMap(集合::流)
.collect(收集器.groupingBy(
ShopOrderItem::getItemcode,
Collectors.reduceing(new ShopOrderItem(),Use::merge)
))
.values();
私有静态ShopOrderItem合并(ShopOrderItem s1、ShopOrderItem s2)
{
返回新的ShopOrderItem(s2.getName()、s2.getItemcode()、s1.getCount()+s2.getCount()、s2.getPrice()、s2.getTaxRate());
}

你让它工作了吗?注意:我对
merge()
并不完全满意,因为我必须使用
s2
来避免一些
null
值。
Collection<ShopOrderItem> result = Stream.of(orderA, orderB, orderC)
                                            .flatMap(Collection::stream)
                                            .collect(Collectors.groupingBy(
                                                        ShopOrderItem::getItemcode, 
                                                        Collectors.reducing(new ShopOrderItem(), Use::merge)
                                                    ))
                                            .values();

private static ShopOrderItem merge(ShopOrderItem s1, ShopOrderItem s2)
{
    return new ShopOrderItem(s2.getName(), s2.getItemcode(), s1.getCount() + s2.getCount(), s2.getPrice(), s2.getTaxRate());
}