java - Guava 重复键异常 IllegalArgumentException

标签 java guava

这里是一个快速剪断。

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);

当我运行它时,我得到了这个异常。

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)

看起来它在某处交换键和值。使用 guava-14.0-rc1.jar

最佳答案

它完全按照您的要求执行 - 按与键关联的值对条目进行排序。鉴于您已调用变量 valueComparator,我假设您知道该部分。

所以根据您使用的比较器,您的两个键是相等的 - 因此它抛出了记录的异常:

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

也许您想按值排序,然后按键排序,以提供唯一性?

Ordering<String> valueThenKeyComparator = 
    Ordering.from(String.CASE_INSENSITIVE_ORDER)
            .onResultOf(Functions.forMap(map))
            .compound(Ordering.<String> natural());

关于java - Guava 重复键异常 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15479922/

相关文章:

Java - 使用同步方法进行多线程练习

java - 如何使用 text() 函数获取元素的 xpath,其中文本具有撇号

java - Maven 未安装依赖项

java - WebDriver 和 FirefoxDriver 的区别

java - 如何配置liquibase maven插件为oracle生成sql输出

Java:如何创建一个返回大数据文件每一行的迭代器(columnList)的通用方法

java - Maps.newHashMap 与新 HashMap 返回的 HashMap

java - Guava 是否有将可迭代对象转换为唯一类型映射的方法?

java - 如何根据值对Hashmultimap进行排序?

java - 为什么 Google Collections 不像 Apache Collections 那样支持 MultiKeyMap?