aatish rana aatish rana - 3 months ago 18
Java Question

Whats wrong with my logic

i have an ArrayList of my custom class

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


this is a small sample of data i am receiving, but most of it follows same pattern.

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


i want to combine this repeated data and store that in a new list like this

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


However i am not receiving correct output

Here's my logic

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);
}
}
}
}

Answer

I approached it this way:

  • Get the similar ones (the rows with same Company,Category and Model)
  • Group them.
  • Merge them.
  • Get your list.

May that help:

Use addToMap method to add elements to a HashMap while DetailedData objects are on stream. Generate the list out of it, merging them.

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;
}
Comments