简体   繁体   中英

Guava map storing class instance as key and int value

I am trying to use Guava's collections to create a map that will store a class instance as the key and an Integer number as the value. So something like this:

Key - Value

FooOne - 1
FooTwo - 2
FooThree - 3
FooFour - 4

I have this in the code right now:

private ClassToInstanceMap<Object> classRanking = MutableClassToInstanceMap.create();
classRanking.put(FooOne.class, 0);
classRanking.put(FooTwo.class, 1);
classRanking.put(FooThree.class, 2);
classRanking.put(FooFour.class, 3);

But that throws an error when run:

java.lang.ClassCastException: Cannot cast java.lang.Integer to com.jer.abc.test.FooOne
    at java.lang.Class.cast(Unknown Source)
    at com.google.common.collect.MutableClassToInstanceMap.cast(MutableClassToInstanceMap.java:82)
    at com.google.common.collect.MutableClassToInstanceMap.access$000(MutableClassToInstanceMap.java:36)
    at com.google.common.collect.MutableClassToInstanceMap$1.checkKeyValue(MutableClassToInstanceMap.java:67)
    at com.google.common.collect.MutableClassToInstanceMap$1.checkKeyValue(MutableClassToInstanceMap.java:64)
    at com.google.common.collect.MapConstraints$ConstrainedMap.put(MapConstraints.java:313)
    at com.fmr.gps.web.support.PortfolioInsightsSupport.populateRankingMap(PortfolioInsightsSupport.java:33)
    at com.fmr.gps.web.support.PortfolioInsightsSupport.<init>(PortfolioInsightsSupport.java:29)
    at com.fmr.gps.web.PortfolioInsightsSupportTest.setUp(PortfolioInsightsSupportTest.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

So I was thinking about using a plain old java map but this thread tells me to do otherwise. So any ideas on how to fix this?

ClassToInstanceMap is for when you want to do stuff like classRanking.put(FooOne.class, new FooOne()) , ie the values will be instances of the key. But 0 is not an instance of FooOne , it's just an Integer . You just want a normal Map<Class<?>, Integer> , so use a HashMap or something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM