0

I'm converting the following Python Code into Java code as follows:

import random
howMany = random.randint(0,1000)
stats = {}
for i in range(howMany):
value = random.randint(0,500)
stats.setdefault(value,0)
stats[value]+=1
for item in stats:
if stats[item] > 1:
    print item

Here is the Java code I have written so far :

import java.util.Random;
import java.util.*;

public class PythonToJava 
{
    public static void main(String[] args) 
    {
        Random rm = new Random();
        int i = rm.nextInt(1000);
        HashMap<Integer,Integer> stats = new HashMap<Integer,Integer>();
        System.out.println("Random Number Generated is: " + i);
        for (int j = 0; j<i; j++)
        {
            int value = rm.nextInt(500);
            System.out.println("The value of VALUE is " + value);
            DefaultingMap<Integer,Integer> defaultvalue = new DefaultingMap<Integer,Integer>();
            defaultvalue.put(value,0);
        }
    }
}

public class DefaultingMap<Integer, Integer> implements Map<Integer, Integer>
{
    private final Map<Integer, Integer> map;
    private final Integer defaultValue;
    public DefaultingMap(Map<Integer, Integer> map, Integer defaultValue)
    {
        this.map = map;
        this.defaultValue = defaultValue;
    }

    @Override public Integer get(Object key)
    {
        Integer ret = map.get(key);
        if (ret == null)
        {
            ret = defaultValue;
        }
        return ret;
    }

    @Override public int size()
    {
        return map.size();
    }
    // etc
}

But getting error at the following line:

Java Code:

DefaultingMap<Integer,Integer> defaultvalue = new DefaultingMap<Integer,Integer>(); Error is : The constructor DefaultingMap() is undefined

and at public class DefaultingMap<Integer, Integer> implements Map<Integer, Integer>

Error is : Multiple markers at this line
    - The type DefaultingMap<Integer,Integer> must implement the inherited abstract method 
     Map<Integer,Integer>.remove(Object)
    - The type parameter Integer is hiding the type Integer
    - The type DefaultingMap<Integer,Integer> must implement the inherited abstract method 
     Map<Integer,Integer>.put(Integer, Integer)
    - The type DefaultingMap<Integer,Integer> must implement the inherited abstract method 
     Map<Integer,Integer>.keySet()
    - The type DefaultingMap<Integer,Integer> must implement the inherited abstract method 
     Map<Integer,Integer>.values()
    - The type DefaultingMap<Integer,Integer> must implement the inherited abstract method 
     Map<Integer,Integer>.containsKey(Object)
    - The type parameter Integer is hiding the type Integer
    - The type DefaultingMap<Integer,Integer> must implement the inherited abstract method 
     Map<Integer,Integer>.containsValue(Object)
    - Duplicate type parameter Integer
    - The type DefaultingMap<Integer,Integer> must implement the inherited abstract method 
     Map<Integer,Integer>.entrySet()
    - The type DefaultingMap<Integer,Integer> must implement the inherited abstract method 
     Map<Integer,Integer>.putAll(Map<? extends Integer,? extends Integer>)
    - The type DefaultingMap<Integer,Integer> must implement the inherited abstract method 
     Map<Integer,Integer>.isEmpty()
    - The type DefaultingMap<Integer,Integer> must implement the inherited abstract method 
     Map<Integer,Integer>.clear()

Can anyone explain why?

Basically I'm trying to set a default value somehow just like setdefault thing works in python. I would appreciate if someone can help me here.

4

2 回答 2

2

我看到的是您有参数化的构造函数,DefaultingMap并且缺少默认构造函数。请添加默认构造函数

public DefaultingMap()
{
  // do your things
}

否则更改此调用

DefaultingMap<Integer,Integer> defaultvalue = new DefaultingMap<Integer,Integer>();
defaultvalue.put(value,0);

对此

DefaultingMap<Integer,Integer> defaultvalue = new DefaultingMap<Integer,Integer>(value,0);

更新
很明显,您需要覆盖刚刚覆盖的 MAP 中的所有方法。请覆盖所有方法。然后这将解决您的问题
以下方法覆盖

remove
put
keySet
values
containsKey
containsValue
entrySet
putAll
isEmpty

请参阅方法摘要部分以查看要覆盖的方法

建议 如果您不想覆盖MAP中的所有方法(因为它真的很痛苦),那么implements MAP您可以拥有extends HashMap. 有了这个,你只需要覆盖你想要的方法。在你的情况下获取大小

于 2013-05-09T04:53:05.920 回答
0

我修改了

DefaultingMap<Integer,Integer> defaultvalue = new DefaultingMap<Integer,Integer>();

DefaultingMap<Integer,Integer> defaultvalue = new DefaultingMap<Integer,Integer>(new HashMap<Integer, Integer>(),0);

public class DefaultingMap<Integer, Integer> implements Map<Integer, Integer>
{
    private final Map<Integer, Integer> map;
    private final Integer defaultValue;

    public DefaultingMap(Map<Integer, Integer> map, Integer defaultValue)
    {
        this.map = map;
        this.defaultValue = defaultValue;
    }

    @Override public Integer get(Object key)
    {
        Integer ret = map.get(key);
        if (ret == null)
        {
            ret = defaultValue;
        }
        return ret;
    }

变成了

class DefaultingMap<K, V> implements Map<K,V>{ //remove public and use proper generic parameters

   private final Map<K, V> map; // is now generic
   private final V defaultValue; // must be of type V

   public DefaultingMap(Map<K, V> map, V defaultValue) throws IllegalArgumentException
   {
   /* because we have defined this constructor DefaultingMap() will need to be 
      defined if we need/want it. */
       if (map == null){
           throw new IllegalArgumentException("provided map cannot be null");
       }
       this.map = map;
       this.defaultValue = defaultValue;
   }

   public V get(Object key){

       V ret = map.get(key);
       if (ret == null){
           ret = defaultValue;
       }
       return ret;
   }

如果您不确定,您需要实现 Map 接口中的所有方法

   @Override
   public V put(K key, V value) {
       return map.put(key,value); // delegate to map
   }

这对我有用。

于 2013-05-09T05:14:46.370 回答