我有一个很长的格式:
Long l = 20151228;
我想将它解析为LocalDate对象.
我怎样才能做到这一点?
以下方法实现了一个BiFunctiona Map<String,String>和一个值来搜索.它搜索的Entry在Map包含给定值,并返回相应的键.
这个实现有效,但是我想写一个没有return语句的lambda表达式,以使代码更优雅.
private BiFunction<Map<String, String>, String, String> findName = (m, s) -> {
Map.Entry<String, String> e =
m.entrySet()
.stream()
.filter(entry -> entry.getValue() != null && !entry.getValue().isEmpty() && entry.getValue().equals(s))
.findFirst()
.orElse(null);
return e != null ? e.getKey() : null;
};
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
如何获得List<String>从List<Object[]>由数组的第一个元素转换为字符串?
我尝试写下面但是没有发生铸造,
List<String> results = query.getResultList()
.stream()
.map(result ->{
Object[] temp = (Object[]) result;
return temp[0].toString();
})
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
下图显示了结果列表的类型
我收到错误:java:不兼容的类型:java.lang.Object无法转换为java.util.List.如何使用Java 8正确执行此操作?
我有一个自定义类PDFDataItem,提供getValue()方法,该方法返回一个double值(请参阅附加的代码).我还有一个包含多个PDFDataItem实例的ArrayList,每个实例都有一个特定的值:不同的实例可以具有相同的值.我想要做的是创建一个LinkedHashMap,其中单独存储实例的值,并且对于找到的每个值,存储的数量(即它比较的实例的数量).当然,我可以找到许多技巧来实现我的目标,但我想知道是否存在快速方法(可能使用lambda).
public class PDFDataItem{
double value;
public PDFDataItem(double value){
this.value = value;
}
public double getValue(){
return value;
}
}
Run Code Online (Sandbox Code Playgroud) 我的 groupingBy 方法有问题。我举个例子:假设有一个List<Person>wherePerson.class和City.classare like this
class Person{
String name;
String surname;
City city;
}
class City{
String name;
String postalCode;
}
Run Code Online (Sandbox Code Playgroud)
我会将列表分组city.postalcode并执行如下操作:
List<Person> myList = ...some persons here...
Map<City,List<Person>> groupMap = myList.stream().collect(
Collectors.groupingBy(person -> person));
Run Code Online (Sandbox Code Playgroud)
显然这不起作用,因为groupingBy不知道如何按对象分组。我会做类似的事情Collectors.groupingBy(person -> person.city.postalcode),但是这样会产生一个类型的地图Map<String,List<Person>>,而不是 Map<City,List<Person>>
谢谢你的帮助。
更新
为了groupingBy在类上使用,它必须简单地正确实现 equals并且hashcode
我有课:
public class BusinessUnit
{
private String name;
//getter
//setter
}
Run Code Online (Sandbox Code Playgroud)
我List<Object>从服务中获得的信息需要转换为List<BusinessUnit>. 数据只是名称。但服务正在返回我对象。在 Java 8 中进行此转换的最佳方法是什么
我对使用 java 8 很陌生,以前从未真正使用过注释。当我尝试使用 eclipseoxygen、java 8 和指定注释 @FunctionalInterface 为函数式接口编写一个简单的程序时,我看到红色 x(错误)表示函数式接口不是注释类型。
请帮助解决这个问题。我知道提供注释不是强制性的,我的程序在没有注释的情况下也可以正常运行,但我仍然想使用它。
我想按键对地图对象进行分组。我尝试使用此代码,但出现编译错误:
Non-static method cannot be referenced from a static context
Run Code Online (Sandbox Code Playgroud)
我的代码:
Map<String, List<A>> getAMap() {
return Arrays.stream(SomeArray.values())
.map(map -> createObject())
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.mapping(Map.Entry::getValue, Collectors.toList())));
}
private Map<String, A> createObject()
final A a = new A(some attributes);
Map<String, A> map = new LinkedHashMap<>();
map.put(some key, a);
.... // add another values.
return map;
}
Run Code Online (Sandbox Code Playgroud)
我需要类似的东西
{
"a", {a1, a2, a3},
"b", {a4, a5, a6},
}
Run Code Online (Sandbox Code Playgroud) List<Hosting> list = new ArrayList<>();
list.add(new Hosting(1, "liquidweb.com"));
list.add(new Hosting(2, "aws.amazon.com"));
list.add(new Hosting(3, "digitalocean.com"));
list.add(new Hosting(2, "aws.amazon.com"));
Run Code Online (Sandbox Code Playgroud)
我想将上面的列表转换为Map<Integer, Set<String>>
1 -> {"liquidweb.com"}
2 -> {"aws.amazon.com"}
3 -> {"digitalocean.com"}
Run Code Online (Sandbox Code Playgroud)
如何在 Java 8 中进行转换?
我正在尝试对一些数字进行排序。我收到“java.lang.IllegalArgumentException:比较方法违反了其一般契约!” 当我执行以下代码时出现异常。
import org.apache.commons.lang3.StringUtils;
public class ComparatorTest {
public static void main(String[] args) {
List<String> ll = List.of("1.A", "1.A.1", "10.A", "10.A.1", "10.A.2", "10.A.3", "12.A", "12.A.1", "12.A.2",
"12.A.4", "12.A.6", "1A.2", "2.A.1", "2.A.1.b", "2.A.1.b.1", "2.A.1.b.2", "2.A.1.b.3", "20.A.1",
"20.A.1.a", "20.A.1.b", "20.A.1.b.1", "20.A.1.b.2", "3.A.1", "3.A.1.a", "3.A.1.a.1", "3.A.1.a.2",
"3.A.1.a.3", "3.A.1.a.4", "3.A.1.b", "3.A.10", "6.A.1", "9.A.1");
ArrayList<String> l2 = new ArrayList<>(ll);
Collections.sort(l2, (obj1, obj2) -> {
try {
String[] prodClass1 = obj1.split("\\.");
String[] prodClass2 = obj2.split("\\.");
for (int i = 0; (i < prodClass1.length) && (i < …Run Code Online (Sandbox Code Playgroud) java ×10
java-8 ×10
java-stream ×3
grouping ×2
lambda ×2
annotations ×1
collectors ×1
comparator ×1
eclipse ×1
java-time ×1
jodatime ×1
optional ×1
sorting ×1