我是Java的新手,所以如果已经在其他地方回答了这个问题,那么我要么不知道搜索正确的内容还是我无法理解答案.
所以问题是:我在列表中有一堆对象:
try(Stream<String> logs = Files.lines(Paths.get(args))) {
return logs.map(LogLine::parseLine).collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
这就是添加属性的方式:
LogLine line = new LogLine();
line.setUri(matcher.group("uri"));
line.setrequestDuration(matcher.group("requestDuration"));
....
Run Code Online (Sandbox Code Playgroud)
如何对日志进行排序,以便最终得到列表,其中具有相同"uri"的对象仅以平均requestDuration显示一次.
例:
object1.uri = 'uri1', object1.requestDuration = 20;
object2.uri = 'uri2', object2.requestDuration = 30;
object3.uri = 'uri1', object3.requestDuration = 50;
Run Code Online (Sandbox Code Playgroud)
结果:
object1.uri = 'uri1', 35;
object2.uri = 'uri2', 30;
Run Code Online (Sandbox Code Playgroud)
提前致谢!
是否可以编写类似这样的内容并避免检查元素是否为空且集合不为空:
response.getBody()
.getRequestInformation()
.getRequestParameters().get(0)
.getProductInstances().get(0)
.getResultParameters()
Run Code Online (Sandbox Code Playgroud)
我找到了这样的东西 http://winterbe.com/posts/2015/03/15/avoid-null-checks-in-java/
基本上,我想要实现的是避免if多个检查天气对象的语句为null或者层次结构中的集合为空.我从上面的帖子中读到,这可以通过可选"Null检查在引擎盖下自动处理".
如果已经有一些解决方案,抱歉复制,请转介给我.
我有一个根据他们的年龄分组的人员列表(group1的年龄<20,group2的年龄> = 20),并根据姓名和年龄使用Java 8对每个组进行排序:人员列表:
第1组:
Ane 12
Col 14
Run Code Online (Sandbox Code Playgroud)
第2组:
James 24
thomas 34
Xavier 55
Run Code Online (Sandbox Code Playgroud)
我想用Java 8做这样的事情:
list.stream().collect(Collectors.groupingBy(...
Run Code Online (Sandbox Code Playgroud) 对于实际使用的Java8示例:: https://github.com/java8/Java8InAction/blob/master/src/main/java/lambdasinaction/chap1/FilteringApples.java
public static boolean isGreenApple(Apple apple),
public static boolean isHeavyApple(Apple apple) {
Run Code Online (Sandbox Code Playgroud)
我想添加类似的方法,例如
public static boolean isAppleOfColor(Apple apple, String color)
Run Code Online (Sandbox Code Playgroud)
我想使用相同的模式访问它:
List<Apple> colorApples = filterApples(inventory, FilteringApples::isAppleOfColor("red"));
Run Code Online (Sandbox Code Playgroud)
但是我不能在FilteringApples :: isAppleOfColor(“ red”)中传递参数。
到目前为止,我已经使用以下方法实现了这一点:
public static Predicate<Apple> colourMatches( String color) {
return p->color.equals(p.getColor());
}
Run Code Online (Sandbox Code Playgroud)
然后打电话
List<Apple> colorApples = filterApples(inventory, (Apple a)->a.colourMatchesOnApple("red"));
System.out.println(colorApples);
Run Code Online (Sandbox Code Playgroud)
这可行。但是有没有办法我可以使用“引用的”参数化方法,例如FilteringApples::isAppleOfColor("red").
谢谢
编辑
谢谢,我刚刚在JLS 15.13中意识到:https : //docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.13 如下文本:
无法指定要匹配的特定签名,例如Arrays :: sort(int [])。而是,功能接口提供了用作过载解析算法(第15.12.2节)的输入的参数类型。这应该满足绝大多数用例;当极少数需要进行更精确的控制时,可以使用lambda表达式。
我有一个函数在a上执行一些流操作(特别是一个过滤器)List.
public List<String> getAndFilterNames(List<Person> people, Predicate<Person> nameFilter){
List<String> allNames = people.stream()
.map(person -> person.getName())
.filter(nameFilter)
.collect(Collectors.toList());
return allNames;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够通过中间流操作(filter,distinct等),并有我的功能运行的终端操作之前执行该操作.就像是:
public List<String> getAndProcessNames(List<Person> people, <intermediate stream operation>){
List<String> allNames = people.stream()
.map(person -> person.getName())
// perform <intermediate stream operation> here
.collect(Collectors.toList());
return allNames;
}
Run Code Online (Sandbox Code Playgroud)
虽然,根据我目前的经验水平,似乎是不可能的.一些中间操作有一个参数(比如filter),有些没有(比如distinct),所以我不能设置一个能处理所有情况的参数类型...我想我可以创建一些签名,但是Function和Supplier.
即便如此,该功能界面与参数的功能需要变化...... filter需要Predicate,map需要Function,并从我的理解,是没有办法来表示一个通用的功能接口.那是对的吗?它们都没有真正的公共类或接口.
那么,到底,看来我最好的选择是公正map和collect,然后运行所需的我流操作的情况下,逐案的基础,如:
public List<String> getNames(List<Person> people){
List<String> allNames = …Run Code Online (Sandbox Code Playgroud) 我是CompletableFuture的新手,我想调用MetadataLoginUtil :: login方法,它可以抛出异常.但是,尽管我已经"特别"编写,但下面的代码并未编译.它说我必须在try&catch中包装MetadataLoginUtil :: login'.
请指教.谢谢!
public void run() throws ConnectionException {
CompletableFuture<Void> mt = CompletableFuture.supplyAsync(MetadataLoginUtil::login)
.exceptionally(e -> {
System.out.println(e);
return null;
})
.thenAccept(e -> System.out.println(e));
}
Run Code Online (Sandbox Code Playgroud) 我通过实现Collector接口并覆盖其方法来实现自定义收集器.Collector实现如下:
class MyCustomCollector implements Collector<Person, StringJoiner, String>{
@Override
public Supplier<StringJoiner> supplier() {
// TODO Auto-generated method stub
return () -> new StringJoiner("|");
}
@Override
public BiConsumer<StringJoiner, Person> accumulator() {
// TODO Auto-generated method stub
return (joiner,person) -> joiner.add(person.name.toUpperCase());
}
@Override
public BinaryOperator<StringJoiner> combiner() {
// TODO Auto-generated method stub
return (joiner1, joiner2) -> joiner1.merge(joiner2);
}
@Override
public Function<StringJoiner, String> finisher() {
// TODO Auto-generated method stub
return StringJoiner::toString;
}
@Override
public Set<java.util.stream.Collector.Characteristics> characteristics() {
// TODO Auto-generated method stub
return …Run Code Online (Sandbox Code Playgroud) 我的xml看起来像:
<?xml version="1.0"?>
<Grid id = 1>
<Setup id = "1">
<Group name = "DrawingRoom">
<Light id = "1">
<name>Light1</name>
<type>ben</type>
<index>1</index>
</Light>
<Light id = "2">
<name>Light2</name>
<type>crux</type>
<index>2</index>
</Light>
<Light id = "3">
<name>Light3</name>
<type>let</type>
<index>3</index>
</Light>
</Group>
<Group name = "BedRoom">
<Light id = "1">
<name>Light1</name>
<type>let</type>
<index>4</index>
</Light>
</Group>
</Setup>
<Setup id = "2">
<Group name = "ClubRoom">
<Light id = "1">
<name>Light1</name>
<type>let</type>
<index>1</index>
</Light>
</Group>
</Setup>
</Grid>
Run Code Online (Sandbox Code Playgroud)
当我尝试在解析xml时填充hashmap时出现问题.我使用四个hashmap来保存不同级别的信息.因此,最终的hashmap包含来自较低级别的hashmap,例如setup,group和light,每个级别的属性是该级别的相应地图的键.
public HashMap<String,String> lightContent = new HashMap<String,String>(); …Run Code Online (Sandbox Code Playgroud) 我可以在不更改日期的情况下更改Java代码中Date对象的时区吗?当我写作时,
Date date = new Date(longValue);
我会在当地时区得到这个日期.我希望得到这个日期,但我的时区应该是我的用户ID(例如"America/Chicago")在DB(非本地)中存在的时区.
在此先感谢您的帮助.
我有一个包含嵌套和内部类的类.在某些程序中,我使用遗留java方式中的相等运算符检查对象的空值.以下是我的代码片段:
class Outer {
Nested nested;
Nested getNested() {
return nested;
}
}
class Nested {
Inner inner;
Inner getInner() {
return inner;
}
}
class Inner {
String foo;
String getFoo() {
return foo;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我如何对引用变量进行空检查:
Outer outer = new Outer();
if (outer != null && outer.nested != null && outer.nested.inner != null) {
System.out.println(outer.nested.inner.foo);
}
Run Code Online (Sandbox Code Playgroud)
我知道这也可以使用java 8的Optional类来完成,但是我没有办法在上面的特定场景中做同样的事情.有什么建议吗?
java-8 ×10
java ×6
java-stream ×4
optional ×2
average ×1
collections ×1
collectors ×1
grouping ×1
hashmap ×1
jodatime ×1
lambda ×1
methods ×1
null-check ×1
predicate ×1
sorting ×1
xml ×1
xml-parsing ×1