Results 1 to 3 of 3
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Rep Power

    Default Empty Map in Java !

    Hi ,

    I wanna create an empty map in java and then the default value to 0. Please let me know if below code is correct.

    Actually, I'm having a python code which I'm trying to convert into Java. For the sake of understanding I'm pasting Python code here

    Java Code:
    import random
    howMany = random.randint(0,1000)
    stats = {} // EMPTY DICTIONARY
    for i in range(howMany):
       value = random.randint(0,500)
       stats.setdefault(value,0)  // Setting the default value to 0 here
    for item in stats:
       if stats[item] > 1:
           print item
    In case of java if I do like this:

    Java Code:
    Map<String, Date> s = Collections.emptyMap();
    Then I'll have to mention String and Date in the diamond. How can I do for Empty Map?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Empty Map in Java !

    Well, I don't speak Python (hiss?) so I can't help you there (I make enough mistakes explaining Java). However, you realize that your empty map is immutable. So you can't set any elements to anything. I assumed you wanted to set some value to 0 but I am not certain.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Empty Map in Java !

    I gave you the answer in a previous thread already; Java doesn't have that 'default value' thing for collections; for a map, the mapping is present or not. If not, you have to assume that 'default value'. Reread your previous thread again.

    kind regards,

    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Jcombobox an empty row
    By sh4rif in forum New To Java
    Replies: 3
    Last Post: 06-06-2012, 11:58 PM
  2. Java Swing - Defining a empty panel for a SplitPane
    By supertreta in forum AWT / Swing
    Replies: 1
    Last Post: 01-02-2012, 06:45 PM
  3. Empty JFrame
    By Wednesday Bass in forum AWT / Swing
    Replies: 2
    Last Post: 03-25-2011, 08:30 PM
  4. Replies: 0
    Last Post: 07-12-2010, 11:57 AM
  5. Empty ResultSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 09:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts