简体   繁体   English

具有冲突解决方案的Java hashtable

[英]java hashtable with collision resolution

I want to get all the values(multiple) of a particular key.But im getting only one value?I dont know how to print all the values.Great help if someone correct the code..did not get any help from google search.. 我想获取一个特定键的所有值(多个)。但是我仅获得一个值? 。

import java.util.*;


public class hashing
{
  public static void main(String args[])
  {
       String[] ary=new String[4];
       String key;
       char[] chrary;
       ary[0]=new String("abcdef");
       ary[1]=new String("defabc");
       ary[2]=new String("ghijkl");
       ary[3]=new String("jklghi");
       Hashtable<String, String> hasht = new Hashtable<String, String>(); 
       for(int i=0;i<4;i++){
          chrary=ary[i].toCharArray();
          Arrays.sort(chrary);
          key=new String(chrary);
          hasht.put(key,ary[i]);
       }
       Enumeration iterator = hasht.elements();
    while(iterator.hasMoreElements()) {
      String temp = (String)iterator.nextElement();
      System.out.println(temp);
    }

 }
}

PS:output is defabc jklghi.I want abcdef defabc ghijkl jklghi. PS:输出是defabc jklghi。我想要abcdef defabc ghijkl jklghi。

Hashtables can only contain one value per key. 哈希表每个键只能包含一个值。 To store multiple values, you should either 要存储多个值,您应该

  1. Store a collection (eg List<String> or array) per key. 每个键存储一个集合(例如List<String>或数组)。 Note that you'll have to initialise the collection prior to insertion of the first value corresponding to that key 请注意,您必须在插入与该键对应的第一个值之前初始化集合
  2. Use a MultiMap 使用多重地图

Note that many MultiMap implementations exist. 请注意,存在许多MultiMap实现。 The Oracle docs provide a simple implementation too (see here , and search for MultiMap ) Oracle文档也提供了一个简单的实现(请参阅此处 ,并搜索MultiMap

The way HashMaps work is that there is only one value for a given key. HashMaps的工作方式是给定键只有一个值。 So if you call: 因此,如果您致电:

map.put(key, value1);
map.put(key, value2);

the second line will override the value corresponding to the key. 第二行将覆盖与键对应的值。

Regarding your comment about collision, it means something different. 关于您对碰撞的评论,这意味着有所不同。 Internally, a HashMap stores the key/value pairs in buckets that are defined based on the hashcode of the key (hence the name: hashmap). 在内部,HashMap将键/值对存储在基于键的哈希码定义的存储桶中(因此,名称为:hashmap)。 In the (low probability if the hashcode function is good) case where two non-equal keys have the same hashcode, the implementation needs to make sure that querying the hashmap on one of those keys will return the correct value. 在两个非等键具有相同哈希码的情况下(如果哈希码函数良好,则可能性很小),实现需要确保查询其中一个键的哈希图将返回正确的值。 That is where hash collision need to be handled. 那就是需要处理哈希冲突的地方。

That's not what collision resolution is meant to do. 这不是解决冲突的目的。 Collision resolution lets you handle the case when two object with different keys would go into the same "bucket" in the hash map. 冲突解决方案使您可以处理以下情况:具有不同键的两个对象将进入哈希图中的同一“存储桶”。 How this resolution happens is an internal detail of the hash map implementation, not something that would be exposed to you. 解决方案的方式是哈希映射实现的内部细节,而不是向您公开的内容。

Actually, in your case, its not collision, its same key with same hashcode. 实际上,在您的情况下,它不是冲突的,它的相同键和相同的哈希码。 In general Collision occurs only if two different keys generate same hashcode , This can occur due to a bad implementation of hashCode() method. 通常,仅当两个不同的键生成相同的哈希码时才会发生冲突,这可能是由于hashCode()方法的错误实现而发生的。

Yes, java.util.HashMap will handle hash collisions, If you look at the source code of HashMap , it stores each value in a LinkedList . 是的, java.util.HashMap将处理哈希冲突,如果您查看HashMap的源代码,它将每个值存储在LinkedList That means, if two different keys with same hashcode comes in.. then both values will go into same bucket but as two different nodes in the linked list . 这意味着,如果两个具有相同哈希码的不同键进入..则两个值都将进入同一存储桶,但作为linked list两个不同节点。

Found this link online, which explain How hash map works in detail. 在线找到了此链接 ,该链接详细解释了哈希映射如何工作。

if key is the same, the value will be updated. 如果键相同,则值将被更新。 jvm will not put a new key/value for same keys... jvm不会为相同的键放一个新的键/值...

Your Hashtable<String, String> maps one string to one string. 您的Hashtable<String, String>将一个字符串映射到一个字符串。 So put replaces the value that was before linked to a specific key. 因此, put替换了链接到特定键之前的值。

If you want multiple values, you can make a Hashtable<String, []String> or a Hashtable<String, List<String>> . 如果需要多个值,则可以创建Hashtable<String, []String>Hashtable<String, List<String>>

A cleaner solution would be to use Google's Multimap which allows to associate multiple values to one key : 较干净的解决方案是使用Google的Multimap ,它可以将多个值关联到一个键:

A collection similar to a Map, but which may associate multiple values with a single key. 一个类似于Map的集合,但是可以将多个值与单个键相关联。 If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values. 如果使用相同的键但值不同的方法两次调用put(K,V),则多重映射将包含从键到两个值的映射。

You are only putting one String for each key: 您只需为每个键放置一个字符串:

hasht.put(key,ary[i]);

So if i=1 that means you put defabc , why do you expect to get multiple values for same key? 因此,如果i = 1意味着您放了defabc ,为什么还要为同一个键获取多个值?

Hashtable, like all Map keep only one value per key, the last value you set. 像所有Map一样,哈希表每个键仅保留一个值,即您设置的最后一个值。

If you want to keep all the values, just print the original array. 如果要保留所有值,只需打印原始数组即可。

String[] ary = "abcdef,defabc,ghijkl,jklghi".split(",");
System.out.println(Arrays.toString(ary));

prints 版画

[abcdef, defabc, ghijkl, jklghi]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM