草庐IT

some_collection

全部标签

java - 使用 Google Collections 创建弱多图

是否有与MultiMaps的漂亮MapMaker等效的工具?目前我这样创建缓存:publicstaticMap>personCache=newMapMaker().weakKeys().makeMap();MultiMap的全部要点是避免嵌套的列表值。有什么方法可以用弱键构造多映射? 最佳答案 不幸的是没有。然而。您可以在我们的问题数据库中提交MultimapMaker功能请求吗?http://google-collections.googlecode.com 关于java-使用Goog

java - Lombok @Getter 和 Collections 的副本

在List字段上使用@Getter工作正常,但是在尝试升级到Java8时我遇到了ConcurrentModificationException,因为getter生成Lombok不执行字段的复制,如果您希望防止实例状态的外部修改,这是必不可少的。关于如何让Lombok在getter上复制Collection的任何想法,或者我只能自己编写吗? 最佳答案 来自@Getterand@Setter文档:Youcanannotateanyfieldwith@Getterand/or@Setter,toletlombokgeneratethede

java - Collection 内容从未在 Intellij IDEA 中更新警告

我创建了一个简单的Counter类:publicclassCounterextendsHashMap{publicCounter(){}publicvoidincrease(Tkey){put(key,getOrDefault(key,0l)+1);}}在我的代码中,我调用了increase()方法,然后使用Map方法访问数据,例如Countercounter=newCounter();for(Integeri:...somecollection...)counter.increase(i);Intellij使用警告颜色突出显示counter的声明(最后一段的第一行),工具提示消息显示

java - 了解 Java 中的 Collections.reverseOrder() 方法

考虑sort的重载定义之一方法来自Array类:publicstaticvoidsort(T[]a,Comparatorc)逆序排列数组的常用方法是传递Comparator由Collections.reverseOrder()返回作为此方法的第二个参数。让我们看看Collections.reverseOrder()的实现来自openjdk7的方法:publicstaticComparatorreverseOrder(){return(Comparator)ReverseComparator.REVERSE_ORDER;}ReverseComparator类:privatestaticc

java - hibernate 异常 : Found two representations of same collection

如果我保存一个包含以下列表的对象@OneToMany(cascade=CascadeType.ALL,mappedBy="taskList")@OrderColumn(name="position",nullable=false)publicListtasks=newArrayList();我得到异常org.hibernate.HibernateException:FoundtworepresentationsofsamecollectionPlay!中的代码Controller看起来像这样:TaskListtaskList=taskList.findById(taskListId);

从 Collection 或 Iterable 中获取第 N 个元素的 Java 库

我有一个Collection,需要获取其Iterator返回的第N个元素。我知道我可以在计算其Iterator元素时进行迭代。是否有第三方库(GoogleGuava或ApacheCommons)可以执行此操作? 最佳答案 Guava的Iterators.get可以帮忙Advancesiteratorposition+1times,returningtheelementatthepositionthposition. 关于从Collection或Iterable中获取第N个元素的Java库

java - Collections.max() 签名的实际使用

这是对ExplanationofCollections.max()signature的后续问题,其中接受的答案没有深入探讨此通配符的实际原因。max方法需要一个Collection我想不出这个通配符有帮助的实际案例。我什至提到了OracleMorefunwithwildcards它指出Ingeneral,ifyouhaveanAPIthatonlyusesatypeparameterTasanargument,itsusesshouldtakeadvantageoflowerboundedwildcards(?superT).Conversely,iftheAPIonlyreturns

java - 在 Stream 和 Collections API 之间进行选择

考虑以下打印List中最大元素的示例:Listlist=Arrays.asList(1,4,3,9,7,4,8);list.stream().max(Comparator.naturalOrder()).ifPresent(System.out::println);使用Collections.max方法也可以达到同样的目的:System.out.println(Collections.max(list));上面的代码不仅更短而且更易读(在我看来)。我想到了类似的示例,例如binarySearch与filter与findAny结合使用。我知道Stream可以是一个无限管道,而不是一个Co

java - 在消费者方法中创建的 Lambda Collect 元素

我正在从丑陋的嵌套for循环转变为java中设计精美的lambda表达式。这是我的实际代码for(Stringfoo:foos){for(Barbar:bars){if(bar.getFoo().equals(foo)){FooBarfooBar=newFooBar();fooBar.setBar(bar);listOfFooBar.add(fooBar);break;}}}我实际的lambda代码来替换上面的代码foos.forEach(i->bars.stream().filter(p->p.getFoo().equals(i)).findFirst().ifPresent(p->

java - List.of(...) 或 Collections.unmodifiableList()

如果你有一个Liststrings例如,你会继续写:Collections.unmodifiableList(strings)或切换到:List.of(strings.toArray(newString[strings.size()]))实例化对性能(内存和运行时方面)的初始影响是什么?List.of中是否有运行时优势?变体? 最佳答案 这并不是一个很好的比较,因为这些方法做了不同的事情:Collections::unmodifiable...创建一个不可修改的View。它不是是不可变的,因为如果您更改原始的支持集合(您的示例中的l