Python到Java的转换及面临的问题

2024-10-06 10:21:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我将以下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中一样。如果有人能帮我,我将不胜感激。你知道吗


Tags: theabstractmapvaluestatstypeintegerpublic
2条回答

我修改了

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
   }

对我有用。你知道吗

我看到的是您在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);

更新
很明显,您需要覆盖刚刚覆盖的映射中的所有方法。请克服所有的方法。这样就可以解决您的问题
采用以下方法超越

remove
put
keySet
values
containsKey
containsValue
entrySet
putAll
isEmpty

请参阅Method Summary Section以查看要重写的方法

建议 如果您不想超越MAP中的所有方法(因为这确实是一种痛苦),那么您可以使用implements MAP,而不是extends HashMap。有了它,你只需要超越你想要的方法。与您的情况一样,获取和大小

相关问题 更多 >