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.

19
  • What does the error say? Commented May 9, 2013 at 4:38
  • 1
    DefaultingMap does not appear to be a part of the standard JDK. You may need to add additional libraries or simply use another class. Commented May 9, 2013 at 4:38
  • 1
    Is DefaultingMap in the same file as PythonToJava? You cannot define two public classes in the same file. Commented May 9, 2013 at 4:45
  • 1
    see the ans from @asif. Commented May 9, 2013 at 4:56
  • 1
    Also note that Python's randint(0,1000) equates to Java nextInt(1001), and same with the 500. Java's range is exclusive, Pythons is inclusive. Commented May 9, 2013 at 5:03

2 Answers 2

2

What I see is You have parameterized constructor in DefaultingMap and default constructor is missing. Kindly Add default constructor

public DefaultingMap()
{
  // do your things
}

Otherwise change this call

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

to this

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

UPDATED
It is clear that you need to override all the methods from the MAP you have just overriden few. Kindly over-ride all the methods. Then this will resolve your issue
Following methods to over-ride

remove
put
keySet
values
containsKey
containsValue
entrySet
putAll
isEmpty

Refer Method Summary Section to see the methods to override

SUGGESTION If you do not wish to over-ride all the methods from MAP (as its really a pain) then rather than implements MAP you can have extends HashMap. With this you just have to over-ride the methods which you would like to. As in your case get and size

Sign up to request clarification or add additional context in comments.

8 Comments

Mentioning public DefaultingMap() { // do your things } did work but the second error still persists which is "Multiple Markers at this line.
Can you tell us what is the other errors? Firstly, separate both the class into different files (As only one public class is permitted per file)
It is clear that you need to override all the methods from the MAP you have just overriden few. Kindly over-ride all the methods. Then this will resolve your issue
Not sure which methods I need to override again. Could you please mention it here?
Thanks for your answer, I would rather extend HashMap here and after doing that also nothing changed. What could be the reason? Here is what I changed: class DefaultingMap<Integer, Integer> extends HashMap<Integer, Integer>
|
0

i modified

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

to

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

Also

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;
    }

became

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;
   }

You need to implement all the methods in the Map interface do them this way if you are unsure

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

that worked for me.

1 Comment

Hi @BevynQ , Thanks for your answer. Could you please update your code for extending a class HashMap instead? I'm preferring to extend a class instead of overriding all the methods of Interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.