Java.util.Map and null key

Hi,
For information, according to the implementation, it is possible to put and get a null key when using a map.
The null key can be added and retrieved when using a HashMap, an exception is thrown for a TreeMap.

The documentation for java.util.Map states:
Some map implementations have restrictions on the keys and values they may contain. For example, some implementations prohibit null keys and values, and some have restrictions on the types of their keys. Attempting to insert an ineligible key or value throws an unchecked exception, typically NullPointerException or ClassCastException.The documentation for java.util.TreeMap.put() states:
Throws:
NullPointerException - if the specified key is null and this map uses natural ordering, or its comparator does not permit null keysSo a Map generally may or may not support null keys and/or values and the decision is up to the concrete implementation. TreeMap states that it doesn't support null keys, unless a Comparator is used that permits null keys.

Similar Messages

  • Java.util.Map and  java.util.HashMap samples

    Hi.
    Please, I need some code samples of java.util.Map and java.util.HashMap interfaces. I have problems to retreive objects in the map.
    Cheers,
    Cata

    Try the tutorial:
    http://java.sun.com/docs/books/tutorial/collections/index.html

  • JPA - Columns to java.util.Map

    I need to map the columns of a DB table into a java.util.Map object.
    Any idea about how to implement this feature?
    These are the columns of my table:
       | ID | Value01 | Value02 | Value03 | ... | ValueN |I'd like to obtain a bean like this:
    public class MyBean {
      private String ID;
      private Map values;
    }I thought I could extend java.util.Map and redefine the get(Object) and put(Object, Object) methods
    (I did something like this in java 1.4) but I can't figure it out in 1.5 with JPA

    >
    hi i have the following method,when i click my button am geting this error java.lang.String cannot be cast to java.util.Map
    >
    And that is correct - you can't do that cast. Which is what this code is trying to do.
    >
    (Map)actionEvent.getComponent().getAttributes().get("test")
    >
    Ask yourself what object is supposed to be the 'Map' in that line of code?
    Is it 'actionEvent', 'getComponent()' or 'getAttributes()'?
    My guess is that it is the 'getAttributes' that returns a map. Which means you need to cast
    actionEvent.getComponent().getAttributes()as a Map. Which means you need to put that entire thing in parentheses so that the cast is performed on it. And then you have to put THAT entire thing in parentheses so you can call the 'get' method on it.
    So this is the 'Map'
    (Map) (actionEvent.getComponent().getAttributes())And this calls the Maps 'get' method
    ( (Map) (actionEvent.getComponent().getAttributes()) ).get("test")If you broke the line into pieces and stored each piece in its own variable you would see it. These object types may not be right but should give you the idea.
    //( (Map) (actionEvent.getComponent().getAttributes()).get("test") ).get("test")
    MyComponent myComponent = (MyComponent) actionEvent.getComponent();
    Map myMap = (Map) myComponent.getAttributes();
    myMap.get("test");

  • Weird exception: Cannot instantiate non-persistent class: java.util.Map

    java.lang.UnsupportedOperationException: Cannot instantiate non-persistent class: java.util.Map
         at com.sleepycat.persist.impl.NonPersistentFormat.newInstance(NonPersistentFormat.java:45)
         at com.sleepycat.persist.impl.PersistEntityBinding.readEntity(PersistEntityBinding.java:89)
         at com.sleepycat.persist.impl.PersistEntityBinding.entryToObject(PersistEntityBinding.java:61)
         at com.sleepycat.persist.PrimaryIndex.put(PrimaryIndex.java:338)
         at com.sleepycat.persist.PrimaryIndex.put(PrimaryIndex.java:299)
         at com.xx.support.dbd.IdentityDataAccessor.insert(IdentityDataAccessor.java:33)
         at com.xx.support.dbd.BerkeleyDBAccountStorage.saveUser(BerkeleyDBAccountStorage.java:95)
         at com.xx.support.bdb.BerkeleyDBAccountStorageTests.initBerkeleyDBData(BerkeleyDBAccountStorageTests.java:38)
         at com.xx.support.bdb.BerkeleyDBAccountStorageTests.setUp(BerkeleyDBAccountStorageTests.java:28)
         at junit.framework.TestCase.runBare(TestCase.java:125)
         at junit.framework.TestResult$1.protect(TestResult.java:106)
         at junit.framework.TestResult.runProtected(TestResult.java:124)
         at junit.framework.TestResult.run(TestResult.java:109)
         at junit.framework.TestCase.run(TestCase.java:118)
         at junit.framework.TestSuite.runTest(TestSuite.java:208)
         at junit.framework.TestSuite.run(TestSuite.java:203)
    What's the root cause of this exception?

    I wrote a small test using the classes you included
    in your message and I am able to retrieve the user by
    key, as in the code above. So I'm not sure what
    you're doing that is causing the problem. Please
    send a small test that reproduces the problem.Oops, I forgot to include the source for the test I wrote. Here it is.
    import java.io.File;
    import java.util.HashMap;
    import java.util.Map;
    import com.sleepycat.je.DatabaseException;
    import com.sleepycat.je.Environment;
    import com.sleepycat.je.EnvironmentConfig;
    import com.sleepycat.persist.EntityStore;
    import com.sleepycat.persist.PrimaryIndex;
    import com.sleepycat.persist.StoreConfig;
    import com.sleepycat.persist.model.Entity;
    import com.sleepycat.persist.model.Persistent;
    import com.sleepycat.persist.model.PrimaryKey;
    public class Test {
        @Persistent
        public static class SimplePrincipal {
            protected String name;
            public SimplePrincipal(String username) {
                this.name = name;
            public SimplePrincipal() {}
        @Entity
        public static class SimpleUser extends SimplePrincipal {
            @PrimaryKey
            private String key;
            private Map properties;
            public SimpleUser() {
                super();
                this.properties = new HashMap();
            public SimpleUser(String username) {
                super(username);
                this.properties = new HashMap();
            public void setKey(String key){
                this.key = key;
            public void addPropertity(String name, String value) {
                this.properties.put(name, value);
            @Override
            public String toString() {
                return "[SimpleUser key: " + key + " name: " + name + ']';
        private Environment env;
        private EntityStore store;
        private PrimaryIndex<String, SimpleUser> primaryIndex;
        private void open()
            throws DatabaseException {
            EnvironmentConfig envConfig = new EnvironmentConfig();
            envConfig.setAllowCreate(true);
            envConfig.setTransactional(true);
            env = new Environment(new File("./data"), envConfig);
            StoreConfig storeConfig = new StoreConfig();
            storeConfig.setAllowCreate(true);
            storeConfig.setTransactional(true);
            store = new EntityStore(env, "test", storeConfig);
            primaryIndex = store.getPrimaryIndex(String.class, SimpleUser.class);
        private void close()
            throws DatabaseException {
            store.close();
            env.close();
        private void execute()
            throws DatabaseException {
            SimpleUser user = new SimpleUser("test");
            user.setKey("testkey");
            primaryIndex.put(user);
            user = primaryIndex.get("testkey");
            System.out.println(user);
        public static void main(String[] args)
            throws DatabaseException {
            Test test = new Test();
            test.open();
            test.execute();
            test.close();
    }Mark

  • Servlet java.util.Map Help

    It's been a while since I have written any code and I am having an issue getting back into it.
    I am currently working on creating servlet to pass info from all my forms to my business logic. However, I am having an issue with getting the data. This may be a topic for a different forum, but I figured I would ask here.
    I tested out the servlet with the getParameter() method, and I can get the values. However, when I try to use the java.util.Map, I get blank values.
    I think the issue may be that I forgot how to use the Map object. Here is my code:
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Servlet CalCartController</title>");
    out.println("</head>");
    out.println("<body>");
    for (int i = 0; i < formParamsMap.size(); i++) {
    Map.Entry entry = (Map.Entry) keyValuePairs.next();
    String key = (String)entry.getKey();
    String value = (String)entry.getValue();
    out.println(value);
    out.println("<h1>Servlet CalCartController </h1>");
    out.println("</body>");
    out.println("</html>");

    hacktorious wrote:
    OK, so if I replace the loop with the following:
    String firstName = (String)formParamsMap.get("firstName");
    out.println(firstName);
    this gives a blank result.If firstName is an actual parameter, it's probably throwing an exception, and you're swallowing it or otherwise not seeing it. I would expect a ClassCastException if the map contained "firstName".

  • Error: java.util.map can not be inherited with different arguments

    Hi,
    I am getting following error while building the source code.
    C:\venus\src\com\martquest\messaging\msgio\MqMessageIOObjectCarrier.java:36: java.util.Map cannot be inherited with different arguments: <> and <java.lang.Object,java.lang.Object>
    [javac] public class MqMessageIOObjectCarrier extends Properties implements IMqMessageIOObjectCarrier
    What should I do to resolve this issue?
    Thanks
    Prachi

    Hi,
    I am getting following error while building the
    source code.whose source code? If it's yours, you'd better look at where you are defining and using Map. The error code tells you exactly what's wrong.

  • Java.util.map in a jar?

    My applet works on Netscape 6.1 presumably because the java.util.map class has capabilities that I'm using in my applet that isn't in other versions of java.util.map. Since I want to make my applet cross platform, is there any way to include java.util.map in my jar file so that people can use that version instead? Thanks.
    Denise

    Denise,
    I think that will break your license agreement with Sun, so you probably don't want to do that, right? ;)
    I think you should try to figure out what the exact cause of your problem is. Off hand, it almost sounds like your applet uses v1.3 api, but your still using the old <Applet> html tag and not the new <Object> tag. Is this right?
    -Ron

  • Message Mapping - java.util.Map

    Hi There
    I would like to know if there is any way, using java.util.Map map; to get the "data type" field name in a UDF? Passing a constant with the field name would not be practical.. I'm looking for a object oriented process to use in all my mappings.
    Also where could I look for the methods I can use in the map trace object?
    Regards,
    Jan

    But I'm still missing the field name..
    Let say I have a message mapping going from Field ACB to Field QAZ and a UDF called CheckLen.
    In my UDF I'm looking at the incoming field and if it's length is correct. if not I'm writing an entry into a table stating there was an incorrect field but what I can't get is the the field name so I can write to the table "Invalid length on field ACB" The only way to get this field name value is by passing a constant of "ACB" to the UDF and using that. But we have hundreds of MMs so implementing this would be much more difficult than a OOP way..

  • Java.util.Map type return in webservice method

    Wonder, how we can return a java.util.Map type from a webservice deployed on a
    weblogic 7.x environment..
    any input is appreciated.
    -Girish

    Thanks Michael. it helps.
    -Girish
    "Michael Wooten" <[email protected]> wrote:
    >
    Hi Girish,
    By definition, a Java object used as a input parameter (or return type)
    of a web
    service operation, must has a no-arg constructor in order for a Java-based
    Web
    Service Stack to serialize/deserialize it to/from XML. java.util.Map
    is an interface,
    so it does not meet this requirement :-)
    If your web service operation is just trying to return (or accept) a
    hash map
    of "arbitrary complex types", I recommend that you consider switching
    this to
    be an "array of arrays of specific complex types". The reason I say this
    is because,
    it allows even non-Java consumers to look at the <schema> elements in
    your web
    services' WSDL, and figure out all the possible "arbitrary complex types"
    that
    might be in this array. If you envision the consumer getting a box, where
    it can
    literally contain anything, you can probably see how much more code they
    would
    need to write, than if they knew the the box contained a box of these,
    and a box
    of these, and so on, and so on.
    Basically, all the "old school" type-safety rules still apply to web
    services
    computing. If you want web services with descent performance metrics,
    you're probably
    going to want to keep a lot of the "old school, distributed computing"
    practices
    in mind, when you design your stuff :-)
    That said, WLS 7.0 SP1 does not currently have a "built-in" HashMap codec.
    But,
    you could create one using the information at:
    http://edocs.bea.com/wls/docs70/webserv/customdata.html#1054435
    Regards,
    Mike Wooten
    "girish" <[email protected]> wrote:
    Wonder, how we can return a java.util.Map type from a webservice deployed
    on a
    weblogic 7.x environment..
    any input is appreciated.
    -Girish

  • BEA error: deleteTaskAttributes(int,java.util.Map)

    Hello,
    I have installed OIM 9100 on BEA with an Oracle Database.
    I got the following error message after starting OIM:
    <08.07.2008 11.07 Uhr CEST> <Warning> <EJB> <BEA-012034> <The Remote interface method: 'public abstract void com.thortec
    h.xl.scheduler.interfaces.SchedulerController.deleteTaskAttributes(int,java.util.Map) throws com.thortech.xl.scheduler.e
    xception.SchedulerGenericException,java.rmi.RemoteException' in EJB 'SchedulerController' contains a parameter of type:
    'java.util.Map' which is not Serializable. Though the EJB 'SchedulerController' has call-by-reference set to false, this
    parameter is not Serializable and hence will be passed by reference. A parameter can be passed using call-by-value only
    if the parameter type is Serializable.>
    Can anybody help me or tell me what it means?
    Thank you,
    Matthias

    BEA Weblogic normally throws a ton of these errors as it seems like OIM's ideas on what should be passed by reference and what should be passed by value does not play well with Weblogic.
    I have been told to ignore these warnings.
    Good luck
    /M

  • Compile error import java.util.map$entry

    Hi,
    I am trying to compile code which imports the following package
    import java.util.Map$Entry.
    The error thrown up is : cant resolve symbol Map$Entry. Why is there a $ in the package import path and is there some configuration required to compile the file.
    I am not allowed to change the code. Does anyone have an idea on how this problem can be solved.
    Thanks and regards
    Kumar Vellal

    Btw java docs for the interface are available at:
    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Map.Entry.html
    Changing the import should probably help compile the code, but without changing the import .. need to check. This is just from what i remember now. Lets see if anyone else comments on this.

  • Exposing java.util.Map data as RequiredModelMBean

    I'm trying to expose java.util.Map data but I can't quite figure how.
    I'd like the management interface to look like it has an attribute that is actually the key of the map.
    For example, calling:
    mBeanServer.getAttribute(new ObjectName("nomojomofo:type=mapBean"), "fubar");
    would get its data from the target object as:
    aMap.get("fubar");
    Edited by: nomojomofo on Oct 9, 2007 5:31 PM

    You can write a DynamicMBean for that:
    http://weblogs.java.net/blog/emcmanus/archive/2006/11/a_real_example.html
    However you should be aware that most JMX consoles do not expect that the MBeanInfo of a given MBean can change over time.
    If the content of your Map is fixed - you won't have any problem.
    If it can change over time, you might consider exposing the Map as a single attribute,
    for instance, as a CompositeData.
    You might also be interested in this article:
    http://blogs.sun.com/jmxetc/entry/dynamicmbeans%2C_modelmbeans%2C_and_pojos...
    Hope this helps,
    -- daniel
    http://blogs.sun.com/jmxetc

  • Why not Deprecate java.util.Date and java.util.Calendar

    With the introduction of java.time, why did you not flag java.util.Date and java.util.Calendar. These classes have been a bane to every Java developer and should never be used again with the introduction of Java 1.8.

    Adding the @Deprecated annotation would only just provide a warning about an old API and recommendation to the developer(s) to no longer use it. Doing so would not break any existing library out there; in fact quite a number of constructors and methods on the Date class have already been flagged deprecated.
    The new java.time package is far superior to Date/Calendar.

  • Java.util.Date and java.sql.Date

    when i am trying to displaying date in my jsp page errors occurs saying that java.util.date and java.sql.date are matching..
    i have some sql statements which i need to execute in the page.so i cant delete that import sql statement.
    so what is the solution for this.
    i want to display the date as 19th april 2003....in the jsp page.
    thank u

    You should use full names.
    java.util.Date date;
    java.sql.Date sdate;
    ..And it may be a good idea to delete one of the two "import" declaration of packages.

  • Java.util.Map to as object

    Hi,
    I want to get java.util.Map object returned from server-side
    on the client-side. I don't know which flex data-type should I use
    for that. I tried Array but it dint help. Someone plz help...
    Thanks in advance,
    Cheree

    You can use a for-in ActionScript loop to examine any
    ActionScript Object:
    for(var i:String in someObject) {
    trace( i+" = "+someObject
    Within the loop, i assumes the name of all the public
    properties in the given object.

Maybe you are looking for

  • Can't click on Spotlight menulet

    For some reason, my menu bar Spotlight icon is dead. Does anyone have any ideas??

  • MM Stock Related Table

    Hi tell me about some  stock related table . I specially want to know that after posting that is  after executing t code MI10 which database table get updated. I also want to know from where Tcode MB5B fetch the data

  • Cannot mount FAT32 Hard Drive in OX 10.5.8

    I have a Mac mini running OSX 10.5.8  I also have a 2TB external hard drive, FAT32 that I use on various Macs and Windows 7 PCs I use the Mac Mini to back up the external drive to a Drobo. Today I went to back up the external drive and it would not m

  • ADF Faces table and link to detail

    Hi all, I am trying to create a link to a detail page from an ADF table. My problem is, that the action is invoked as often as there are entries in my table. Interesting enough the param "objektId" each time contains the same primary key from the lin

  • MacBook Pro Retina hangs while opening mounted share or USB drive

    I have a Macbook Pro Retina that I just updated to 10.8.2 over the past weekend.  Now, when I try to open a mounted SMB share or USB drive, the system hangs completely!  I had the system set to auto-reconnect my network shares, so when this started h