Java:转换列表<;列表<;字符串>&燃气轮机;进入列表<;列表<;双倍>&燃气轮机;

Java:转换列表<;列表<;字符串>&燃气轮机;进入列表<;列表<;双倍>&燃气轮机;,java,collections,Java,Collections,我有一个来自Filereader(字符串)的列表,如何将其转换为列表(Double):我必须返回行数组的第一个值的列表。谢谢 private List<List<String>> stoxxFileReader(String file, int column) throws IOException { List<List<String>> allLines = new ArrayList<>(); t

我有一个来自Filereader(字符串)的列表,如何将其转换为列表(Double):我必须返回行数组的第一个值的列表。谢谢

 private List<List<String>> stoxxFileReader(String file, int column) throws IOException {

        List<List<String>> allLines = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(file))) {

          br.readLine();
          String line = null;

          while ((line = br.readLine()) != null) {
                  String[] values = line.split(",");  

            allLines.add(Arrays.asList(values));     

        }   
    }
        return allLines;
私有列表stoxxFileReader(字符串文件,int列)引发IOException{
List allLines=new ArrayList();
try(BufferedReader br=new BufferedReader(new FileReader(file))){
br.readLine();
字符串行=null;
而((line=br.readLine())!=null){
字符串[]值=行。拆分(“,”);
add(Arrays.asList(values));
}   
}
返回所有行;

您可以使用下面的方法将所有字符串列表转换为双精度字符串

public static <T, U> List<U> convertStringListTodoubleList(List<T> listOfString, Function<T, U> function) 
   { 
       return listOfString.stream() 
           .map(function) 
           .collect(Collectors.toList()); 
   }
公共静态列表转换器StringListToDoubleList(List listOfString,函数)
{ 
返回listOfString.stream()
.map(函数)
.collect(Collectors.toList());
}
调用方法

List<Double> listOfDouble = convertStringListTodoubleList(listOfString,Double::parseDouble);
List listOfDouble=convertstringlistodoubleList(listOfString,Double::parseDouble);

java.util.stream.collector有一个方便的方法来实现这一点。请参考下面的代码片段。您可以用逻辑替换

    Map<String, Integer> map = list.stream().collect(
            Collectors.toMap(kv -> kv.getKey(), kv -> kv.getValue())); 
Map Map=list.stream().collect(
Collectors.toMap(kv->kv.getKey(),kv->kv.getValue());

我不确定您想要得到什么样的输出,但在任何情况下都没有那么复杂。假设您有这样的输出:

[["1", "2"],
 ["3", "4"]]
如果要将所有第一个元素作为Double
[1,3]
(假设第一个元素始终存在):


非常感谢您的回答!为什么我有时会在论坛中阅读列表?我认为类型变量必须始终是对象,double是一种基本类型???
List
我不相信它是有效的,您能举个例子吗?我应该指出,Java中有一个
double
和一个
double
,与
Int
整数
        List<Double> firstAsDouble = allLines.stream()
            .map(line -> line.get(0))
            .map(Double::parseDouble)
            .collect(Collectors.toList());
        List<List<Double>> matrix = allLines.stream()
            .map(line -> line.stream().map(Double::parseDouble).collect(Collectors.toList()))
            .collect(Collectors.toList());
        List<Double> flatArray = allLines.stream()
            .flatMap(line -> line.stream().map(Double::parseDouble))
            .collect(Collectors.toList());