我有一个自定义类的ArrayList

public class DetailedData
{
    String Company;
    String Model;
    String Category;
    String ImageType;
    String ImageBuffer1;
    String ImageBuffer2;
}


这只是我收到的一小部分数据,但大多数遵循相同的模式。

dell, inspiron 15-R, laptop, Header, 9j4AAQSk, null
dell, inspiron 15-R, laptop, Thumbnail, iVBORw0KGg, null
apple, macbook air, laptop, Header, 9j4AAQSk, null
apple, macbook air, laptop, Thumbnail, iVBORw0KGg, null
dell, xps 13, laptop, Header, 9j4AAQSk, null


我想结合这些重复的数据并将其存储在这样的新列表中

dell, inspiron 15-R, laptop, Header, 9j4AAQSk, iVBORw0KGg
apple, macbook air, laptop, Header, 9j4AAQSk, iVBORw0KGg
dell, xps 13, laptop, Header, 9j4AAQSk, null


但是我没有收到正确的输出

这是我的逻辑

 ArrayList<DetailedData> data=getList();
 ArrayList<DetailedData> temp = new ArrayList<>();
 for (int i = 0; i < data.size(); i++)
 {
    DetailedData currentData = data.get(i);
    if (temp.size() == 0)
    {
        temp.add(currentData);
    } else
    {
        for (int j = 0; j < temp.size(); j++)
        {
            DetailedData tempData = temp.get(j);
            if (tempData.Company.equals(currentData.Company))
            {
                if (tempData.ImageType.equals(currentData.ImageType))
                {}
                else
                {
                    temp.get(j).ImageBuffer2 = currentData.ImageBuffer1;
                }

            } else
            {
                temp.add(currentData);
            }
        }
    }
}

最佳答案

我这样处理:


获取相似的行(具有相同公司,类别和型号的行)
将它们分组。
合并它们。
获取您的列表。


可能有帮助:

当DetailedData对象在流中时,使用addToMap方法将元素添加到HashMap中。
 从中生成列表,将其合并。

static void addToMap(DetailedData d){
    DataIdentity identity = new DataIdentity();
    identity.Company = d.Company;
    identity.Category = d.Category;
    identity.ImageType = d.ImageType;
    identity.Model = d.Model;

    DataValue value = new DataValue();
    if(set.containsKey(identity)){
        value = set.get(identity);
    }

    if(d.ImageType == "Header"){
        value.ImageBuffer1 = d.ImageBuffer1;
    }
    else if(d.ImageType == "Thumbnail"){
        value.ImageBuffer2 = d.ImageBuffer1;
    }
    set.put(identity, value);
}

static ArrayList<DetailedData> generateList(){
    ArrayList<DetailedData> list = new ArrayList<Aatish.DetailedData>();
    for(Entry<DataIdentity, DataValue> e : set.entrySet()){
        DetailedData data = new DetailedData();
        DataIdentity di = e.getKey();
        data.Category = di.Category;
        data.Company = di.Company;
        data.ImageType = di.ImageType;
        data.Model = di.Model;

        DataValue dv = e.getValue();
        data.ImageBuffer1 = dv.ImageBuffer1;
        data.ImageBuffer2 = dv.ImageBuffer2;
        list.add(data);
    }
    return list;
}

HashMap<DataIdentity,DataValue> set;

class DetailedData {

    String Company;
    String Model;
    String Category;
    String ImageType;
    String ImageBuffer1;
    String ImageBuffer2;

    @Override
    public String toString() {
        return "[" + Company + "," + Model + "," + Category + "," + ImageType
                + "," + ImageBuffer1 + "," + ImageBuffer2 + "]";
    }

}

static class DataIdentity{
    String Company;
    String Model;
    String Category;
    String ImageType;
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((Category == null) ? 0 : Category.hashCode());
        result = prime * result
                + ((Company == null) ? 0 : Company.hashCode());
        result = prime * result + ((Model == null) ? 0 : Model.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        DataIdentity other = (DataIdentity) obj;
        if (Category == null) {
            if (other.Category != null)
                return false;
        } else if (!Category.equals(other.Category))
            return false;
        if (Company == null) {
            if (other.Company != null)
                return false;
        } else if (!Company.equals(other.Company))
            return false;
        if (Model == null) {
            if (other.Model != null)
                return false;
        } else if (!Model.equals(other.Model))
            return false;
        return true;
    }


}

static class DataValue{
    String ImageBuffer1;
    String ImageBuffer2;
}

07-25 20:12