通常Guava Lists.transform看起来像这样:
Lists.transform(new ArrayList<String>("1", "2", "3"),
new Function<String, Integer>() {
@Override
Integer apply(String str) {
return Integer.valueOf(str);
}
});
Run Code Online (Sandbox Code Playgroud)
一个Converter<A, B>定义doForward,并doBackward从去的方法A来B和B到A分别.逻辑上它是一对Function.看起来没有任何简单的方法
Converter到Function描述它的那对,或者Converter并向方法说出方向Lists.这些是不兼容的还是除了手动编写胶水代码之外还有其他方法吗?
手动胶水代码看起来像
final Converter<A, B> myConverter = /*...*/;
Lists.transform(myList, new Function<A, B>() {
@Override
B apply(A a) {
return myConverter.doForward(a);
}
});
Run Code Online (Sandbox Code Playgroud) 这个真让我(和我的同事)烦恼.
不是
那么为什么没有人注意到这个命名惯例的缺陷或者是否有这个错字背后的意图?
我有一个Iterable<T>.这个iterable包含m元素.我想创建一个Iterable<T>包含n元素,其中n = min(m, N)一些N用户输入.这些元素应该是n给定iterable 的第一个元素.
理想情况下,返回的Iterable将由原始Iterable而不是元素的副本支持.
是否有神奇的功能可以做到这一点,也许在番石榴?
例如,以下是标准模式
Function<Integer, Integer> powerOfTwo = new Function<Integer, Integer>() {
@Override
public Integer apply(Integer input) {
return (int) Math.pow(input, 2);
}
};
Run Code Online (Sandbox Code Playgroud)
实施申请有什么好处?写作会不会更清晰
Function<Integer, Integer> powerOfTwo = new Function<Integer, Integer>(Integer input) {
return (int) Math.pow(input, 2);
};
Run Code Online (Sandbox Code Playgroud) 寻找一种简单的方法来收集KeyAndValues having fields - String and List<T>到ImmutableListMultimap<String,T>?试图做一些像,
Collector.of(
ImmutableListMultimap.Builder::new,
ImmutableListMultimap.Builder<String,List<T>>::putAll,
(b1, b2) -> b1.putAll(b2.build()),
(builder) -> builder.build());
Run Code Online (Sandbox Code Playgroud)
putAll需要Key,List<T>.我不知道如何在组合器中实现这一点.
编辑:
@Value(staticConstructor = "of")
private static class KeyAndValues<T> {
private final String key;
private final List<T> values;
}
Run Code Online (Sandbox Code Playgroud) 我怎样才能转换List<Employee>为Map<Integer,List<Employee>>.
List<Employee>基于员工Object中存在的depId的组,Map键是depId.
java.util.*或Google Guava中是否有任何方法可以将其转换为迭代列表.
我试图使用HashSet来确保我从.txt文件中读取的数据是唯一的.
以下是样本数据;
999990 bummer
999990 bummer
999990 bummer
999990 bummer
99999 bummer
999990 bummerr
Run Code Online (Sandbox Code Playgroud)
使用Java.io.File和Java.util.Scanner读取并将其存储为Term of Term;
阅读;
while (rawTerms.hasNextLine()){
String[] tokens = rawTerms.nextLine().trim().split(delimiter);
if (tokens.length == 2) {
uniqueSet.add(new Term(Double.parseDouble(tokens[0]), tokens[1])); //add the term to set
}
else {
rawTerms.close();
throw new Exception("Invalid member length: "+ tokens.length);
}
}
allTerms = new ArrayList<>(uniqueSet); //Covert set into an ArrayList
Run Code Online (Sandbox Code Playgroud)
使用番石榴的术语类;
public Term(double weight, String theTerm){
this.weight = weight;
this.theTerm = theTerm;
}
@Override
public boolean equals(final Object obj) {
if …Run Code Online (Sandbox Code Playgroud) 我LinkedHashMultimap在我的项目中使用.我需要在保留插入顺序的同时展平值.例如用
SetMultimap<String, Integer> m = LinkedHashMultimap.create();
m.put("a", 1);
m.put("b",2);
m.put("a",3);
Run Code Online (Sandbox Code Playgroud)
我得到以下输出
a : [1,3]
b : 2
Run Code Online (Sandbox Code Playgroud)
但是我需要
a : 1
b : 2
a : 3
Run Code Online (Sandbox Code Playgroud)
或者我需要输出在List中
[a,1,b,2,a,3]
Run Code Online (Sandbox Code Playgroud)
PS我正在使用,LinkedHashMultimap因为我不想复制valuesa key,我需要保留插入顺序
我怎么能这样做,所以我可以迭代上面的输出进行进一步处理?
据我所知,在更改某些对象状态之前,我们使用Guava Preconditions快速失败(这是stackoverflow的一个很好的答案).这很好.但是它会抛出运行时异常,这不是应用程序用户最喜欢的异常(500错误等等......).所以我需要你在设计方面给我一些帮助.
我有一个声明许多方法的接口.每个方法都有必须控制的参数(例如:not null).所以在实现类中我使用如下指令:
Preconditions.checkNotNull(fooObj);
Run Code Online (Sandbox Code Playgroud)
但是,调用此API的程序可能会因运行时异常而崩溃,在本例中为NullPointerException.
那么你如何处理这些未经检查的异常呢?
谢谢.
--------编辑应用层:
数据访问层
API声明交换DTO的方法
使用Guava实现API并检查参数的过程
Web服务取决于流程层
基本上,更好的写作方式:
Map<String, String> originalMap = getMapOfValues();
Map<String, String> newMap = originalMap.entrySet()
.stream()
.map(entry ->
Maps.immutableEntry(entry.getKey(), mapValue(entry.getValue()))
).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue);
Run Code Online (Sandbox Code Playgroud)
(Maps.immutableEntry是番石榴的方法)
guava ×10
java ×8
collections ×1
collectors ×1
eclipse ×1
exception ×1
java-7 ×1
java-8 ×1
multimap ×1