我有这个使用Java 8的代码片段来获取特定文件的创建日期时间:
Path path = Paths.get("D:\\SampleFile.txt");
BasicFileAttributes attributes = null;
try {
attributes = Files.readAttributes(path, BasicFileAttributes.class);
System.out.println("Creation Date Time: " + attributes.creationTime());
} catch(IOException ioe) {
ioe.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
:我使用作为例子的文件的真实创建小时从所述一个以上代码段显示不同6小时
实时日期时间:2017-02-05T 10:34:28
这一代码时间:2017-02-05T 16:34:28.247156Z
有谁知道这种差异的原因是什么以及如何获得正确的创建日期时间值?
先感谢您!
我有对象的列表objList其中有两个字段age和id.age是long和id是String.我想迭代列表并获取列表中具有最小年龄的对象的id.我可以用传统的for循环来做,但我相信在Java 8中会有一个紧凑的解决方案.
如果我这样做objList.stream(),然后检查该流的地图功能,我只是有mapToInt,mapToLong和mapToDouble.有了这些,我得到了最小年龄,但我需要具有最小年龄的物体的id.
它保证列表不会为空并且至少有一个对象.
如何使用Stream找到此行中所有整数的总和?
String line = "Test 15 lqlq 35 bad 99999 guess 34";
List<String> lineSplitted = Arrays.stream(line.split(" ")).collect(Collectors.toList());
int result = 0;
try {
result = lineSplitted.stream().filter(s -> Integer.parseInt(s)).sum();
}
catch (Exception e) {
}
System.out.println(result);
Run Code Online (Sandbox Code Playgroud) 我只是想检查一下我是否做得对.这是使用可选的正确方法还是可以改进?
String longitudeResult = "Address_Longitude is empty or null;";
String latitudeResult = "Address_Latitude is empty or null;";
if (Optional.ofNullable(location).isPresent()) {
Optional<Double> longitude = Optional.ofNullable(location.getLongitude());
Optional<Double> latitude = Optional.ofNullable(location.getLatitude());
if (longitude.isPresent()) {
longitudeResult = longitude.get().toString();
}
if (latitude.isPresent()) {
latitudeResult = latitude.get().toString();
}
}
Run Code Online (Sandbox Code Playgroud) 使用Spring RestTemplate执行HTTP POST时,出现java.net.socketException:连接重置
但是,当我在Java 8上执行相同的代码时,相同的代码可以正常工作。我没有发现与此相关的任何类似问题。
有人可以指出我可能做错的任何事情吗?我什至尝试使用-Dhttps.protocols = TLSv1.1,TLSv1.2之类的命令行参数
到目前为止没有任何工作。任何援助将不胜感激
这是我的代码:
String xmlResponse = null;
if (restTemplate == null) {
CloseableHttpClient httpClient =
HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
HttpComponentsClientHttpRequestFactory requestFactory = new
HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
restTemplate = new RestTemplate(requestFactory);
}
HttpHeaders rqHeaders = new HttpHeaders();
rqHeaders.setContentType(MediaType.TEXT_XML);
HttpEntity<String> createAppRequest = new HttpEntity<>(xmlRequest, rqHeaders);
ResponseEntity<String> responseEntity = restTemplate.exchange(datasafeURL, HttpMethod.POST, createAppRequest, String.class);
Run Code Online (Sandbox Code Playgroud)
结果:
2017-06-29 14:23:58.278 INFO 1125 --- [nio-9902-exec-3] o.apache.http.impl.execchain.RetryExec : I/O exception (java.net.SocketException) caught when processing request to {s}->https://pcu.users.com:443: Connection reset
2017-06-29 14:23:58.278 INFO 1125 --- [nio-9902-exec-3] …Run Code Online (Sandbox Code Playgroud) 我有一个包含字符串和字符串列表的流,我想把所有值作为字符串列表.无论如何用一些流操作来做到这一点?
Stream stream = Stream.of("v1", Arrays.asList("v2, v3"));
Run Code Online (Sandbox Code Playgroud) 我已经创建了一个基于Bean属性动态构建rest URI的方法,最初我必须将它重构为函数式,这是我第一次进行函数式编程.命令式和功能性都按预期工作,但我对功能可读性,功能性接缝以及此方法的过度杀戮感到不满意,或者可能是因为我仍然是新手功能程序员!
您如何将此方法重构为更清晰的功能方式?
或者你会保持它势在必行吗?
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.lang.reflect.Method;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.util.UriComponentsBuilder;
public String functionalBuildRestUri() throws Exception {
final UriComponentsBuilder uriBuilder = UriComponentsBuilder.newInstance().scheme("https")
.host("foo.com").path("/offers");
//here is the functional
List<PropertyDescriptor> propDescList = Arrays.asList(BeanUtils.getPropertyDescriptors(getClass()));
//this part is readable and precis, but to enable it had to add 4 methods
propDescList.stream().filter(notClassProp())
.filter(notNullPropValue())
.collect(Collectors.toMap(PropertyDescriptor::getName, propValue()))//conversion to map doesn't feel good to me how can …Run Code Online (Sandbox Code Playgroud) refactoring functional-programming naming-conventions java-8
我是Java 8的新手,试图了解Java8的splitIterator功能。
我已经编写了以下代码,我的要求是每当我调用get()时;get方法应该从itr3返回一个值;是否有可能得到相同的?如何?
public class TestSplitIterator {
static List<Integer> list = new ArrayList<Integer>();
public static void main(String args[]) {
for (int i = 0; i < 100; i++) {
list.add(i);
}
// below method call should return only one value whenever i call it;
get(list);
}
private static int get(List<Integer> list) {
Collections.sort(list, Collections.reverseOrder());
System.out.println(list);
Spliterator<Integer> itr1 = list.spliterator();
Spliterator<Integer> itr2 = itr1.trySplit();
Spliterator<Integer> itr3 = itr2.trySplit();
// i want to return value from itr3 whenever get(List list ic called) …Run Code Online (Sandbox Code Playgroud) 我有一个对象流,它根据它们拥有的id定义equals和hashcode.所以我知道使用stream.distinct()我会得到一个没有任何重复的流,但它会保留第一个外观,而不是最后一个.
这在文档中明确说明:
对于有序流,不同元素的选择是稳定的*(对于重复元素,将保留遇到*顺序中首先出现的元素.)
那么我怎么能有一个没有重复的流,但保持最后的外观呢?
例如,如果我有这个项目列表1,2,3,1,4,5.
如果我申请,distinct()我会得到1,2,3,4,5,我期待:2,3,1,4,5.我怎样才能做到这一点?
为什么方法引用不在此示例中启动线程?
package example;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ESMethodReference {
int i1, i2, result = 0;
ESMethodReference(int i1, int i2){
this.i1 =i1;
this.i2 = i2;
}
public Runnable calculate(){
System.out.print("In calculate()");
return new Runnable() {
@Override
public void run() {
result += i1 + i2;
System.out.print(" creating result");
}
};
}
public static void main(String[] args) throws InterruptedException{
ESMethodReference es = new ESMethodReference(1, 1);
ExecutorService executorService = Executors.newSingleThreadExecutor();
for (int i = 0; i < 2; i++){
executorService.submit(es.calculate());
Thread.sleep(100); …Run Code Online (Sandbox Code Playgroud) java-8 ×10
java ×8
java-stream ×4
java-7 ×1
lambda ×1
optional ×1
refactoring ×1
resttemplate ×1
ssl ×1