标签: java-8

Java 8 Stream groupingby 不返回列表

有没有办法Map<Long, String>直接从groupingByJava 8 中的函数创建,而不是创建Map<Long, Set<String>>

我有以下代码:

List<Object[]> list = new ArrayList<>();
//row[0] is the primary key and row[1] is the name of the employee
Object[] input1 = { 1l, "employee1",  null};
Object[] input2 = { 1l, "employee1", "vorw2"};
Object[] input3 = { 2l, "employee2", "vorw3"};
Object[] input4 = { 3l, "employee3", "vorw1"};
list.add(input1);
list.add(input2);
list.add(input3);
list.add(input4);

//code to replaced
Map<String, Set<String>> byPK= list.stream().collect(Collectors.groupingBy(row -> row[0].longValue(), Collectors.mapping(row -> row[1].toString(),Collectors.toSet() )));
Run Code Online (Sandbox Code Playgroud)

我需要像这样的 o/p:

List ==> (1l, …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

-3
推荐指数
1
解决办法
1552
查看次数

想要将字符串列表转换为java 8中的映射

我有一个字符串列表,我需要将它放在Map中,以java 8中的键,值对的形式出现.有人帮助我.

List<string> list= [(Id=123,roll number=1,name=maddy)];
Run Code Online (Sandbox Code Playgroud)

我需要id的值作为键,值需要123.对于用逗号分隔的所有项目都是一样的.但我需要在java 8中使用它

java dictionary list java-8

-3
推荐指数
1
解决办法
75
查看次数

Java 8将字符串列表转换为映射列表

我有List<List<String>>。我想根据内部List的特定元素将其转换为Map。

我试过了

ddList.stream().flatMap(x -> x.stream()
            .collect(Collectors.toMap(Function.identity(), String::length)));
Run Code Online (Sandbox Code Playgroud)

它不起作用。这是怎么了?

java collections lambda java-8

-3
推荐指数
1
解决办法
72
查看次数

从列表列表到集合的 Java 流

我有一个List<List<String>>如何使用 Java 8 流迭代并将其存储到 Set 中?

我正在沿着这条线尝试一些东西,但我无法完全编译

List <List<String>> itemLists = ...
    Set <String> codes = itemLists.stream()
    .flatMap(itemList - > {
        items.stream()
        .collect(Collectors.toSet());
    });
Run Code Online (Sandbox Code Playgroud)

java arraylist java-8 java-stream

-3
推荐指数
2
解决办法
459
查看次数

如何在 Java Stream API 中重写`while(true)`

这个Java代码可以用Stream API重写吗?

while (true) {
    ...
    if (isTrue()) break;
}

private boolean isTrue() {
    ...
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

-3
推荐指数
1
解决办法
514
查看次数

java 8中的迭代返回列表中的最后一个值

显然我不擅长 Java 8。有人能告诉我为什么我的集合在迭代过程中没有保留我的所有值吗?如果我放置一个断点,代码会迭代但只返回最后一个值。

Set<Group> groups = new Hashset<>();
Group group = new Group();
List my = model.getValues();

list.stream().forEach(gr -> {
            group.setName(gr.getDescription());
            group.setDescription(gr.getDescription());

           groups.add(group); 
});

System.Out.Println(groups);
Run Code Online (Sandbox Code Playgroud)

java lambda list java-8 java-stream

-3
推荐指数
1
解决办法
93
查看次数

BufferedReader 不读取整个文件并且不退出循环

我有要在我的应用程序中读取的 ini 文件,但问题是它没有读取整个文件,而是停留在 while 循环中。

我的代码:

FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);

String line = br.readLine();
Properties section = null;

while(line!=null){
     if(line.startsWith("[") && line.endsWith("]")){
         section = new Properties();
         this.config.put(line.substring(1, line.length() - 1), section);
     }else{
         String key = line.split("=")[0];
         String value = line.split("=")[1];
         section.setProperty(key, value);
     }

     line = br.readLine();
     System.out.println(line);

     // To continue reading newline. 
     //if i remove this, it will not continue reading the second header
     if(line.equals("")){ 
         line = br.readLine();
     }  
}

System.out.println("Done"); // Not printing …
Run Code Online (Sandbox Code Playgroud)

java bufferedreader java-8

-3
推荐指数
1
解决办法
91
查看次数

带有 Optional 的三元运算符中不需要的空指针异常

据我了解,以下代码不应抛出,Null Pointer exception因为我正在安全地使用 Optional 接口。

但是,当我运行此代码时,它会抛出NPE.

public class Test {
        public static void main(String[] args) {
            final Integer inte = false ? 0 : Optional.ofNullable((Integer) null).orElse(null);
         }
    }
Run Code Online (Sandbox Code Playgroud)

如果我在代码中的某个地方弄错了,请告诉我并帮助我纠正相同的错误。

java optional java-8

-3
推荐指数
1
解决办法
199
查看次数

以原始顺序在数组列表中使用并行流

我有一个产品,我想用相同的原始顺序在另一个数组中填充产品,我使用了并行流,结果没有按原始顺序排序

    List<Product> products = productList.getProducts();
    
    List<ProductModelDTOV2> productModelDTOV2s = new ArrayList<>();
    
    products.parallelStream().forEach(p -> {
        try {
            ProductModelDTOV2 ProductModelDTOV2 = dtoFactoryV2.populate(p, summary);
            productModelDTOV2s.add(ProductModelDTOV2);
        } catch (GenericException e) {
            log.debug(String.format("Unable to populate Product %s", p));
        }
    });
    return productModelDTOV2s;
Run Code Online (Sandbox Code Playgroud)

java lambda java-8

-3
推荐指数
1
解决办法
68
查看次数

不兼容的类型:lambda 表达式中的错误返回类型 | void 不是函数式接口

我正在浏览 Java 8 Feature 并使用 Jshell 来探索它。我尝试运行以下命令:

Consumer<String> consumer = (str)->System.out::println;
Run Code Online (Sandbox Code Playgroud)

但它失败并出现以下错误:

Error:
|  incompatible types: bad return type in lambda expression
|      void is not a functional interface
Run Code Online (Sandbox Code Playgroud)

我不明白到底是什么问题 Consumer 接受一个论点而什么都不返回。我在这里通过传递 1 arg str 和打印来做同样的事情。那么这个语法有什么问题。

谢谢,

java lambda consumer java-8 functional-interface

-3
推荐指数
1
解决办法
100
查看次数