开发者

Java hashmap problem

I would like to ask

in Hashmap

how can I c开发者_如何学JAVAount all the numbers if the element ID is same?

Could anyone give some idea for me?

Or just give some useful reference links for me to study

thanks

import java.util.*;
import java.util.Iterator;

public class hash {

    public static void main(String[] args) {

        HashMap hashMap = new HashMap();
        hashMap.put("ABS", new Double(3434.34));
        hashMap.put("ABD", new Double(123.22));
        hashMap.put("ABD", new Double(123.22));
        hashMap.put("ABD", new Double(123.22));
        hashMap.put("ABD", new Double(123.22));

        Set set = hashMap.entrySet();

        Iterator i = set.iterator();
        while (i.hasNext()) {
            Map.Entry me = (Map.Entry) i.next();
            System.out.println(me.getKey() + " : " + me.getValue());
        }


    }
}


You can't. You need a data structure that supports counting keys. Map implementations don't.

Guava

One thing you can use is Guava's Multimap

Sample Code:

final Multimap<String, Double> map =
    Multimaps.newListMultimap(
        Maps.<String, Collection<Double>>newTreeMap(),
        new Supplier<List<Double>>(){

            @Override
            public List<Double> get() {
                return Lists.newArrayList();
            }});
map.put("ABS", Double.valueOf(3434.34));
map.put("ABD", Double.valueOf(123.22));
map.put("ABD", Double.valueOf(123.22));
map.put("ABD", Double.valueOf(123.22));
map.put("ABD", Double.valueOf(123.22));
for (final Multiset.Entry<String> key : map.keys().entrySet()) {
    System.out.println(
        "Key: "
        +key.getElement()
        +", count: "
        +key.getCount()
        +", values: "
        +map.get(key.getElement())
    );
}

Output:

Key: ABD, count: 4, values: [123.22, 123.22, 123.22, 123.22]
Key: ABS, count: 1, values: [3434.34]

The MultiMap in Apache Commons / Collections will also do the trick.


Plain Java

If you are not allowed to use an external library, you can still implement this functionality with a Map<String, List<Double>>. Create a method like this

Helper Method

public static <K, V> void assignValue(
    final Map<K, Collection<V>> map, final K key, final V value) {

    Collection<V> values = map.get(key);
    if(values==null){
        values=new ArrayList<V>();
        map.put(key, values);
    }
    values.add(value);
}

Usage

And use it like this:

final Map<String, Collection<Double>> map =
    new HashMap<String, Collection<Double>>();
assignValue(map, "ABS", Double.valueOf(3434.34));
assignValue(map, "ABD", Double.valueOf(123.22));
assignValue(map, "ABD", Double.valueOf(123.22));
assignValue(map, "ABD", Double.valueOf(123.22));
assignValue(map, "ABD", Double.valueOf(123.22));

for(final Entry<String, Collection<Double>> entry : map.entrySet()){
    System.out.println(new StringBuilder()
        .append("Key: ")
        .append(entry.getKey())
        .append(", count: ")
        .append(entry.getValue().size())
        .append(", values: ")
        .append(entry.getValue())
        .toString());
}

Output

Key: ABD, count: 4, values: [123.22, 123.22, 123.22, 123.22]
Key: ABS, count: 1, values: [3434.34]


HashMap is-a Map which cannot contain duplicate keys. If duplicate keys are allowed, which value will you expect to return when calling map.get(duplicateKey)?


Have a read through this, particularly the 'put' method which says:

Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.

The way you've structured your code means that the HashMap will only ever contain two key/value pairs.

public class hash {

public static void main(String[] args) {

    HashMap hashMap = new HashMap();
    hashMap.put("ABS", new Double(3434.34)); // "ABS" key created. 1 object in map.
    hashMap.put("ABD", new Double(123.22)); // "ABD" key created. 2 objects in map.
    hashMap.put("ABD", new Double(123.22)); // "ABD" reference will point to new value.
    hashMap.put("ABD", new Double(123.22)); // .. and again
    hashMap.put("ABD", new Double(123.22)); // .. and again

So while you've written five put lines, only two objects will ever be placed in the HashMap.

You can use

hashMap.size()

to return an int value of how many key/value pairs are in 'hashMap'.


so you need to have Double+Count as value then and use it instead of Double, as a exercise make it extend java.lang.Number :)

Class DoubleCount{
  final double value;
  private int count;
  DoubleCount(double value){
    this.value = value;
  }
  public int getCount(){
    return count;
  } 

  public double getValue(){
   return value;
  }

  public int incCount(){
    return ++count;
  } 
}


size = hashMap.size(); then use for loop to iterate

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