Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guava Duplicate keys exception IllegalArgumentException

Tags:

java

guava

Here is a quick snipped.

Map<String, String> map = new HashMap<String, String>();
map.put("1", "1");
map.put("2", "1");          
Ordering<String> valueComparator = Ordering.from(String.CASE_INSENSITIVE_ORDER).onResultOf(Functions.forMap(map));  
Map<String, String> sortedMap = ImmutableSortedMap.copyOf(map, valueComparator);

When i run it i get this exception.

java.lang.IllegalArgumentException: Duplicate keys in mappings 2=1 and 1=1
    at com.google.common.collect.ImmutableSortedMap.validateEntries(ImmutableSortedMap.java:304)
    at com.google.common.collect.ImmutableSortedMap.copyOfInternal(ImmutableSortedMap.java:281)
    at com.google.common.collect.ImmutableSortedMap.copyOf(ImmutableSortedMap.java:220)
    at com.dbs.datasource.TestBeans.test(TestBeans.java:90)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:243)
    at junit.framework.TestSuite.run(TestSuite.java:238)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

It looks like it swapping key and value somewhere. Using guava-14.0-rc1.jar

like image 966
Pintac Avatar asked Dec 21 '22 08:12

Pintac


1 Answers

It's doing exactly what you're asking it to - ordering the entries by the value associated with the key. I assume you're aware of that part, given that you've called the variable valueComparator.

So your two keys are equal according to the comparator you're using - and hence it's throwing the documented exception:

IllegalArgumentException - if any two keys are equal according to the comparator

Perhaps you want to order by value and then by key, to provide uniqueness?

Ordering<String> valueThenKeyComparator = 
    Ordering.from(String.CASE_INSENSITIVE_ORDER)
            .onResultOf(Functions.forMap(map))
            .compound(Ordering.<String> natural());
like image 93
Jon Skeet Avatar answered Jan 20 '23 16:01

Jon Skeet