我正在以格式从数据库中读取日期时间2017-04-20 11:01:21.053,我需要解析该格式04/20/2017 11:01:21
我正在尝试使用以下代码在LocalDateTime(Java 8)上解析此日期:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-mm-dd hh:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse(dateToFormat, formatter);
Run Code Online (Sandbox Code Playgroud)
但是在尝试解析时出现以下错误2017-04-20 11:01:21.053:
文本'2017-04-20 11:01:21.053'无法在索引14处解析
我在这做错了什么?
我怎样才能转换List成Optional<List>?
以下代码生成编译错误:
public Collection<Optional<UserMeal>> getAll() {
Comparator comparator = new SortedByDate();
List<UserMeal> mealList = new ArrayList<>(repository.values());
Collections.sort(mealList,comparator);
Collections.reverse(mealList);
**List<Optional<UserMeal>> resultList = Optional.of(mealList);**
return resultList;
}
Run Code Online (Sandbox Code Playgroud) 每当我启动以下代码时:
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
String exchangeName = "direct_logs";
channel.exchangeDeclare(exchangeName, "direct");
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, exchangeName, "red");
channel.basicQos(1);
final Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag,
Envelope envelope,
AMQP.BasicProperties properties,
byte[] body) throws IOException{
String message = new String(body, "UTF-8");
System.out.println(message);
System.out.println("message received");
}
};
channel.basicConsume(queueName, true, consumer);
Run Code Online (Sandbox Code Playgroud)
它不会像文档中所暗示的那样开始无限循环。相反,它会立即停止。我可以消耗一段时间的唯一方法是channel.basicConsume用循环替换,如下所示:
DateTime startedAt = new DateTime();
DateTime stopAt = startedAt.plusSeconds(60);
long i=0;
try {
while (stopAt.compareTo(new …Run Code Online (Sandbox Code Playgroud) 我想知道是否存在一种从同步方法调用创建CompletableFuture的情况。如果没有,为什么?
长版:
final CompletableFuture<ReturnType> future = new CompletableFuture<>();
final String parameters = "hello";
ReturnType result;
try {
result = syncMethodCall(parameters);
} catch (Exception e) {
future.completeExceptionally(e);
}
future.complete(result);
return future;
Run Code Online (Sandbox Code Playgroud)
所需的简短版本(或同类):
final String parameters = "hello";
return CompletableFuture.superMethod(() -> {syncMethodCall(parameters)});
Run Code Online (Sandbox Code Playgroud) 我需要根据需要按顺序执行的3个检查来选择第一个结果.即如果没有符合标准1的对象,那么我们寻找满足标准2的任何对象,依此类推.这是我的工作代码
MyClass result = myObjects.stream()
.filter(s -> s.meetsCriterion1())
.findFirst()
.orElseGet(() -> {
return myObjects.stream()
.filter(s -> s.meetsCriterion2())
.findFirst()
.orElseGet(() -> {
return myObjects.stream()
.filter(s -> s.meetsCriterion3())
.findFirst()
.orElseGet(() -> {
return null;
});
});
});
Run Code Online (Sandbox Code Playgroud)
我们可以改进这段代码吗?我不确定是否有办法重用第一个流来评估所有标准.
我正在尝试学习Java 8 Stream,当我尝试将一些函数转换为java8来练习时.我遇到了一个问题.
我很好奇如何将跟随代码转换为java流格式.
/*
* input example:
* [
{
"k1": { "kk1": 1, "kk2": 2},
"k2": {"kk1": 3, "kk2": 4}
}
{
"k1": { "kk1": 10, "kk2": 20},
"k2": {"kk1": 30, "kk2": 40}
}
]
* output:
* {
"k1": { "kk1": 11, "kk2": 22},
"k2": {"kk1": 33, "kk2": 44}
}
*
*
*/
private static Map<String, Map<String, Long>> mergeMapsValue(List<Map<String, Map<String, Long>>> valueList) {
Set<String> keys_1 = valueList.get(0).keySet();
Set<String> keys_2 = valueList.get(0).entrySet().iterator().next().getValue().keySet();
Map<String, Map<String, Long>> result = new …Run Code Online (Sandbox Code Playgroud) 我有一个主班
public class Main{
public static void main(String[] args) {
Gui gui = new Gui();
}
}
Run Code Online (Sandbox Code Playgroud)
那我再上课Gui
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class Gui extends Application{
public Gui() {
Application.launch();
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("");
FlowPane flowLayout= new FlowPane();
Scene scene = new Scene(flowLayout,200,200);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
我也想通过创建Gui实例并使用默认构造函数从Main类启动Javafx Application。我该怎么做?
我有一个interface Course和一个class CourseCLS implements Course.
首先,我尝试CourseCLS用相同的数字对对象进行分组.我不确定它是否正确.
Map<Integer, List<CourseCLS>> first =
courses.values().stream()
.collect(Collectors.groupingBy(c -> c.getNumber()));
Run Code Online (Sandbox Code Playgroud)
现在,我需要改变Map<Integer, List<CourseCLS>>成Map<Integer, List<Course>>,然后返回.
有什么想法怎么做?
groupingBy(c -> c.overHeadPercentage(),
Collectors.mapping(p -> (Course) p, Collectors.toList()))
Run Code Online (Sandbox Code Playgroud) 我试图建立一个值的名称地图.我有这个方法getValue两次调用该方法.一旦确保返回值不为null,然后再次实际收集该值.我试图弄清楚如何添加一个lambda块来捕获返回对象.
Arrays.stream(enums)
.collect(Collectors.toMap(
en -> en.name(),
en -> ((issue.getValue(en) != null) ? issue.getValue(en) : "")
));
Run Code Online (Sandbox Code Playgroud)
当我尝试不同的安排时,我不断收到各种IDE语法错误.我觉得这应该是微不足道的,但它却在逃避我.任何帮助都是极好的...
Java 7
List<Person> personList = Person.createShortList();
// Sort with Inner Class
Collections.sort(personList, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
return p1.getSurName().compareTo(p2.getSurName());
}
});
Run Code Online (Sandbox Code Playgroud)
Java 8
Collections.sort(personList, (Person p1, Person p2) ->
p1.getSurName().compareTo(p2.getSurName()));
for (Person p : personList) {
p.printName();
}
Run Code Online (Sandbox Code Playgroud)
如果接口Comparator有2个方法而不只是一个compare可以使用Lambda?
例如
public interface Comparator<T> {
int compare(T o1, T o2);
int compareTest(T o1, T o2);
}
Run Code Online (Sandbox Code Playgroud) java-8 ×10
java ×9
java-stream ×3
lambda ×3
optional ×2
asynchronous ×1
datetime ×1
dictionary ×1
java-threads ×1
javafx ×1
rabbitmq ×1