2012-08-02 40 views
0

我有交易的列表,每個行業現在有一個像源的一些屬性,從行業的名單,我想要得到的是具有相同源值各行各業將它們組合成一個行業,例如如果條件在arraylist中滿足,如何比較元素併合並?

tradeName  quote   source   quantity  price 
Google   GOOG   Goldman Sachs  15   610 
Microsoft  MSFT   Barclays    400   28 
Google   GOOG   Goldman Sachs  45   610 
Google   GOOG   Goldman Sachs  40   610 
Microsoft  MSFT   Barclays    1000   28 

現在基於源的信息,我應該結合行業,所以我更新的貿易清單將是

tradeName  quote   source   quantity  price 
Google   GOOG   Goldman Sachs  100   610 
Microsoft  MSFT   Barclays    1400   28 

我不知道比較部分,如何去解決呢?


試圖

下面的方法,

for (Trade trade : tradeList) 
{ 
    //Not sure how to compare this.trade.source with all sources 
    //of all trades present in the trade. 
    //Logic should be if source matches then quantity should be added 
    //but am not sure how comparison would work. 
} 

Class Trade 
{ 

private tradeName; 
private quote; 
private source; 
private quantity; 
private price; 

//Getters and Setters for each of above mentioned attributes. 

} 
+0

你有什麼試過?你需要知道如何編寫循環?循環內的比較?你究竟需要知道什麼?一個僞代碼算法? – 2012-08-02 15:59:43

+0

@ngmiceli:我正在遍歷循環,但現在我將如何在循環內與ar​​raylist的所有元素進行比較,這是我掛斷的地方。 – Rachel 2012-08-02 16:00:28

+0

如果您向我們展示您目前爲迭代循環所寫的內容,我們可以更好地幫助 – 2012-08-02 16:01:02

回答

1

雖然我同意使用一個HashMap可能變得更高效,我寧願試圖保持到O.P的提供方法。如果我們要遍歷每個成員,那麼讓我們來檢查一下到目前爲止是否找到該成員。如果是這樣,請修改他。如果沒有,請添加他。我建議把所有這些都列入新的清單,然後根據新清單做任何你想做的事情(把舊的設爲新的,印刷新的,把新的電子郵件發送給俄羅斯總統)。

那麼怎麼樣跟隨? (我沒有編譯這個,所以原諒任何錯別字)

//Create a new list to store our "simplified" results into 
ArrayList<Trade> newTradeList = new ArrayList<Trade>(tradeList.size()); 

//For each trade in the old list, call it "trade" and... 
for(Trade trade : tradeList){ 

    //First we ask, is there already a trade in this list with the same source? 
    //indexOf finds where in the array the element lives. I store that result in index 
    int index = newTradeList.indexOf(trade); 

    //If the element isn't in our list yet, indexOf will return -1. 
    //If the result is NOT -1, then we have already seen a trade with this source before 
    if(index != -1) { 
     //In that case, get that element. We know what index it lives at so grab it. 
     Trade t = newTradeList.get(index); 
     //Then, do whatever operations to combine the two trades. I assumed you add the quantities. 
     //So the trade in our new list's quantity should be what it was, plus the new trade's quantity 
     t.setQuantity(t.getQuantity() + trade.getQuantity()); 
    } 

    //But if we have never seen this source before, let's add it to our our new list 
    else { 
     newTradeList.add(trade); 
    } 
} 

這當然,作出一些假設。爲了使.indexOf工作,您需要您的Trade類具有適當定義的equals方法(檢查源代碼或源代碼和價格是否相同 - 看起來是否正確)。如果您重新定義equals,那麼您還應該定義hashCode。它也假定你有一個方法.addQuantity只是因爲它使得代碼比get和set的組合更清潔。


HashMap中的具有的優勢幾乎即時的(O(1))檢查是否元素已經存在,並確保它的元素是唯一的(作爲一個HashMap是一個集合,一組不能有重複的元素)。代碼看起來非常相似,只不過我們可以利用HashMap的containsKey方法,而不是indexOf

+0

我可以將其更改爲hashMap,我沒有任何這樣的約束。 – Rachel 2012-08-02 16:25:29

+0

我會說實話,我不明白你的代碼。 – Rachel 2012-08-02 16:52:11

+0

我會編輯它,並盡我所能解釋... – 2012-08-02 17:04:35

2

我想我會創建一個HashMap<String, Trade>,使用tradeName+quote+source作爲一個鍵,然後從列表中添加地圖各個行業到適當項目。

例子:

Map<String, Trade> map = new HashMap<String, Trade>(); 
for (Trade trade : tradeList) { 
    String key = trade.tradeName + "#" + trade.quote + "#" + trade.source; // signature, what you merge by 
    if (map.containsKey(key)) { 
     map.put(key, trade); // the first trade with such a signature 
    } else { 
     // not the first, so merge it with the existing one 
     map.get(key).add(trade); // you'll have to implement the Trade.add() method 
    } 
} 
List<Trade> merged = new LinkedList<Trade>(map.values()); 
+0

您能否提供更詳細的示例 – Rachel 2012-08-02 16:05:57

+0

女士們,先生們,請在downvoting時發表評論。 – Qnan 2012-08-02 16:58:32

+0

