Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取包含";的索引;。txt";在JAVA中不使用循环的列表中_Java_Arrays_List_Java Stream_Indexof - Fatal编程技术网

获取包含";的索引;。txt";在JAVA中不使用循环的列表中

获取包含";的索引;。txt";在JAVA中不使用循环的列表中,java,arrays,list,java-stream,indexof,Java,Arrays,List,Java Stream,Indexof,我想使用JAVA获取列表中包含“.txt”的字符串的第一个索引。不做任何循环。因为我在文件中有很多价值,比如: ["sample.txt", "sample.csv","sample.docx","sample","sample.xlsx","sample2.txt, ...]; 我只需要的索引“sample.txt” List files=Arrays.asList(fileList

我想使用JAVA获取列表中包含
“.txt”
的字符串的第一个索引。不做任何循环。因为我在文件中有很多价值,比如:

["sample.txt", "sample.csv","sample.docx","sample","sample.xlsx","sample2.txt, ...];
我只需要的索引“sample.txt”

List files=Arrays.asList(fileList).stream()
.map(x->x.getAbsolutePath())
.collect(Collectors.toList());
index=files.indexOf(?);

您是否希望使用:

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);
List List=Arrays.asList(fileList);
String first=list.stream(文件列表)
筛选器(x> > x.EnthsAuthor(“.txt”)/或包含如果txt可以在中间。
.findFirst()
.orElse(空);//您甚至可以使用OrelsThrow抛出和异常
//要获取索引,可以使用
int index=list.indexOf(第一个);

既然您已经在使用streams,为什么不尝试使用filter和findFirst?文件列表的类型是什么?这就是我要找的。非常感谢你!
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);