简体   繁体   English

获取列表中包含“.txt”的索引而不使用 JAVA 中的循环

[英]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.

相关问题 有没有办法在不使用循环的情况下在列表中获取 object 的索引 - Is there a way to get the index of an object in a list without using a loop Java 不使用 sql 的属性索引列表 - Java list with index on attributes without using sql 使用并行流从Java列表中获取Pojo,而无需使用任何类型的索引 - Get Pojo from a java list using parallel stream without using any kind of index 在不使用任何循环的情况下获取JSTL中ArrayList的第二个索引值 - Get second index value of ArrayList in JSTL without using any loop 如何使用java在for循环中获取当前和下一个arraylist索引 - How to get current and Next arraylist index in for loop using java Java Set.contains(o)与List.get(index)时间复杂度 - Java Set.contains(o) vs. List.get(index) Time Complexity 如何从 java 中的 class object 列表中获取第一个匹配元素的索引(不使用任何第三方库) - How to get index of first matched element from the list of class object in java (Without using any third party Library) 在不使用迭代循环的情况下分配Java列表的子集 - assign subset of Java list without using iteration loop 如何在不使用 for 循环的情况下通过字符串列表过滤 java 流 - How filter java stream by a list of strings without using a for loop Java:将输入与列表中的所有内容进行比较而不使用循环? - Java: Comparing an input to everything in a list without using a loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM