2016-08-09 80 views
2

我有我的自定義類哪些錯誤與我的邏輯

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

的ArrayList這個數據我收到一個小樣本,但大部分遵循相同的模式。

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

這是整個代碼 –

+1

'我無法接收正確output' - >你收到輸出什麼? 「大部分都遵循相同的模式」 - >大部分? – lupz

回答

1

我這樣走近它:

  • 獲取類似的(具有相同公司,類別和型號的行)
  • 將它們分組。
  • 合併它們。
  • 獲取您的清單。

可能是幫助:

使用addToMap方法將元素添加到一個HashMap同時DetailedData對象投產。 從中生成列表,合併它們。

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

+0

這段代碼是用在android應用程序中的,所以不支持java 8,可以用其他方式幫忙嗎? –