@Rachel檢查編輯。當然,這假設英鎊符號(「#」)不出現在任何關鍵字段中。如果是這樣,挑一個沒有。 – Qnan 2012-08-02 17:10:26

-1

我覺得應該這樣做。

Trade gsTrades = new Trade(); 
    Trade barclaysTrades = new Trade(); 

    for(Trade trade: tradeList){ 
     if(trade.getSource().equals("GS")){ 
      gsTrades.setQuantity(gsTrades.getQuantity()+trade.getQuantity()); 
      gsTrades.setPrice(gsTrades.getPrice()+trade.getPrice()); 
     }else{ 
      barclaysTrades.setQuantity(barclaysTrades.getQuantity()+trade.getQuantity()); 
      barclaysTrades.setPrice(barclaysTrades.getPrice()+trade.getQuantity()); 
     } 
    } 
    System.out.println("GS trade details = " + gsTrades.toString()); 
    System.out.println("Barclays trade details = " + barclaysTrades.toString()); 
+0

這不起作用,因爲我只在這裏分享了一個例子,我的列表有1000個交易,需要根據同一個源條目進行比較和合並交易。 – Rachel 2012-08-02 16:54:02

0

請檢查該方法2:

的Java Bean:

public class Trade { 

private String tradeName; 
private String quote; 
private String source; 
private Integer quantity; 
private Integer price; 

public String getTradeName() { 
    return tradeName; 
} 

public void setTradeName(String tradeName) { 
    this.tradeName = tradeName; 
} 

public String getQuote() { 
    return quote; 
} 

public void setQuote(String quote) { 
    this.quote = quote; 
} 

public String getSource() { 
    return source; 
} 

public void setSource(String source) { 
    this.source = source; 
} 

public Integer getQuantity() { 
    return quantity; 
} 

public void setQuantity(Integer quantity) { 
    this.quantity = quantity; 
} 

public Integer getPrice() { 
    return price; 
} 

public void setPrice(Integer price) { 
    this.price = price; 
} 

@Override 
public int hashCode() { 
    final int prime = 31; 
    int result = 1; 
    result = prime * result + ((source == null) ? 0 : source.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; 
    Trade other = (Trade) obj; 
    if (source == null) { 
     if (other.source != null) 
      return false; 
    } else if (!source.equals(other.source)) 
     return false; 
    return true; 
} 

@Override 
public String toString() { 
    StringBuilder builder = new StringBuilder(); 
    builder.append("Trade [tradeName="); 
    builder.append(tradeName); 
    builder.append(", quote="); 
    builder.append(quote); 
    builder.append(", source="); 
    builder.append(source); 
    builder.append(", quantity="); 
    builder.append(quantity); 
    builder.append(", price="); 
    builder.append(price); 
    builder.append("]"); 
    return builder.toString(); 
} 
} 

- 編輯 -

檢查這一點,這只是很簡單:

import java.util.ArrayList; 
import java.util.List; 

public class TradeTest { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    Trade t1 = new Trade(); 
    t1.setPrice(610); 
    t1.setQuantity(15); 
    t1.setQuote("GOOG"); 
    t1.setSource("Goldman Sachs"); 
    t1.setTradeName("Google"); 

    Trade t2 = new Trade(); 
    t2.setPrice(28); 
    t2.setQuantity(400); 
    t2.setQuote("MSFT"); 
    t2.setSource("Barclays"); 
    t2.setTradeName("Microsoft"); 

    Trade t3 = new Trade(); 
    t3.setPrice(610); 
    t3.setQuantity(45); 
    t3.setQuote("GOOG"); 
    t3.setSource("Goldman Sachs"); 
    t3.setTradeName("Google"); 

    Trade t4 = new Trade(); 
    t4.setPrice(610); 
    t4.setQuantity(40); 
    t4.setQuote("GOOG"); 
    t4.setSource("Goldman Sachs"); 
    t4.setTradeName("Google"); 

    Trade t5 = new Trade(); 
    t5.setPrice(28); 
    t5.setQuantity(1000); 
    t5.setQuote("MSFT"); 
    t5.setSource("Barclays"); 
    t5.setTradeName("Microsoft"); 

    List<Trade> trades = new ArrayList<Trade>(); 
    trades.add(t1); 
    trades.add(t2); 
    trades.add(t3); 
    trades.add(t4); 
    trades.add(t5); 

    List<Trade> googleTrades = new ArrayList<Trade>(); 
    List<Trade> microsoftTrades = new ArrayList<Trade>(); 

    Integer googleQuantities = 0; 
    Integer microsoftQuantities = 0; 

    for (Trade trade : trades) { 
     if (trade.getSource().equals("Goldman Sachs")) { 
      googleTrades.clear(); 
      googleQuantities += trade.getQuantity(); 
      trade.setQuantity(googleQuantities); 
      googleTrades.add(trade); 
     } else if (trade.getSource().equals("Barclays")) { 
      microsoftTrades.clear(); 
      microsoftQuantities += trade.getQuantity(); 
      trade.setQuantity(microsoftQuantities); 
      microsoftTrades.add(trade); 
     } 
    } 

    System.out.println("Google trades: \n"); 
    System.out.println(googleTrades); 
    System.out.println("\n"); 
    System.out.println("Microsoft trades: \n"); 
    System.out.println(microsoftTrades); 

} 

}

檢查是否適用於您。