小编use*_*985的帖子

Java的简短形式如果Else声明

我有一个检查空值的方法.有没有办法减少方法中的行数?目前,代码看起来"脏":

private int similarityCount (String one, String two) {

    if (one == null && two == null) {
        return 1;
    } else if (one == null && two != null) {
        return 2;
    } else if (one != null && two == null) {
        return 3;
    } else {
        if(isMatch(one, two))
             return 4;
        return 5;
    }

}
Run Code Online (Sandbox Code Playgroud)

java if-statement short ternary-operator

28
推荐指数
6
解决办法
5361
查看次数

使用 Lists.partition 或 Iterable.partition 将集合拆分为子集

我想知道将集合分成子集的有效方法是什么?

Iterable<List<Long>> partitions = Iterables.partition(numbers, 10);
Run Code Online (Sandbox Code Playgroud)

或者

List<List<Long>> partitions = Lists.partition(numbers, 10);
Run Code Online (Sandbox Code Playgroud)

时间复杂度有什么区别?

谢谢

java iterable list set partition

6
推荐指数
1
解决办法
9830
查看次数

从可为空列表创建 Java 8 流

有没有办法检查java8中的null,如果list为null则返回null,否则执行操作。

 public Builder withColors(List<String> colors) {
        this.colors= colors== null ? null :
                colors.stream()
                .filter(Objects::nonNull)
                .map(color-> Color.valueOf(color))
                .collect(Collectors.toList());

        return this;
    }
Run Code Online (Sandbox Code Playgroud)

我看到有一个选项可以使用

Optional.ofNullable(list).map(List::stream) 
Run Code Online (Sandbox Code Playgroud)

但这样我就得到了 Color.valueOf(color) 的错误代码

谢谢

java java-8 java-stream

6
推荐指数
1
解决办法
6489
查看次数

Java接口应该只包含getter吗?

我有一些关于界面使用的一般问题:

  1. 为每个对象类创建接口有什么好处?
  2. 界面应该只包含'getter'方法吗?
  3. 为什么不也是二传手呢?
  4. 为什么我要为每个对象类创建一个接口?它会在JUnit测试中为我服务吗?

例如 :

    public interface Animal {
      public getVoice();
      public String getName();
    }

public class Dog implements Animal {
   private String name;

   public getVoice(){
      System.out.println("Brrr");
   }
   public String getName(){
      return this.name;
   }
   public void setName(String name){
      this.name = name;
  }

}
Run Code Online (Sandbox Code Playgroud)

谢谢

java junit interface

5
推荐指数
1
解决办法
768
查看次数

Java将枚举列表连接到字符串

有一个枚举值列表,我需要将它转换为由某个字符分隔的单个字符串。

enum Level {
  LOW,
  MEDIUM,
  HIGH
}

Set<Level> levels = new HashSet<>(Arrays.asList(Level.LOW, Level.HIGH));
Run Code Online (Sandbox Code Playgroud)

预期结果:

String result = "LOW, HIGH"
Run Code Online (Sandbox Code Playgroud)

String.join枚举吗?

java enums concatenation

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

java重构if else

我有这个if else代码,我想知道是否有更多有用/智能的方式来编写它:

public void saveContent() throws Exception {
   if(book.isColored()) {
      book.setChoosen(“1234”);
   } else if (book.isAvailable()) {
      book.setChosen(“23498”);
   } else if (book.isAdults()) {
      book.setChosen(“0562”);
   } else {
      ReaderResponse response = reader.getReaderResponse();
      if (response != null) {
         book.setChosen(response.getName());
         }
      } else {
            book.setChosen(“4587”);
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

该方法返回void.

java if-statement coding-efficiency

0
推荐指数
1
解决办法
117
查看次数