簡體   English   中英

將不同的ArrayList作為值分配給Java中HashMap中的每個不同鍵

[英]Assign different ArrayList as value to every different key in a HashMap in Java

簡而言之:我必須構建沒有參數的iPsForDays方法。 此方法返回一個HashMap>,該HashMap>使用記錄並將Web日志中的日期映射到當天發生的IP地址ArrayList(包括重復的IP地址)。 例如,Sep 14映射到一個IP地址,Sep 21映射到四個IP地址,Sep 30映射到五個IP地址。 我的問題是,對於每個鍵(日期),值(ArrayList)都是相同的(包含相同的值)。 我怎樣才能擁有不同的價值? 這是代碼:

public HashMap<String, ArrayList<String>> iPsForDays(){
    HashMap<String, ArrayList<String>> mapDatesToIPs = new HashMap<String,ArrayList<String>>();
    ArrayList<String> ips = new ArrayList<String>();
    for(LogEntry le : records){
        Date d = le.getAccessTime();
        String dates = d.toString().substring(4, 10);//date is a diff format
        if(!mapDatesToIPs.containsKey(dates)){
            ips.add(le.getIpAddress());
            mapDatesToIPs.put(dates, ips);
        }
        else if(mapDatesToIPs.containsKey(dates)){
            String ip = le.getIpAddress();
            if(!ips.contains(ip)){
                ips.add(ip);
            }
        }
    }
    return mapDatesToIPs;       
}

在for循環中初始化arraylist

for(LogEntry le : records){
ips = new ArrayList<String>();
 .......
 ........

這將每天創建一個新的ArrayList並存儲在Map中

您需要在for循環中聲明ips ,如下面的代碼所示,但是還有其他問題。 請參見下面的代碼注釋和說明

public HashMap<String, ArrayList<String>> iPsForDays(){
    HashMap<String, ArrayList<String>> mapDatesToIPs = new HashMap<String,ArrayList<String>>();
    for(LogEntry le : records){
        Date d = le.getAccessTime();
        String dates = d.toString().substring(4, 10);//date is a diff format
        if(!mapDatesToIPs.containsKey(dates)){
            //Changed Below
            String ip = le.getIpAddress();
            ArrayList<String> ips = mapDatesToTps.get(dates);
            //If you need unique then why ArrayList not HashSet?
            if(!ips.contains(ip)){
              mapDatesToTps.get(dates).add(ip);
            }
        }
        else if(mapDatesToIPs.containsKey(dates)){
            /Added Here
            ArrayList<String> ips = new ArrayList<String>();
            String ip = le.getIpAddress();
        }
    }
    return mapDatesToIPs;       
}

您在for循環之外定義了對象,因此每次您都使用相同的對象時。 解決方案是為每個鍵創建新的對象。 但是我也建議進行以下更改:

  1. 獲取密鑰的現有ArrayList和您的Ip。 您在每次迭代中都再次添加了所有Ip。
  2. 如果是第一次在鍵的ArrayList中添加元素,則不需要在別的地方檢查unique
  3. 您需要檢查條件是否唯一。 如果您需要獨特的功能,為什么不使用HashSet? 對於Array而言, contains操作非常激烈!

您僅創建一個ips ArrayList實例。 對此ArrayList進行的任何更改都將反映在整個Map中。 您必須創建此ArrayList的新實例才能實現您要實現的目標。

只需移動此ArrayList<String> ips = new ArrayList<String>(); 在您的for循環條件中。 另外,您必須更改代碼以在地圖中已經存在新對象時將其添加到列表中。

這是更正的代碼段:

public HashMap<String, ArrayList<String>> iPsForDays(){
    HashMap<String, ArrayList<String>> mapDatesToIPs = new HashMap<String,ArrayList<String>>();

    for(LogEntry le : records){
        Date d = le.getAccessTime();
        String dates = d.toString().substring(4, 10);//date is a diff format
        if(!mapDatesToIPs.containsKey(dates)){
            /* Move Here */
            ArrayList<String> ips = new ArrayList<String>();
            ips.add(le.getIpAddress());
            mapDatesToIPs.put(dates, ips);
        }
        else if(mapDatesToIPs.containsKey(dates)){
            String ip = le.getIpAddress();
            /* Retrieve List */
            ArrayList<String> ips = mapDatesToIPs.get(dates);
            ips.add(ip);
        }
    }
    return mapDatesToIPs;       
}

在這里,我修改了您的代碼。 因此,您需要檢查地圖是否包含日期,如果不包含日期,則檢查arraylist ips是否為null,如果為null,則意味着我們剛剛進入循環,因此無需添加至地圖,只需初始化arraylist並開始添加ip。

    public HashMap<String, ArrayList<String>> iPsForDays(){

        HashMap<String, ArrayList<String>> mapDatesToIPs = new HashMap<String,ArrayList<String>>();
        ArrayList<String> ips = null;
        String previous_date = null;
        for(LogEntry le : records){
            Date d = le.getAccessTime();
            String dates = d.toString().substring(4, 10);//date is a diff format
            if(!mapDatesToIPs.containsKey(dates)){

                if (ips != null && ips.size>0){ //Check whether we have any ips list or it is running for first time, if first then it will be null, so no need to add to mapDatesToIps.
                    mapDatesToIPs.put(previous_date, ips);
                }
                previous_date = dates;
                ips = new ArrayList<String>();
                ips.add(le.getIpAddress());
                mapDatesToIPs.put(previous_date, null);
            }
            else if(mapDatesToIPs.containsKey(dates)){
                String ip = le.getIpAddress();
                if(!ips.contains(ip)){
                    ips.add(ip);
                }
            }
        }
//This is necessary as when for loop iteration is over, it will not go to if block but will directly exit so required to check if we do not have ips in list then add it against the last date value we have in previous_date variable.
if (ips != null && ips.size>0){
                    mapDatesToIPs.put(previous_date, ips);
                }
        return mapDatesToIPs;       
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM