找到最大的arraylist

时间:2018-04-04 03:45:07

标签: java algorithm arraylist

我在我的ArrayList中使用以下数据:

enter image description here

我想知道如何在下面的对中得到marketName的最大百分比

enter image description here

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

以下是逻辑:

  1. 创建一个TreeMap,它将根据对保持密钥。
  2. 使用pair和marcketName的组合作为键和
    1. 如果该密钥不存在则将其放入Map
    2. 如果当前值更大,则检查存储值 用更大的。
  3. 循环遍历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;
    }
    }