我的逻辑错误

时间:2016-08-09 12:58:13

标签: java arrays sorting arraylist filtering

我有一个自定义类的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);
            }       
        }
    }
} 

2 个答案:

答案 0 :(得分:2)

我发现了一种利用溪流的简洁方法:

List<DetailedData> data = getList();

List<DetailedData> temp = data.stream()
    .collect(Collectors.groupingBy(DetailedData::getModel))
    .values().stream()
    .map(DetailedData::merge)
    .collect(Collectors.toList());          

temp.forEach(System.out::println);

输出:

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

DetailedData::merge是将DetailedData列表合并为1的方法。例如:

public static DetailedData merge(List<DetailedData> list) {
    if(list.isEmpty()) // Not possible after 'groupingBy'.
        return null;

    if(list.size() == 1)
        return list.get(0);

    // At least 2 elements:
    DetailedData ret = list.get(0);
    ret.ImageBuffer2 = list.get(1).ImageBuffer1;
    return ret;
}

请参阅Full Code

答案 1 :(得分:1)

我这样接近:

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

可能会有所帮助:

使用addToMap方法在DetailedData对象正在传输时向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;
}