2013-04-08 12 views
1

我有兩個列表,其中匹配索引列表之間的項目鏈接。第一列表提供了系在第二列表值的鍵:遞歸聚合列表的鍵控對中的重複

List<Double> a1 = [10,20,20,30,10];   // keys 
List<Double> y1 = [2012,2013,2012,2012,2013]; // values 

欲從密鑰(指數)列表以這樣的方式,當重複被發現,該鍵的值被添加刪除重複。因此,例如,如果找到兩個鍵值爲10的鍵,我想用一個值爲20的單個鍵替換這兩個鍵。此過程將被重複,直到沒有重複的鍵剩下爲止。所以我想這樣的列表輸出:

List<Double> a1 = [60,30]; 
List<Double> y1 = [2012,2013]; 

我試圖用下面的代碼來解決這個問題,但輸出是不正確的。

y2=new ArrayList<Double>(); 
a2 = new ArrayList<Double>(); 
String y = ""; 
double a = 0; 

for (int i = 0; i < y1.size(); i++) { 

    if (y1.get(i).equals(y)) { 
     a = a + y1.get(i); 
    } else { 
     if (!y.equals("")) { 
      y2.add(y); 
      a2.add(a); 
     } 

     y = y1.get(i); 
     a = a1.get(i); 
    } 
} 

y2.add(y); 
a2.add(a); 

任何幫助表示讚賞,謝謝。

+2

你試過了什麼? – Ankit 2013-04-08 10:08:26

+0

只需將列表中的元素轉儲到Set中,它將取出重複項。 – 2013-04-08 10:08:45

+2

使用'Map '。 – SudoRahul 2013-04-08 10:09:04

回答

6

使用,因爲Y1陣列沒有下令Map

Map<Double, Double> map = new HashMap<>(); 
for (int i = 0; i < y1.size(); i++) { 
    double oldValue = map.containsKey(y1.get(i)) ? map.get(y1.get(i)) : 0.0; 

    map.put(y1.get(i), oldValue + a1.get(i)); 
} 

y1.clear(); 
a1.clear(); 

for (Entry<Double, Double> entry : map.entrySet()) { 
    y1.add(entry.getKey()); 
    a1.add(entry.getValue()); 
} 
+1

您應該添加代碼以將值放入地圖中,回到列表中,正如問題所要求的那樣。此外,'TreeMap'可能更合適,可以輕鬆地按排序順序獲取列表。 – hyde 2013-04-08 10:29:22

+0

@hyde現在把數據放回列表中 – 2013-04-08 10:34:24

+0

非常感謝您:))它的作品完美 – user2199280 2013-04-08 10:42:37

0
private void method(List<Double> y1, List<Double> a1) { 
    for (int i = 0; i < y1.size(); i++) { 
     if (y1.get(i) != -1) { 
     for (int j = i + 1; j < y1.size(); j++) { 
      if (y1.get(i).equals(y1.get(j))) { 
      y1.set(j, -1d); 
      a1.set(i, a1.get(i) + a1.get(j)); 
      a1.set(j, -1d); 
      } 
     } 
     } 
    } 
    for (Iterator<Double> itr = y1.iterator(); itr.hasNext();) 
     if (itr.next().equals(-1d)) 
     itr.remove(); 
    for (Iterator<Double> itr = a1.iterator(); itr.hasNext();) 
     if (itr.next().equals(-1d)) 
     itr.remove(); 
    } 
0

你的代碼失敗。當你想保留Y1之間的映射和A1,你應該創建這個值類,並創建這個類的一個數組:

public class Elem implements Comparable<Elem> 
{ 
    double year; 
    double value; 

// getters and setters 

public double getValue() { 
    return value; 
} 

public void setValue(double value) { 
    this.value = value; 
} 


public int compareTo(Elem item) 
    { 
     if (this.getYear() == item.getYear()) 
     return 0; 
    else if (this.getYear() > item.getYear()) 
     return 1; 
    else 
     return -1; 
} 

public int hashCode() 
{ 
    StringBuffer buffer = new StringBuffer(); 
    buffer.append(this.year); 
    buffer.append(this.value); 
    return buffer.toString().hashCode(); 
} 

public boolean equals(Object item) 
{ 
    if (item instanceof Elem) 
    { 
     Elem e = (Elem) item; 
     if (e.getYear() == this.getYear()) 
     { 
      return true; 
     } 
     return false; 
    } 
    return false; 
} 
} 

現在你可以箱子的陣列,pupulate和排序是:

List<Elem> y1 = new ArrayList<Elem>(); 

    //---- populate array ---- 

    Collections.sort(y1); 

    ArrayList<Elem> y2; 

    y2 = new ArrayList<Elem>(); 
    Elem y = null; 

    double a = 0; 

    for(int i = 0;i<y1.size();i++){ 
     if((y != null) && (y1.get(i).equals(y))){ 
      a = y.getValue() + y1.get(i).getValue(); 
      y1.get(i).setValue(a); 
      y = y1.get(i); 
     } 
     else{ 
      if(y != null) { 
       y2.add(y); 
      } 

      y = y1.get(i); 
     } 


    } 
    if(y != null) { 
     y2.add(y); 
    }