给定具有字符串类型值的映射,这些值都是用逗号分隔的数字,是否可以将每个值转换为双精度?

问题描述 投票:0回答:2

最近我问有关使用Gson将Json转换为可以对值进行排序的某种东西,最好的选择是使用Linked HashMap。

List<String> stringList = Arrays.asList(tm.split("  |,")); // split into pair key : value
    Map<String, List<String>> mapString = new LinkedHashMap<>();
    stringList.forEach(s1 -> {
                String[] splitedStrings = s1.split(": "); //split into key : value
                String key = splitedStrings[0].replaceAll("[^A-Za-z0-9]",""); // remove non alphanumeric from key, like {
                String value = splitedStrings[1];
                if (mapString.get(key) == null) {
                    List<String> values = new ArrayList<>();
                    values.add(value);
                    mapString.put(key, values);
                }else if (mapString.get(key) != null) {
                    mapString.get(key).add(value);
                }
            });

运行此代码时,将创建一个包含频率,幅度和数据其他属性的键的映射。这是原始的Json Message与相同数据集的结果映射值相比(经过格式化以使其更易于理解和看起来更好)]

 {"groupId":"id3_x_","timestamp":1.591712740507E9,"tones": 
   [{"frequency":1.074,"level":3.455,"bw":0.34,"snr":3.94,"median":0.877}, 
   {"frequency":14.453,"level":2.656,"bw":0.391,"snr":2.324,"median":1.143}, 
   {"frequency":24.902,"level":0.269,"bw":0.282,"snr":2.216,"median":0.121}, 
   {"frequency":22.607,"level":0.375,"bw":0.424,"snr":2.034,"median":0.184}, 
   {"frequency":9.863,"level":2.642,"bw":0.423,"snr":1.92,"median":1.376}]}

要映射值:

Message Received  
Group ID:         id3_x_
Message Topic:    pi7/digest/tonals
Time of Arrival:  1.591712740507E9
---------------DATA---------------
Frequency: [1.07, 14.45, 24.90, 22.61, 9.86]
Magnitude: [3.46, 2.66, 0.27, 0.38, 2.64]
Bandwidth: [0.34, 0.39, 0.28, 0.42, 0.42]
SNR: [3.94, 2.32, 2.22, 2.03, 1.92]
Median: [0.88, 1.14, 0.12, 0.18, 1.38]]

虽然这对于分析数据非常有用,但存储的信息是字符串。我想做的是将映射中的每个值(例如:频率1.07、14.45等)转换为双精度值,然后我可以运行其他程序并进行计算,例如平均值。我在网上环顾四周,没有发现任何我要找的东西,所以我想知道是否有办法使用数组,列表或任何其他方式将这些字符串转换为double。

我是一家技术公司的实习生,所以我仍在尝试介绍Java并描述我在说什么,因此,如果对我的要求有任何疑问,请让我知道并提前感谢!

java string list hashmap double
2个回答
0
投票

[您可以从JSON文件中获取Map,还可以从Map yourmap.getvalues()中提取values数组,然后可以解析这些元素的每一个并将其大小写为double,


示例:频率:[1.07、14.45、24.90、22.61、9.86]

for ( String f : Frequency ) {
    double f_double = Double.parse(f); // turns String into double
    }

0
投票

您可以使用另一个将重复属性值存储在数组中的类来执行此操作。您可以简单地通过a.getValues()获得它们。这只是一个概念,您应该对其进行扩展,因为它将为您带来方便。

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Map<String, List<Attribute>> map = new LinkedHashMap<>();

        List<Attribute> attributes = new ArrayList<>();
        attributes.add(new Attribute("frequency", 3.46, 5.11, 6.12));
        attributes.add(new Attribute("magnitude", 3.46, 10.22, 10.54));
        //and so on

        map.put("idString1", attributes);

        //printing double values
        for (String key : map.keySet()) {

            for (Attribute a : map.get(key)) {
                System.out.println(a.getName() + " " +Arrays.toString(a.getValues()));
                //a.getValues() to get all of doubles
            }
        }
    }

    private static class Attribute {

        private String name;
        private double[] values;

        Attribute(String name, double... values) {
            this.name = name;
            this.values = values;
        }

        String getName() {
            return name;
        }

        double[] getValues() {
            return values;
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.