将Python转换为Java并面临一些问题

时间:2013-05-09 04:36:57

标签: java python hashmap

我正在将以下Python代码转换为Java代码,如下所示:

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

这是我到目前为止编写的Java代码:

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
}

但是在以下行收到错误:

Java代码:

DefaultingMap<Integer,Integer> defaultvalue = new DefaultingMap<Integer,Integer>(); 错误是:构造函数DefaultingMap()未定义

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()

任何人都可以解释原因吗?

基本上我试图设置一个默认值,就像setdefault在python中工作一样。如果有人能在这里帮助我,我将不胜感激。

2 个答案:

答案 0 :(得分: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

请参阅Method Summary Section以查看要覆盖的方法

<强> SUGGESTION 如果您不希望覆盖 MAP 中的所有方法(因为它真的很痛苦),那么您可以implements MAP而不是extends HashMap。有了这个,你只需要覆盖你想要的方法。在您的情况下,获取大小

答案 1 :(得分: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
   }

对我有用。