Guava

Multi criteria sorting of a list of objects with Guava Ordering

喜欢而已 提交于 2020-01-12 07:01:16
问题 I have a class WHICH CANNOT implement comparable, but needs to be sorted based on 2 fields. How can I achieve this with Guava? Let's say the class is: class X { String stringValue; java.util.Date dateValue; } And I have a list of these: List<X> lotsOfX; I want to sort them based on the value field first and then based on dateValue descending within each 'group' of 'value' fields. What I have been doing so far is: List<X> sortedList = ImmutableList.copyOf(Ordering.natural().onResultOf

Removing the “first” object from a Set

巧了我就是萌 提交于 2020-01-12 02:53:06
问题 Under certain situations, I need to evict the oldest element in a Java Set . The set is implemented using a LinkedHashSet, which makes this simple: just get rid of the first element returned by the set's iterator: Set<Foo> mySet = new LinkedHashSet<Foo>(); // do stuff... if (mySet.size() >= MAX_SET_SIZE) { Iterator<Foo> iter = mySet.iterator(); iter.next(); iter.remove(); } This is ugly: 3 lines to do something I could do with 1 line if I was using a SortedSet (for other reasons, a SortedSet

Are Guava-11.0.2.jar conflicting with com.google.common_1.0.0.0_0-6.jar?

烈酒焚心 提交于 2020-01-11 10:04:07
问题 I have a problem to deploy an application on weblogic server, after a discussion here on stackoverflow i realized that the problem is that weblogic 12c is using a lib file com.google.common_1.0.0.0_0-6.jar , and my application is using Guava-11.0.2.jar, and it sound for me like they are conflicting, tried to search on google but i have no idea what to do, whats the proper solution to solve it? 回答1: I came across the same problem, what I did was, I replaced the com.google.common_1.0.0.0_0-6

java - google guava cache difference between invalidateAll() and cleanUp()

拟墨画扇 提交于 2020-01-11 05:37:50
问题 Say I have a Cache that is defined like this: private static Cache<String, Long> alertsUIDCache = CacheBuilder.newBuilder(). expireAfterAccess(60).build(); From what I read (please correct me if I am wrong): If value is written to Cache at 0:00, it should be moved to "ready to be evicted" status after 60 seconds. The actual removing of the value from the Cache will happen at the next cache modification (what is exactly is cache modification ?). is that right? Also, I am not sure what the

Collect Lines using Multimap Collector

喜欢而已 提交于 2020-01-11 05:22:10
问题 Is there a way to covert the below to using collectors yet? List<String[]> lines = getLines(); Multimap<String,String> multimap = ArrayListMultimap.create(); lines.forEach(line -> multimap.put(line[0],line[1]); ); 回答1: You can use Multimaps.toMultimap collector: ListMultimap<String, String> multimap = lines.stream() .collect(Multimaps.toMultimap( l -> l[0], l -> l[1], ArrayListMultimap::create )); Or if you don't need mutability, use ImmutableListMultimap.toImmutableListMultimap collector:

Google Collections on Android

你离开我真会死。 提交于 2020-01-11 03:55:06
问题 Has anyone ever used Multimaps on Android is it possible? 回答1: Guava works as-is on Android. What problems did you have? Use the released JAR, not the Guava sources. And as always, you should be using ProGuard as part of your build process to trim the size of your final binary down. 来源: https://stackoverflow.com/questions/5168505/google-collections-on-android

Filtering on List based on one property with guava

被刻印的时光 ゝ 提交于 2020-01-10 22:46:41
问题 I have a class called Person - public class Person implements Nameable { private String name; public String getName(){ return name; } } Now I have two lists - List<Person> persons = // some persons List<Person> subsetOfPersons = // some duplicate persons, but different objects and don't share the same identity Now I would like to filter the persons which are not present in the subsetOfPersons , equality criteria is name property and Person doesn't have equals. How can I do this? 回答1: I'm sure

How does Eclipse know that com.sun is a restricted API?

岁酱吖の 提交于 2020-01-10 05:15:27
问题 I'm using ClassPath.getAllClasses from Guava to find in runtime classes that implement a certain interface. When used with rt.jar it finds many classes from sun.* and similar packages and I'd like to filter them out. I want to reuse the mechanism used by Eclipse and maybe other java compilers to warn about restricted API. Does this information reside JRE or hardcoded into Eclipse? 回答1: This information is stored in so-called Execution Environments profiles inside of Eclipse . Because these

How does Eclipse know that com.sun is a restricted API?

这一生的挚爱 提交于 2020-01-10 05:15:05
问题 I'm using ClassPath.getAllClasses from Guava to find in runtime classes that implement a certain interface. When used with rt.jar it finds many classes from sun.* and similar packages and I'd like to filter them out. I want to reuse the mechanism used by Eclipse and maybe other java compilers to warn about restricted API. Does this information reside JRE or hardcoded into Eclipse? 回答1: This information is stored in so-called Execution Environments profiles inside of Eclipse . Because these

高并发利器之限流

早过忘川 提交于 2020-01-07 18:52:57
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 何为限流? 在开发高并发系统时,有三把利器用来保护系统:缓存、降级和限流。那么何为限流呢?顾名思义,限流就是限制流量,就像你宽带包了1个G的流量,用完了就没了。通过限流,我们可以很好地控制系统的qps,从而达到保护系统的目的。 常见限流算法 1、计算器算法 计数器算法是限流算法里最简单也是最容易实现的一种算法。比如我们规定,对于A接口来说,我们1分钟的访问次数不能超过100个。那么我们可以这么做:在一开 始的时候,我们可以设置一个计数器counter,每当一个请求过来的时候,counter就加1,如果counter的值大于100并且该请求与第一个 请求的间隔时间还在1分钟之内,那么说明请求数过多;如果该请求与第一个请求的间隔时间大于1分钟,且counter的值还在限流范围内,那么就重置 counter。 public class CounterTest { public long timeStamp = getNowTime(); public int reqCount = 0; public final int limit = 100; // 时间窗口内最大请求数 public final long interval = 1000; // 时间窗口ms public boolean grant() {