我有一个检查空值的方法.有没有办法减少方法中的行数?目前,代码看起来"脏":
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) 我想知道将集合分成子集的有效方法是什么?
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)
时间复杂度有什么区别?
谢谢
有没有办法检查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) 的错误代码
谢谢
我有一些关于界面使用的一般问题:
例如 :
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)
谢谢
有一个枚举值列表,我需要将它转换为由某个字符分隔的单个字符串。
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
枚举吗?
我有这个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.