我正在尝试使用 Guava 函数从列表中删除重复项。这样做的原因是“重复”是基于列表中两个项目之间的比较,而这些对象是否“重复”需要相当多的逻辑。
这是我对该功能的尝试:
private Function<List<BaseRecord>, List<BaseRecord>> removeDuplicates =
new Function<List<BaseRecord>, List<BaseRecord>>() {
public List<BaseRecord> apply(List<BaseRecord> records) {
List<BaseRecord> out = Lists.newArrayList();
PeekingIterator<BaseRecord> i = Iterators.peekingIterator(records.iterator());
while (i.hasNext()) {
BaseRecord current = i.next();
boolean isDuplicate = false;
if ( i.hasNext() ) {
BaseRecord next = i.peek();
// use a ComparisonChain to compare certain fields, removed
isDuplicate = compareCertainObjects(o1, o2);
}
if ( !isDuplicate ) {
out.add(current);
}
}
return out;
}
};
Run Code Online (Sandbox Code Playgroud)
然后我尝试用 Lists.transform(originalRecords, removeDuplicates) 调用它
不幸的是,Eclipse 并不高兴:
The method …Run Code Online (Sandbox Code Playgroud) 我有一个对象集合如下:
Collection<Foo>
Run Code Online (Sandbox Code Playgroud)
哪里Foo
public class Foo {
private User user;
private Item item;
public Foo(User user, Item item) {
this.user = user;
this.item = item;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Item getItem() {
return item;
}
public void setItem(Item item) {
this.item = item;
}
}
Run Code Online (Sandbox Code Playgroud)
我想Collection<Item>使用返回另一个类型的集合Collection<Foo>。我可以通过使用 afor loop并循环 Collection,获取项目并将其添加到新列表中来做到这一点。到目前为止,我已经使用 Google Guava 创建了 usingCollection<Foo>谓词。
Google guava 中是否有方法/函数允许我创建 …
我必须使用 GuavaLoadingCache将 xpath 表达式缓存为 xpath 值。
问题在于并非所有 xpath 都有值。大多数时候该值为null.
代码片段:
private LoadingCache<String, List<String>> cachedXpaths = CacheBuilder.newBuilder()
.expireAfterWrite(3, TimeUnit.MINUTES)
.maximumSize(1000)
.concurrencyLevel(5)
.weakKeys()
.build(new CacheLoader<String, List<String>>() {
@Override
public List<String> load(String key) throws Exception {
return createListByKey(key);
}
});
private static List<String> createListByKey(String key) throws Exception {
List<String> values = null;
try {
values = instance.getXpathValues(key);
} catch (XPathExpressionException ignore) {
}
return values;
}
Run Code Online (Sandbox Code Playgroud)
结果:
testEncounterSection(com.epam.cdatest.section.EncountersSectionTest) Time elapsed: 0.002 sec <<< FAILURE!
com.google.common.util.concurrent.UncheckedExecutionException: com.epam.cdatest.exceptions.XpathHasEmptyValueException
at com.epam.cdatest.parsers.XpathEvaluator.getXpathValues(XpathEvaluator.java:123)
at …Run Code Online (Sandbox Code Playgroud) 我正在用Java浏览Java中的密码Hashing.
password = Hashing
.sha256()
.hashString(input, StandardCharsets.UTF_8)
.toString();
Run Code Online (Sandbox Code Playgroud)
当我将任何文本传递给该行时,它会散列并以小写字符输出所有内容,例如,如果我传递"foo",则值password变为:
2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用此站点来散列"foo",它输出的值是
2C26B46B68FFC68FF99B453C1D30413413422D706483BFA0F98A5E886266E7AE
Run Code Online (Sandbox Code Playgroud)
据我所知,这只是完全相同的密码,除了大写字母而不是小写字母.
导致那些输出不同值的原因是什么,以及如何使用大写字母输出番石榴(不需要调用toUpperCase,除非这是唯一的方法)
我有一个设置,我的每个“人”都映射到一个特定的“房间”。
但是,多人可以映射到同一个房间。
如果看到现有人员,则应将其房间更新为新值。
所以这是传统的用例 Map<Person, Room>.put(Person, Room)
但是,查找始终是“这个房间里有哪些人?”
Set<People> get(Room q){}
我显然可以制作自己的数据结构或简单地迭代键值对;但是其中一个 Java 集合库是否具有良好的结构来支持我需要的参照完整性和查找?
我遇到了一些需要大量工作才能解决的番石榴依赖问题。由于第三方库也依赖于 Guava(不幸的是我不得不使用它),我必须将我对 Guava 的依赖从27.0.1-jre一路降级到20.0. 除了下面的代码被破坏之外,似乎没有主要的副作用,因为ImmutableSet.toImmutableSet()它只在 Guava 中引入21.0:
ImmutableSet.toImmutableSet();
Run Code Online (Sandbox Code Playgroud)
完整的代码块是:
cronJobDefinitions = cronJobsRegistry.get()
.stream()
.map(ThrowingFunction.unchecked(clazz -> clazz.newInstance()
.getCronJobDefinition()))
.collect(ImmutableSet.toImmutableSet());
Run Code Online (Sandbox Code Playgroud)
也许有一种简单的方法可以用ImmutableSet.toImmutableSet()JDK 中的替代方法替换?我目前正在使用 JDK8,(但如果在较新的 JDK-s 中有更好的解决方案,我会很想知道)。
我可以像在 ehcache 中那样插入具有不同到期时间的元素吗
例如 :
缓存 : add element1 //5 秒后过期
cache : add element2 //15秒后过期
cache : add element3 //在'x'秒/分钟后过期
如果没有,你能建议任何替代缓存来解决这个问题吗?
我也打算使用 ehcache,但结果证明它不支持 active/eager 过期。
我正在尝试使用 Streams.zip 创建地图。我不关心流返回的结果,我只需要为每个元素执行 lambda。这是我的代码
String x[] = {"a", "b"};
String y[] = {"c", "d"};
var data = new HashMap<String, String>();
var myStream = Streams.zip(Arrays.stream(x), Arrays.stream(y),
(arg1, arg2) -> { data.put(arg1, arg2); return 0; };
var result = myStream.collect(Collectors.toList()); // ideally don't want this line
Run Code Online (Sandbox Code Playgroud)
如果没有 collect() 行,我不会填充地图,那么最简单的方法是什么,我尝试了 count() 而不是 collect ,但这完全是 zip 的快捷方式。或者是否有没有 Streams.zip 的更简单的单行解决方案来从两个列表填充地图?
我有一个用例,我需要存储大约大小的键 - 值对。大小为 8 GB 的单个 VM 中的 5 亿个条目。Key 和 Value 属于 Long 类型。键从 1、2、3 开始自动递增,依此类推。
仅当我在程序开始时构建这个 Map[KV] 结构作为独占操作,一旦构建,仅用于查找,在此结构中不会执行更新或删除。
我已经用 java.util.hashMap 尝试过这个,但正如预期的那样,它消耗了大量内存并且程序给 OOM:堆使用超过错误。
我需要一些有关以下方面的指导,这有助于减少内存占用,我对访问性能有所下降感到满意。
我正在寻找一个排序的多映射,其中一个键可以映射到多个值,无论equals()这些值返回什么。因此,它不应该是这样的:
import com.google.common.collect.*;
public class Test {
public static void main(String[] args) {
Multimap<Double, String> multimap = TreeMultimap.create();
multimap.put(3.0, "a");
multimap.put(3.0, "a");
multimap.put(2.0, "b");
// prints "b", "a"
for(String s : multimap.values())
System.out.println(s);
}
}
Run Code Online (Sandbox Code Playgroud)
我一直在使用,java.util.Map<Key, List<Value>>但它带有一些样板。
一种可能性是参数化类型:
public class SortedMapOfLists<K, V> {
SortedMap<K, List<V>> map = new TreeMap<>();
public void put(K key, V value) {
List<V> list = map.get(key);
if(list == null) {
list = new ArrayList<>();
map.put(key, list);
}
list.add(value);
}
public List<V> values() …Run Code Online (Sandbox Code Playgroud) guava ×10
java ×10
caching ×2
collections ×1
dictionary ×1
ehcache ×1
hash ×1
hashmap ×1
ignite ×1
java-stream ×1
list ×1
sha256 ×1
xml ×1
xpath ×1