小编Nic*_*s K的帖子

lambda从map中检索整数

我试图在使用Lambda表达式进行计算后得到数字,但得到错误.

我正在使用的lambda表达式:

int num = Optional.ofNullable(list.stream().filter(x->x.getType().getTypeId()==Type.getTypeId()).limit(1).map(x->x.getNum())).get();
Run Code Online (Sandbox Code Playgroud)

过滤后,我想获得第一个检索到的值.但我得到的错误是

cannot convert from Stream<Integer> to int
Run Code Online (Sandbox Code Playgroud)

所以,目前我使用的方式是

Optional<> li = list.stream().filter(x->x.getType().getTypeId()==Type.getTypeId()).findFirst();
if (li.isPresent()) {
    num = li.map(x-> x.getNum()).get();
}
Run Code Online (Sandbox Code Playgroud)

但是,我正在寻找上述是否可以在一行而不是额外的if声明

早些时候,我试图get()findFirst(),但它给人nullpointerException.如何安全地检索该值.

java lambda dictionary java-8

2
推荐指数
1
解决办法
40
查看次数

Java递归方法找到阶乘返回负输出

我知道它是溢出但事情是20是相对较小的数字,这不应该发生吗?有没有更好的方法来找到像1000这样的大数的阶乘而没有得到这个奇怪的结果?

public class RecursiveFunctionsExamples {

public int factorial(Integer n)
{
    Integer res;
    if(n == 0){ 
        res = 1;
    }else{
       res =  n * factorial(n-1);
    }

    return res;
}


public static void main(String[] args) {
    System.out.println(new RecursiveFunctionsExamples().factorial(20));
}
}
Run Code Online (Sandbox Code Playgroud)

java algorithm recursion

2
推荐指数
1
解决办法
408
查看次数

如何使用另一个列表中的Map属性创建一个新列表

想象一下,我有一个List<Map<String,Object>>:

[{'id':1,'name':'xyz'},{'id':2,'name':'abc'},{'id':3,'name':'pqr'}]
Run Code Online (Sandbox Code Playgroud)

我需要生成另一个列表,包括上面列表中的名称:

List<String>
Run Code Online (Sandbox Code Playgroud)

避免使用循环,是否可以通过使用java stream api来实现这一点?

java dictionary list java-8 java-stream

2
推荐指数
1
解决办法
101
查看次数

春季日期格式

我有一个棱角分明的应用程序,它向我发送了类似dd / MM / yyyy的日期。我想将此日期插入数据库中。这是我的实体

@Entity
public class Individu implements Serializable {

    @Id
    private String nui;
    private int civility; 
    private String lastName;
    private String useName;
    private String firstName;
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern="dd/MM/yyyy")
    private Date birthDate;
Run Code Online (Sandbox Code Playgroud)

但是当我运行SpringBootApp时,我总是会遇到此错误:

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "20/02/1990": not a valid representation (error: Failed to parse Date value '20/02/1990': Cannot parse date "20/02/1990": not compatible with any of standard forms ("yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS", "EEE, dd MMM yyyy HH:mm:ss zzz", "yyyy-MM-dd")); 
Run Code Online (Sandbox Code Playgroud)

有人对我有解决方案吗?

java spring datetime spring-boot

2
推荐指数
1
解决办法
3343
查看次数

重构方法使用泛型

起初我写道:

private ArrayList<Integer> getDataList() {
    ArrayList<Integer> dataList = new ArrayList<>(LEN);
    for (int i = 0; i < LEN; i++)
        dataList.add(i);
    Collections.shuffle(dataList);
    return dataList;
}
Run Code Online (Sandbox Code Playgroud)

后来我决定使用泛型:

private <E> ArrayList<E> getDataList() {
    ArrayList<E> dataList = new ArrayList<>(LEN);
    for (int i = 0; i < LEN; i++)
        dataList.add(/* a procedure that generates E instance from index i*/);
    Collections.shuffle(dataList);
    return dataList;
}
Run Code Online (Sandbox Code Playgroud)

