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.