鉴于以下代码,我如何将其简化为单一的功能线?
// DELETE CSV TEMP FILES
final Map<Boolean, List<File>> deleteResults = Stream.of(tmpDir.listFiles())
.filter(tempFile -> tempFile.getName().endsWith(".csv"))
.collect(Collectors.partitioningBy(File::delete));
// LOG SUCCESSES AND FAILURES
deleteResults.entrySet().forEach(entry -> {
if (entry.getKey() && !entry.getValue().isEmpty()) {
LOGGER.debug("deleted temporary files, {}",
entry.getValue().stream().map(File::getAbsolutePath).collect(Collectors.joining(",")));
} else if (!entry.getValue().isEmpty()) {
LOGGER.debug("failed to delete temporary files, {}",
entry.getValue().stream().map(File::getAbsolutePath).collect(Collectors.joining(",")));
}
});
Run Code Online (Sandbox Code Playgroud)
这是我遇到的一种常见模式,我有一些东西,我想过滤这个流,根据该过滤器创建两个流,然后我可以做一件事流A和另一件事流B.这是一种反模式,还是以某种方式支持?
Map<Character, Integer> getMap(String target) {
return target.chars().boxed()
.map(c -> Character.valueOf((char) c.intValue()))
.collect(Collectors.groupingBy(
c -> c,
Collectors.reducing(0, c -> 1, Integer::sum)
));
}
Run Code Online (Sandbox Code Playgroud)
这条线target.chars().boxed().map(c -> Character.valueOf((char) c.intValue()))真的很难看,有没有更好的方法呢?
我正在使用NetBeans IDE 8.2 for JavaFX.我已经知道,为了改变按钮的位置,我需要使用setLayoutX/Y. 我试过这个,对按钮没有任何影响.这是我的代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javafxapplication2;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* @author coolDawg1234
*/
public class JavaFXApplication2 extends Application {
@Override
public void start(Stage primaryStage) {
String x = "1";
String y = "0"; …Run Code Online (Sandbox Code Playgroud) 我想在流上连续执行以下操作.
1).我想从创建数字流2来n.
我想这可以这样做:IntStream.range(2, n).
2).添加过滤器,只有当此数字不能被已通过此过滤器的任何其他数字整除时,才能使数字更进一步.
我无法实现这样的过滤器.我想我必须创建一个包含an的匿名类,ArrayList我将存储通过此过滤器的所有数字ArrayList.这一切应该看起来像:
IntStream.range(2, n).filter(new IntPredicate() {
ArrayList<Integer> prev;
@Override
public boolean test(int value) {
if (prev == null) {
prev = new ArrayList();
return true;
}
for (int i = 0; i < prev.size(); i++) {
if (value % prev.get(i) == 0) {
return false;
}
}
prev.add(value);
return true;
}
Run Code Online (Sandbox Code Playgroud)
3).获取Map<Integer, Integer>,其中密钥为数百(即100,200,300,400等),值为每百个中的素数数.所以对于百,100我必须找到[100,199]范围内的素数.
我想非常清楚,在第二步之后,流中只剩下素数.但是我不知道如何执行第三步并且不确定我的第二步的实现.
能帮我解决所有操作吗?
构造函数引用(用于函数式编程时是一种特殊类型的方法引用)在没有在Functional接口中声明的抽象方法的任何显式定义的情况下进行求值.你能帮我解决它的解决方法或链接的复杂性吗?特别是对于以下程序如何评估empFactory对象?
public class Employee{
String name;
Integer age;
public Employee(String name, Integer age){
this.name=name;
this.age=age;
}
}
public interface EmployeeFactory{
public abstract Employee getEmployee(String name, Integer age);
}
public class Run{
public static void main(String... args){
EmployeeFactory empFactory=Employee::new;
Employee emp= empFactory.getEmployee("Ammy Sen", 25);
}
}
Run Code Online (Sandbox Code Playgroud) interface A {
default void m1() {}
}
interface B{
static void m1() {}
}
class C implements A,B {
}
Run Code Online (Sandbox Code Playgroud) 以下代码返回一个空字符串:
LocalDateTime.parse("2018-01-01T00:00:00.00")
.format(new DateTimeFormatterBuilder().toFormatter())
Run Code Online (Sandbox Code Playgroud)
我期望一个例外或以某种默认方式格式化的日期.
我是否在Java中发现了一个错误,或者这是否符合规范?我在Javadoc中找不到有关此行为的任何信息.
我有一个以字符串为值的地图。我想首先按长度排序,如果字符串的长度相同,我想按字母排序。我写了那些代码:
String out = outMap.values().stream()
.sorted(Comparator.comparing(e -> e.length()).thenComparing()...)
.collect(Collectors.joining());
Run Code Online (Sandbox Code Playgroud)
问题是,当我写thenComparing时,我不能使用e.length()了。我该如何解决?
编辑:Map<Character, String>。我想对所有字符串进行排序,并在输出中连接一个字符串。
我尝试学习SpEL,但我的一个脚本不起作用.我的脚本是:
changePages(#{T(StoryContent).pages}, (x, y) -> x.replaceAll(y, x.substring(0, 1) + '...' + x.substring(x.length() - 1, x.length())))
Run Code Online (Sandbox Code Playgroud)
Java代码:
public void validate(Story story, List<StoryRule> rules) throws NoSuchMethodException, SecurityException {
StoryContent storyContent = storyContentRepository.findOne(story.getContentId());
story.setStatus(SchedulerStatus.VALIDATION);
storyRepository.save(story);
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext inventorContext = new StandardEvaluationContext(storyContent);
inventorContext.registerFunction("changePages", ValidationByRuleServiceImpl.class
.getDeclaredMethod("changePages", new Class[] { List.class, BiFunction.class }));
try {
rules.stream().map(StoryRule::getScript)
.forEach(script -> parser.parseExpression(script).getValue(inventorContext));
if (!storyContent.getTitle().equals(story.getName()))
story.setName(storyContent.getTitle());
story.setStatus(SchedulerStatus.PUBLISHED);
storyContentRepository.save(storyContent);
} catch (Exception e) {
LOG.error(e.getMessage());
story.setStatus(SchedulerStatus.ERROR);
} finally {
storyRepository.save(story);
}
}
private void changePages(List<String> pages, BiFunction<String, …Run Code Online (Sandbox Code Playgroud) 例如,我有课
public class Human {
private String name;
...
}
Run Code Online (Sandbox Code Playgroud)
我想实现这样的事情:
(1)
List<Human> humans = initHumans();
Equals<Human> humanEquals = new Equals<>();
Predicate<Human> filter = humanEquals.filter("name", "John");
List<Human> filteredHumans = humans
.stream()
.filter(filter)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
等于:
public class Equals<T> extends AbstractPredicate<T> {
public java.util.function.Predicate<T> filter(String fieldName, String fieldValue) {
....
}
}
Run Code Online (Sandbox Code Playgroud)
是否提供了(1)行为的有效工具过滤方法?
我想回复Predicate这样的:
Predicate<Human> predicate = human -> human.getName().equals("John");
Run Code Online (Sandbox Code Playgroud)
同样应该适用于其他类:
Predicate<Car> filter = humanEquals.filter("color", "red");
//like this:
Predicate<Car> predicate= human -> human.getColor().equals("red");
Run Code Online (Sandbox Code Playgroud) java-8 ×10
java ×9
java-stream ×4
button ×1
comparator ×1
datetime ×1
generics ×1
java-time ×1
javafx ×1
layout ×1
netbeans ×1
predicate ×1
reflection ×1
spring ×1
spring-el ×1