答案 0 :(得分:1)
以下是逻辑:
循环遍历TreeMap并打印所需的结果。
public class FindMaxArrayList {
public static void main(String[] args) {
List<Marcket> marcketList=new ArrayList<>();
Map<String,Double> maxofArrayList=new TreeMap<>();
// Create Sample Data
Marcket m1=new Marcket("BitTrex", "ETH/BTC",0.518);marcketList.add(m1);
Marcket m2=new Marcket("Liqui", "ETH/BTC",0.658);marcketList.add(m2);
Marcket m3=new Marcket("Yobit", "ETH/BTC",0.658);marcketList.add(m3);
Marcket m4=new Marcket("BitTrex", "ETH/BTC",0.618);marcketList.add(m4);
Marcket m5=new Marcket("Liqui", "ETH/BTC",0.458);marcketList.add(m5);
Marcket m6=new Marcket("Yobit", "ETH/BTC",0.758);marcketList.add(m6);
Marcket m11=new Marcket("BitTrex", "LTC/BTC",0.818);marcketList.add(m11);
Marcket m12=new Marcket("Liqui", "LTC/BTC",0.458);marcketList.add(m12);
Marcket m13=new Marcket("Yobit", "LTC/BTC",0.758);marcketList.add(m13);
Marcket m14=new Marcket("BitTrex", "LTC/BTC",0.518);marcketList.add(m14);
Marcket m15=new Marcket("Liqui", "LTC/BTC",0.858);marcketList.add(m15);
Marcket m16=new Marcket("Yobit", "LTC/BTC",0.958);marcketList.add(m16);
//Processing List
for(Marcket m:marcketList) {
if(maxofArrayList.containsKey(m.getPair()+":"+m.getMarcketName())) {
if(maxofArrayList.get(m.getPair()+":"+m.getMarcketName())<m.getPercentage()){
maxofArrayList.put(m.getPair()+":"+m.getMarcketName(), m.getPercentage());
}
}else {
maxofArrayList.put(m.getPair()+":"+m.getMarcketName(), m.getPercentage());
}
}
//Printing List
for(String key:maxofArrayList.keySet()) {
String[] content=key.split(":");
System.out.println(content[1]+"\t"+content[0]+"\t"+maxofArrayList.get(key));
}
}
}
class Marcket{
private String marcketName;
private String pair;
private double percentage;
public Marcket(String marcketName, String pair, double percentage) {
this.marcketName = marcketName;
this.pair = pair;
this.percentage = percentage;
}
public String getMarcketName() {
return marcketName;
}
public void setMarcketName(String marcketName) {
this.marcketName = marcketName;
}
public String getPair() {
return pair;
}
public void setPair(String pair) {
this.pair = pair;
}
public double getPercentage() {
return percentage;
}
public void setPercentage(float percentage) {
this.percentage = percentage;
}
}