在Java中,如何使用列表中的列表来映射和获取类成员列表。
public class CustomerSales {
public List<Product> productList;
....
}
public class Product {
public List<ProductSubItem> productSubItemList
....
}
public class ProductSubItem {
public String itemName;
Run Code Online (Sandbox Code Playgroud)
试图:
然而,这并没有得到itemName
。我正在寻找一种干净有效的方法,理想情况下可能想尝试 4-5 级深度,但为了简单起见,问题只有 3 级,等等
List<String> itemNameList = customerSales.productList.stream()
.map(p -> p.productSubItemList())
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我正在使用 Java 8。
尝试使用此资源:仍然不走运,如何使用 Java 8 Stream 从某些类属性中获取列表?
我想将 a 转换Map<String,List<String>>
为Map<String,Set<String>>
优化搜索。我想出了下面的传统方法。
for (Map.Entry<String, List<String>> entry : this.mapWithList.entrySet()) {
Set<String> hSet = new HashSet<>(entry.getValue());
this.mapWithSet.put(entry.getKey(), hSet);
}
Run Code Online (Sandbox Code Playgroud)
forEach
我想知道如何在Java 8中使用它。
另外,使用forEach
lambda 后,代码性能会更好吗?
我想做的是,如果存在客户,则更新客户,但如果没有客户,则抛出异常。但我找不到正确的流函数来做到这一点。我怎样才能做到这一点?
public Customer update(Customer customer) throws Exception {
Optional<Customer> customerToUpdate = customerRepository.findById(customer.getId());
customerToUpdate.ifPresentOrElse(value -> return customerRepository.save(customer),
throw new Exception("customer not found"));
}
Run Code Online (Sandbox Code Playgroud)
我无法返回来自保存函数的值,因为它说它是 void 方法并且不期望返回值。
我正在尝试过滤实现接口的对象列表。我正在尝试为所有类创建一个通用方法。
就像是:
interface SomeInterface {
String getFlag();
}
class SomeObject implements SomeInterface {
public String getFlag() {
return "X";
}
}
List<SomeObject> someObjectList = new ArrayList<>();
// Compilation error here
List<SomeObject> filterList = filterGenericList(someObjectList, "X");
private List<?> filterGenericList(List<? extends SomeInterface> objects, String flag) {
return objects.stream()
.filter(it -> it.getFlag().equals(flag))
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
如何避免编译错误?
Incompatible types.
Found: 'java.util.List<capture<?>>',
Required: 'java.util.List<SomeObject>'
Run Code Online (Sandbox Code Playgroud) 我有一个字符串流:
Stream<String> st = Arrays.stream(new String[]{"10", "20", "30", "40", "50"});
Run Code Online (Sandbox Code Playgroud)
我想将它们转换为整数并使用该方法对它们进行求和reduce
。我使用以下格式的reduce:
int result = st.reduce(0, (a, b) -> a + Integer.parseInt(b));
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
no suitable method found for reduce(int, (a, b) -> a + [...]nt(b))
Run Code Online (Sandbox Code Playgroud)
有关如何重写reduce 方法以使其工作的任何提示吗?
我有这样的代码,它应该Map
从整数数组创建一个。键代表位数。
public static Map<Integer, List<String>> groupByDigitNumbersArray(int[] x) {
return Arrays.stream(x) // array to stream
.filter(n -> n >= 0) // filter negative numbers
.collect(Collectors.groupingBy(n -> Integer.toString((Integer) n).length(), // group by number of digits
Collectors.mapping(d -> (d % 2 == 0 ? "e" : "o") + d,
Collectors.toList()))); // if even e odd o add to list
}
Run Code Online (Sandbox Code Playgroud)
问题与 一致mapping()
。我收到错误:
public static Map<Integer, List<String>> groupByDigitNumbersArray(int[] x) {
return Arrays.stream(x) // array to stream
.filter(n …
Run Code Online (Sandbox Code Playgroud) 我有两个已排序的整数数组。我希望将它们合并到一个排序数组中。我想使用 Java Stream 来实现这一点。
我可以做一个嵌套流吗?
Arrays.asList(nums1).stream()
.forEach(i -> Arrays.asList(nums2).stream()
.forEach(j -> //compare i,j)
.collect as list// ;
Run Code Online (Sandbox Code Playgroud)
例如,[1,3,4]
并且[2,5]
应该返回[1,2,3,4,5]
我有一个嵌套列表,outputToStore
其内容如下所示:
[[Final, 331, M, 22/03/2020 00:00:00],
[Initial, 335, M, 22/06/2022 00:00:00],
[Exception, 335, M, 22/05/2022 00:00:00],
[Final, 335, M, 20/06/2022 00:00:00],
[Keep, 335, M, 02/06/2022 11:00:00],
[Final, 335, M, 10/04/2022 02:00:00],
[Deleted, 335, M, 22/06/2022 15:55:10],
[Exception, 335, M, 22/06/2022 15:55:09],
[Final, 335, M, 22/06/2022 15:56:00],
[Initial, 335, M, 11/06/2022 00:00:00]]
Run Code Online (Sandbox Code Playgroud)
我需要根据两个条件对此进行排序:第一个是自定义顺序:“Initial”、“Final”、“Deleted”、“Keep”、“Exception”,然后基于datetime。
我能够做到这一点,但不确定这是最好的方法。
我的代码:
List<String> definedOrder = Arrays.asList("Initial","Final","Deleted","Keep","Exception");
Collections.sort(outputToStore, Comparator.comparing(o -> Integer.valueOf(definedOrder.indexOf(o.get(0)))));
Collections.sort(outputToStore,( o1, o2)-> {
// let your comparator look up …
Run Code Online (Sandbox Code Playgroud) 我尝试执行以下操作,但它不起作用
Optional<List<Integer>> listOne = someAPI.call()
// convert
Optional<ArrayList<Integer>> listTwo = new ArrayList<>(listOne); // doesn't work
Optional<ArrayList<Integer>> listTwo = Optional.of(new ArrayList(listOne)); // also not working
Run Code Online (Sandbox Code Playgroud)
请注意,第一个 API 返回Optional<List>
. 我需要发送Optional<ArrayList>
到另一个 API。
我正在尝试编写一个名为 Range 的类,它采用一个length 的整数数组(未排序),仅包含从到n
范围内的数字。0
k
首先,我声明一个构造函数,它将通过计数排序算法预处理数组。
然后我想编写query()
接受两个整数参数的方法:a
和,它形成从到 的b
数字范围,并返回数组中具有给定范围内的值的所有元素的总频率。a
b
我的代码:
import java.util.Arrays;
import java.util.HashMap;
public class Range {
private int[] a;
private int k;
public Range(int[] a, int k) {
int index = 0;
int[] counterArray = new int[k + 1];
for (int i : a)
counterArray[i]++; // initialize counterArray
for (int i = 0; i < counterArray.length; i++)
while (0 < counterArray[i]) {
a[index++] …
Run Code Online (Sandbox Code Playgroud) java ×10
java-stream ×6
list ×4
arrays ×3
sorting ×3
algorithm ×2
hashmap ×2
java-8 ×2
option-type ×2
arraylist ×1
collectors ×1
comparator ×1
exception ×1
generics ×1
lambda ×1
nested-lists ×1
spring ×1
spring-boot ×1
string ×1