要点:我们试图尽可能用java 8流和lambdas重写旧的Java代码.
问题:我有一个地图,其键是一个字符串,值是用户定义的对象列表.
Map<String, List<Person>> personMap = new HashMap<String, List<Person>>();
personMap.put("A", Arrays.asList(person1, person2, person3, person4));
personMap.put("B", Arrays.asList(person5, person6, person7, person8));
Run Code Online (Sandbox Code Playgroud)
在这里,人们根据他们的名字的起始角色进行分组.
我想在地图中找到列表中每个键的平均年龄.
同样,我尝试过stackoverflow中提到的很多东西,但没有一个与我的案例匹配,或者很少在语法上不起作用.
提前致谢!
版本1
interface HelloWorld{
String hello(String s);
}
HelloWorld h = String::new;
h.hello("Something");
Run Code Online (Sandbox Code Playgroud)
版本2
interface HelloWorld{
void hello(String s);
}
HelloWorld h = String::new;
h.hello("Something");
Run Code Online (Sandbox Code Playgroud)
版本3
interface HelloWorld{
String hello();
}
HelloWorld h = String::new;
h.hello();
Run Code Online (Sandbox Code Playgroud)
版本4
interface HelloWorld{
void hello();
}
HelloWorld h = String::new;
h.hello();
Run Code Online (Sandbox Code Playgroud)
我已经创建了相同代码的四个版本,但我没有更改HelloWorld h = String::new;
我能够理解的第一个案例,它创建了新的String of String,其值在参数中传递并返回对象.
有些人可以详细说明为什么编译器在其他情况下没有给出任何错误并有一些解释?
List<String> list =new ArrayList<>();
list.add("abcd");
list.add("afsd");
list.add("addd");
Map<String, List<Person>> ordersByInstrumentId = findService.findAllUsers(**list.get(0)**)
.stream()
.collect(Collectors.groupingBy(Dummy::getId, Collectors.toList()))
Run Code Online (Sandbox Code Playgroud)
我的方法findAllUsers将String作为参数.我想用迭代替换list.get(0),因此它将为每个元素调用函数并存储在Map中.我尝试了list.foreach,但它返回void,所以没用
如何使用数组流计算字符串中的字频?我正在使用Java 8.
这是我的代码:
String sentence = "The cat has black fur and black eyes";
String[] bites = sentence.trim().split("\\s+");
String in = "black cat";
Run Code Online (Sandbox Code Playgroud)
计算句子中的单词"black"和"cat"频率.单词"黑色"频率为2,单词"cat"为1.
那么目标输出就是3.
在dto to bean转换中,
我尝试仅在bean中找不到dto时才添加dto ...或者如果dto的id为null
我使用没有匹配的流.
当我尝试添加许多汽车时,只添加第一个
List<Car> cars = bean.getCar();
List<CarDto> carsDto = dto.getCar();
for (CarDto carDto : carsDto) {
if (cars.stream().noneMatch(e -> Objects.equals(e.getId(), carDto.getId()) || carDto.getId()==null )) {
//get car from bd....
bean.addCar(car);
}
}
Run Code Online (Sandbox Code Playgroud) 所以我有一个Map<String, Map<String, Integer>>,我想将它转换为一个 Map<Integer, Map<String, Integer>>字符串键被替换为地图中的索引(0,1,2,.....).我知道指数在地图中不准确但在我的情况下并不重要.我尝试使用AtomicInteger作为索引,在流上的每个操作后递增但我无法解决问题....
我正在使用spring-retry(带有Java 8 lambda)来重试失败的REST调用。我只想重试那些返回500错误的呼叫。但是我不能为此配置retrytemplate bean。当前,bean很简单,如下所示:
@Bean("restRetryTemplate")
public RetryTemplate retryTemplate() {
Map<Class<? extends Throwable>, Boolean> retryableExceptions= Collections.singletonMap(HttpServerErrorException.class,
Boolean.TRUE);
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(3, retryableExceptions);
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(1500); // 1.5 seconds
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(retryPolicy);
template.setBackOffPolicy(backOffPolicy);
return template;
}
Run Code Online (Sandbox Code Playgroud)
谁能帮我这个忙。提前致谢。
我的项目中有一个自定义的StringUtils类,我想返回Optional,但我注意到在Apache的String Utils中它们没有返回Optional.
一般来说,在StringUtils中返回Optional是否可以?或者这是一种矫枉过正?
我正在尝试Array#map用Java 创建一个与Javascript相同的东西.
我已经能够做到这一点
ArrayList<String> data = myList.stream().map(e -> {
return "test "+e;
}).collect(Collectors.toCollection(ArrayList::new));
Run Code Online (Sandbox Code Playgroud)
在这里,myList是最初的ArrayList,data是结果ArrayList.
但是,我发现每次这样做都非常繁琐.
所以我尝试创建一个让我的生活更轻松的通用函数:
public static ArrayList<?> map(ArrayList<?> list, Function<? super Object,?> callback){
return list.stream().map(callback).collect(Collectors.toCollection(ArrayList::new));
}
Run Code Online (Sandbox Code Playgroud)
然后调用它:
ArrayList<String> data = DEF.map(myList,e -> {
return "test "+e;
});
Run Code Online (Sandbox Code Playgroud)
但是我得到了错误
[Java] DEF类型中的方法map(ArrayList,Function)不适用于参数(List,(e) - > {})
如何编辑我的通用函数以接受我正在使用的lambda?
我有一个字符串2017-07-31T01:01:00-07:00,我试图解析它到目前为止和CST时区.当我使用Date和Java 8 ZonedDateTime解析此字符串时,我得到了不同的结果.我不知道为什么会发生这种情况以及我做错了什么.
String dateStr = "2017-07-31T01:01:00-07:00";
LocalDateTime time = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss-hh");
String[] dateArray = dateStr.split("-");
String[] timeZones = TimeZone
.getAvailableIDs(TimeZone.getTimeZone("GMT-" + dateArray[dateArray.length - 1]).getRawOffset());
format.setTimeZone(TimeZone.getTimeZone(timeZones[0]));
Date dateObj = null;
try {
dateObj = format.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
time = dateObj.toInstant().atZone(TimeZone.getTimeZone("CST").toZoneId()).toLocalDateTime();
ZonedDateTime time2 = ZonedDateTime.parse(dateStr).toInstant().atZone(TimeZone.getTimeZone("CST").toZoneId());
System.out.println(time);
System.out.println(time2.toLocalDateTime());
Run Code Online (Sandbox Code Playgroud) java ×10
java-8 ×10
java-stream ×3
lambda ×3
arrays ×1
datetime ×1
function ×1
hashmap ×1
optional ×1
parameters ×1
spring ×1
spring-boot ×1
spring-retry ×1