2012-08-02 41 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); 

} 

}

检查是否适用于您。