[英]Get index that contains “.txt” in the list without using loop in JAVA
I would like to get the first index of a string that contains a ".txt"
in the list using JAVA.我想使用 JAVA 获取列表中包含
".txt"
的字符串的第一个索引。 without doing any loop.不做任何循环。 Because I have lots of value in File like:
因为我在 File 中有很多价值,例如:
["sample.txt", "sample.csv","sample.docx","sample","sample.xlsx","sample2.txt, ...];
I only need the index of "sample.txt"我只需要“sample.txt”的索引
List<String> files = Arrays.asList(fileList).stream()
.map(x -> x.getAbsolutePath())
.collect(Collectors.toList());
index = files.indexOf(?);
Are you looking to use:您是否希望使用:
List<String> list = Arrays.asList(fileList);
String first = list.stream(fileList)
.filter(x -> x.endsWith(".txt")) // or contains if the .txt can be in the middle
.findFirst()
.orElse(null); // you can even throw and exception using orElseThrow
// to get the index you can use
int index = list.indexOf(first);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.