接口中的静态方法不能覆盖,因此无法在E上调用静态方法来生成实例.

如何重构这个使用泛型?谢谢.

java generics

2
推荐指数
1
解决办法
66
查看次数

java - 为什么这个递归方法超出了我的预期

做一个非常简单的程序来测试递归.程序打印一些东西,直到数字不大于0.

public class TestIt {

public static void print(int f){
    while(f>0){
        System.out.println("Not today");
        f--;
        print(f);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码是从主程序中调用的.

public static void main(String[] args) {
  TestIt.print(2);
}
Run Code Online (Sandbox Code Playgroud)

也许我终于失去了理智,但程序打印的次数超出了我的预期.如果我向方法发送3,则程序打印7次.任何想法为什么?

java recursion

2
推荐指数
1
解决办法
54
查看次数

无法将布尔字段显式设置为 null

我有这个:

  public static class RaceParam {

    boolean keep = null; // does not compile

  }
Run Code Online (Sandbox Code Playgroud)

这将无法编译,因为您不能将 null 分配给原语。所以这就引出了一个问题,当我们这样做时,会得到什么价值:

  public static class RaceParam {

    boolean keep;

  }
Run Code Online (Sandbox Code Playgroud)

它默认为 false 还是 true?默认某事似乎很危险。

java boolean

2
推荐指数
1
解决办法
1949
查看次数

Getting infinite loop when reading file from method

I am trying to refactor my code, and adding methods where possible. When I read a file from a method and return the result of the computation, the code goes into extreme memory consumption, infinite loop.

My modification looks like this:

import java.util.Scanner;

public class NumberOfLines {
    public static int compute () {
        // read this file
        String theFile = "numbers.txt";

        Scanner fileRead = null;

        if (NumberOfLines.class.getResourceAsStream(theFile) != null) {
            fileRead = new Scanner(NumberOfLines.class.getResourceAsStream(theFile));
        }           
        else {
            System.out.print("The file " …
Run Code Online (Sandbox Code Playgroud)

java methods file infinite-loop java.util.scanner

2
推荐指数
1
解决办法
42
查看次数

修改 Angular 服务中的 api 响应

我正在尝试修改 API 请求返回的响应。现在我得到的答复是

\n\n
[\n   {\n       name: "Afghanistan"\n   }, \n   {\n       name: "\xc3\x85land Islands"\n   }\n]\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想修改为:

\n\n
[\n   {\n        name: "Afghanistan",\n        name1: "Afghanistan",\n        name2: "Afghanistan"\n    },\n    {\n        name: "\xc3\x85land Islands",\n        name1: "\xc3\x85land Islands",\n        name2: "\xc3\x85land Islands"\n    }\n]\n
Run Code Online (Sandbox Code Playgroud)\n\n

我正在尝试复制名称字段并创建新字段,例如:同一对象中的 name1、name2 。这是工作项目https://stackblitz.com/edit/angular-8bzcdp \n任何人都可以帮忙

\n

rxjs typescript angular

2
推荐指数
1
解决办法
4149
查看次数

自定义指令在角度模块内不起作用?

在我的应用程序中,我有两个模块。IdentityModule and ServiceModule。它们以延迟加载技术加载。

现在我创建了一个IconSpacerDirective与 绑定的自定义指令app.module.ts。它在我的内部运行良好app.component.ts

但它在IdentityModule. 我遇到这个错误:

ERROR Error: Uncaught (in promise): Error: Type IconSpacerDirective is part of the declarations of 2 modules: AppModule and IdentityRegistryModule! Please consider moving IconSpacerDirective to a higher module that imports AppModule and IdentityRegistryModule. You can also create a new NgModule that exports and includes IconSpacerDirective then import that NgModule in AppModule and IdentityRegistryModule.
Error: Type IconSpacerDirective is part of the declarations of 2 modules: …
Run Code Online (Sandbox Code Playgroud)

typescript angular-directive angular-module angular

2
推荐指数
1
解决办法
4320
查看次数