简体   繁体   English

无法将java.lang.Long强制转换为java.lang.Integer

[英]java.lang.Long cannot be cast to java.lang.Integer

I want to read a map map from the Database (Firebase),and i compare it to another map motcle , if i find a duplicate key i increment the value of the key in map ,I am having a ClassCastException. 我想从数据库(Firebase)中读取一个地图map ,并将其与另一个地图motcle进行比较,如果我发现重复的钥匙,我会增加map钥匙的值,那么我就遇到了ClassCastException。 the code: 编码:

public void addMotcle(final Map motcle, String Userid)
{
    mDatabase = FirebaseDatabase.getInstance().getReference("User");
    DatabaseReference user = mDatabase.child(Userid);
    final DatabaseReference keylist = user.child("motcle");
    keylist.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if(dataSnapshot.exists())
            {   
                //I tried to put the map in another map and change it but 
                //It is still the same error
                Map<String,Integer> map = (Map) dataSnapshot.getValue();
                Map<String,Integer>Hmap= new HashMap<>();
                Hmap.putAll(map);
                //if there is duplicate we incremente les mots clé
                for (Object key : motcle.keySet()) {
                    if (Hmap.containsKey(key)) {
                        Hmap.put((String)key,((Integer) Hmap.get(key))+1);
                    }
                    else
                    {
                        Hmap.put((String) key,1);
                    }
                }
               keylist.setValue(Hmap);

            }
            else
            {
             keylist.setValue(motcle);

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });



}

Here is the Value that i want to write on in Firebase 这是我要在Firebase中写的值 数据

the error is in the line map.put((String)key,map.get(key)+1); 错误在map.put((String)key,map.get(key)+1);

why can't i write an Integer in the value if i declared map as Map<String,Integer> ? 如果我将map声明为Map<String,Integer>为什么不能在值中写一个Integer

When you do Map<String,Integer> map = (Map) dataSnapshot.getValue(); 当您执行Map<String,Integer> map = (Map) dataSnapshot.getValue(); , you guarantee that map is a Map , but not that it's a Map<String, Integer> . ,您可以保证mapMap ,但不能保证它是Map<String, Integer> Since generics are erased and not available at runtime, as long as dataSnapshot.getValue() is a Map of some kind, this cast will succeed. 由于泛型会被擦除并且在运行时不可用,只要dataSnapshot.getValue()是某种Map ,此转换就可以成功。

Because you've declared Hmap as a Map<String,Integer> , when you get a value from the map, it will try to cast it to Integer , but there is nothing actually guaranteeing ahead-of-time that it will be an Integer . 因为您已将Hmap声明为Map<String,Integer> ,所以当您从地图中获取值时,它将尝试将其HmapInteger ,但实际上并没有任何保证能提前保证它将成为Integer。 As a result, if the value is actually a Long , it will try to cast that Long to an Integer and fail (because Long does not extend Integer , and widening operations don't work on boxed types). 结果,如果该值实际上是Long ,它将尝试将Long转换为Integer并失败(因为Long不会扩展Integer ,并且扩展操作不适用于盒装类型)。

You need to find out what the actual types of the values of dataSnapshot.getValue() are, and cast it to the correct type of Map to avoid this error. 您需要找出dataSnapshot.getValue()值的实际类型是什么,并将其dataSnapshot.getValue()转换为正确的Map类型,以避免此错误。

See here for more details: Weird problem about Java Generics operation 有关更多详细信息,请参见此处: 关于Java泛型操作的奇怪问题

这是因为键列表是一个最终变量,您无法更改其值,因此可以像这样初始化它:-

DatabaseReference keylist = user.child("motcle");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.Integer 不能转换为 java.lang.Long - java.lang.Integer cannot be cast to java.lang.Long java.lang.ClassCastException:java.lang.Long 不能在 java 1.6 中转换为 java.lang.Integer - java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer in java 1.6 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long in hibernate - java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long in hibernate java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long - java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long 无法将java.lang.Long强制转换为java.lang.Integer JPA GAE和枚举 - java.lang.Long cannot be cast to java.lang.Integer JPA GAE and enumeration fastjson java.lang.Integer 不能转换为 java.lang.Long - fastjson java.lang.Integer cannot be cast to java.lang.Long ClassCastException java.lang.Integer 无法在获取共享首选项值时强制转换为 java.lang.Long - ClassCastException java.lang.Integer cannot be cast to java.lang.Long on getting the Shared Preference value 将方法传递给Controller:java.lang.Long不能强制转换为java.lang.Integer - Passing methods to Controller: java.lang.Long cannot be cast to java.lang.Integer Java 中的 JSON 加载:java.lang.ClassCastException:java.lang.Long 不能转换为 java.lang.Integer - JSON Loading in Java: java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer SDN4 java.lang.ClassCastException:使用AttributeConverter时无法将java.lang.Integer强制转换为java.lang.Long - SDN4 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long when using AttributeConverter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM