简体   繁体   English

使用 java 中的 Stream 将列表字符串转换为 ArrayList 浮点数

[英]Convert a List String to an ArrayList Float with Stream in java 8

Good afternoon, Community!下午好,社区!

I have a List:我有一个清单:

List<String> rate = new ArrayList<>(); 

and I need to convert the data into a float if it can be done with java 8 stream.如果可以使用 java 8 stream 完成,我需要将数据转换为浮点数。 I was doing it in the following way:我是通过以下方式进行的:

float valueRate = Float.parseFloat(rate);

Try it like this:试试这样:

  • Given a list of strings floating point values.给定一个字符串浮点值列表。
  • map them to a stream of float using Float.valueOf() map 使用Float.valueOf()将它们转换为float的 stream
  • and collect into a List.并收集到一个列表中。
List<String> list = List.of("1.2", "3.4", "2.5f");
List<Float> floats = list.stream().map(Float::valueOf).collect(Collectors.toList());
    
System.out.println(floats);

Prints印刷

[1.2, 3.4, 2.5]

You can use Stream#map with Float.valueOf (to avoid autoboxing).您可以将Stream#mapFloat.valueOf一起使用(以避免自动装箱)。

List<Float> res = rate.stream().map(Float::valueOf).collect(Collectors.toList());

With Java 16:用 Java 16:

List<Float> res = rate.stream().map(Float::valueOf).toList();

如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string> - How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java转换Arraylist <Float> 漂浮[] - Java convert Arraylist<Float> to float[] 转换 ArrayList <arraylist<string> > 列出<list<string> > - Java </list<string></arraylist<string> - Convert ArrayList<ArrayList<String>> to List<List<String>> - Java 如何转换ArrayList <ArrayList<String> &gt;列表 <List<String> &gt;在Java? - How to convert an ArrayList<ArrayList<String>> to List<List<String>> in Java? 何将Arraylist转换 <Person> 到地图 <String, List<Long> &gt;使用流? - Ho to convert an Arraylist<Person> to Map<String, List<Long>> with Stream? 将字符串转换为ArrayList <Integer> 与流 - Convert String into ArrayList<Integer> with stream 如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string> - How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream 将字符串转换为 ArrayList<string> 在 Java</string> - Convert String to ArrayList<String> in Java 将String []转换为Arraylist <String> 用java - convert a String[] to Arraylist<String> with java Java转换ArrayList <String[]> to String [] [] - Java convert ArrayList<String[]> to String[][] 在Java中将Stream转换为String - Convert Stream to String in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM