2011-02-11 126 views
1

我想實現兩個HashMaps.Below的組合是我的代碼,但我無法產生我真正想要的。 我的代碼的結果是:實現兩個哈希映射

{{userId1=clientID1}=timestamp1} 
{{userId1=clientID1, userId2=clientID2}=timestamp2, {userId1=clientID1, userId2=clientID2}=timestamp1} 

其實我需要:

{{userId1=clientID1}=timestamp1},{userId2=clientID2}=timestamp2},{userId3=clientID3}=timestamp3}..} 

就是一對一mapping.Can我們真的落實這一要求如果沒有任何人可以提出這個辦法?我會非常感謝...

public static void main (String args[]) 
{ 
HashMap map = new HashMap<String, String>(); 
HashMap<HashMap,String> multi = new HashMap(); 
map.put("userId1", "clientID1"); 
multi.put(map, "timestamp1"); 

System.out.println(multi); //gives me correct requirement 

map.put("userId2", "clientID2"); 
multi.put(map, "timestamp2"); 

System.out.println(multi); //since the table is filled again a combination of values results(each time it keeps on increasing) which is not really required as per my requirement .Hope my problem is clear.please do help me .. 
} 
+3

你想要做的事情對我來說沒有任何意義。你能否用非代碼術語來闡述你想要達到的目標,包括細節? – 2011-02-11 11:38:06

+1

如果我沒有弄錯,這是什麼類的發明。 – biziclop 2011-02-11 11:40:58

回答

0

你需要的不是兩個HashMap,而只有一個。地圖的值應該由client_id和時間戳組成。

看到這個代碼:

public class Compose { 
    int clientId; 
    Date timestamp; 
} 

,地圖將HashMap中,其中的關鍵是USER_ID和值是撰寫。

0

問題在於,當你沿着第一個HashMap增加元素的數量。考慮在第一個和第二個add操作期間,map HashMap包含的內容爲multi

要解決這個問題,你需要給每個你添加了一些時間創建一個新的HashMap來multi

HashMap<HashMap,String> multi = new HashMap(); 
HashMap<String,String> key = new HashMap<String,String>(); 
key.add("userID","clientID"); 
multi.add(key,"timestamp"); 

HashMap<String,String> key = new HashMap<String,String>(); 
key.add("userID2","clientID2"); 
multi.add(key,"timestamp"); 

你可以看到,將變得有些笨拙,是不是最有效的記憶。我會做什麼,而不是也許是創建一個像userID:clientID組合字符串鍵和使用,在多代替

HashMap<HashMap,String> multi = new HashMap(); 
multi.add("userID:clientID","timestamp"); 

我不知道這是否可能爲您的應用程序,但。

0

你應該創建一個自定義對象,而不是,是這樣的:

public class Mapping{ 

    private final String userId; 
    private final String clientId; 

    public Mapping(final String userId, final String clientId){ 
     this.userId = userId; 
     this.clientId = clientId; 
    } 

    public String getUserId(){ 
     return userId; 
    } 

    public String getClientId(){ 
     return clientId; 
    } 

    // implement hashcode and equals using userId and clientId, 
    // or it won't work !!! 

} 

現在使用這個對象作爲你的HashMap中的鍵(時間戳也許應該是一個long/Long):

Map<Mapping,Long> clientMappings = new HashMap<Mapping,Long>(); 

// register a client mapping 
clientMappings.put(new Mapping(userId, clientId), System.currentTimeMillis()); 

// get a client mapping timestamp, will be null if no such mapping exists 
Long timeStamp = clientMappings.get(new Mapping(userId, clientId)); 
0

我認爲錯誤是在該行

map.put("userId2", "clientID2"); 

您正在將值添加到舊的Hashmap,但您需要創建一個Seound HashMap。

以下應該做的伎倆。

public static void main (String args[]) 
{ 
HashMap map = new HashMap<String, String>(); 
HashMap<HashMap,String> multi = new HashMap(); 
map.put("userId1", "clientID1"); 
multi.put(map, "timestamp1"); 

System.out.println(multi); //gives me correct requirement 

map = new HashMap(); 
map.put("userId2", "clientID2"); 
multi.put(map, "timestamp2"); 

System.out.println(multi); //since the table is filled again a combination of values results(each time it keeps on increasing) which is not really required as per my requirement .Hope my problem is clear.please do help me .. 
} 
0

我不知道到底爲什麼你想要做你所描述的,但如果你細說我敢肯定有比你正在試圖用一個更好的設計。

但是,如果你想繼續沿着相同的路徑,那麼你的問題似乎是你一遍又一遍地更新「地圖」,這個引用沒有改變。每次更改地圖上的元素時,您都需要爲其密鑰創建一個新的HashMap對象,此更改將過濾爲多個。

你有沒有嘗試過這樣的事情:

Map<Map, String> multi = new HashMap<Map, String>(); 
Map<String, String> map = new HashMap<String, String>(); 

map.put("userId1", "clientID1"); 
multi.put(map, "timestamp1"); 

map = new HashMap<String, String>(); 

map.put("userId2", "clientID2"); 
multi.put(map, "timestamp2"); 

通知的上述地圖的第二個實例。

2

你爲什麼不能簡單地使用兩個單獨的地圖,一個是userId - > clientId,另一個是userId - >時間戳?首先使用Map作爲Map的關鍵是什麼?

請記住,地圖在Java中是可變的,並且使用可變對象作爲Map的關鍵字通常是非常糟糕的做法。

0

一個解決方案是創建一個類,含有的clientId,用戶id和時間戳字段:


class ClientDetails { 
    String clientId; 
    String userId; 
    Date timestamp; 
} 

,然後創建使用的clientId作爲密鑰和ClientDetails作爲值的映射圖。

但是,如果您需要使用此類作爲鍵 - 您必須至少重寫equals(Object obj)方法。以下是一個示例:


public class Main { 

    public class Client { 

     public String clientId; 
     public String userId; 

     public Client(String p_clientId, String p_userID) { 

      this.clientId = p_clientId; 
      this.userId = p_userID; 
     } 

     @Override 
     public boolean equals(Object obj) { 

      boolean result = false; 
      if (obj != null && obj instanceof Client) { 
       Client client = (Client) obj; 
       if (client.clientId.equals(clientId) 
         && client.userId.equals(userId)) { 

        result = true; 
       } else { 
        result = false; 
       } 
      } else { 
       result = super.equals(obj); 
      } 
      return result; 
     } 
    } 

    public static void main(String[] args) { 

     Main main = new Main(); 
     Map<Client, Date> hashMap = new HashMap<Client, Date>(); 
     hashMap.put(main.new Client("clientId1", "userId1"), new Date()); 
    } 

} 

爲清楚起見,我已經省略了一些空檢查。