2011-02-11 71 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()); 
    } 

} 

为清楚起见,我已经省略了一些空检查。