如何从给定的 List<String>
中获取包含更多小写字符的字符串?
我已经使用 Java 10 Streams 以函数式方式完成了此操作,但我想使用 while
以迭代方式完成此操作。循环。但我不知道该怎么做。
这是我的功能代码。
public static Optional<String> stringSearched (List<String> pl) {
return pl.stream()
.max(Comparator.comparing(x->x.chars()
.filter(Character::iLowerCase)
.count()
));
}
最佳答案
import java.util.ArrayList;
import java.util.List;
public class Something {
public static void main(String []args){
ArrayList<String> list = new ArrayList<String>();
list.add("LOSER");
list.add("wInner");
list.add("sECONDpLACE");
System.out.println(findMoreLowerCase(list));
}
private static String findMoreLowerCase(final List<String> list){
String moreLowerCase = ""; // Tracks the current string with less lowercase
int nrOfLowerCase=0; // Tracks the nr of lowercase for that string
int current; // Used in the for loop
for(String word: list){
current = countLowerCase(word);
if(current > nrOfLowerCase){
// Found a string with more lowercase, update
nrOfLowerCase = current;
moreLowerCase = word;
}
}
return moreLowerCase;
}
private static int countLowerCase(String s){
// Scroll each character and count
int result = 0;
for (char letter : s.toCharArray()) {
if (Character.isLowerCase(letter)){
++result;
};
}
return result;
}
}
关于java - 如何从给定列表中获取包含更多小写字符的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53224385/