在列表/哈希图中对数据进行分组



我有一个这样的文件

Petal_Length    0   1.3 - 2.42
Petal_Length    1   2.42 - 3.54
Petal_Length    2   3.54 - 4.66
Petal_Length    3   4.66 - 5.78
Petal_Length    4   5.78 - 6.9
Petal_Width     5   0.3 - 0.76
Petal_Width     6   0.76 - 1.2200000000000002
Petal_Width     7   1.2200000000000002 - 1.6800000000000002
Petal_Width     8   1.6800000000000002 - 2.14
Petal_Width     9   2.14 - 2.6
Sepal_Length    10  4.3 - 5.02
Sepal_Length    11  5.02 - 5.739999999999999
Sepal_Length    12  5.739999999999999 - 6.459999999999999
Sepal_Length    13  6.459999999999999 - 7.179999999999999
Sepal_Length    14  7.179999999999999 - 7.899999999999999
Sepal_Width     15  2.3 - 2.76
Sepal_Width     16  2.76 - 3.2199999999999998
Sepal_Width     17  3.2199999999999998 - 3.6799999999999997
Sepal_Width     18  3.6799999999999997 - 4.14
Sepal_Width     19  4.14 - 4.6

我正在尝试将这些数据分组为

Petal_Length[0:1.3 - 2.42,1:2.42 - 3.54,2:3.54 - 4.66,3:4.66 - 5.78,4:5.78 - 6.9]

这就是分组的方式.我的目标是获取属性名称索引和范围。

是否使用哈希图?

更新

我所做的是——

       while((line = bf.readLine())!=null){
        String featureVal[] = line.split("t");
        val.add(featureVal[0]);
        listToSet = new HashSet<String>(val);
        //Creating Arraylist without duplicate values
        attributeVal = new ArrayList<String>(listToSet);
        //Extracting key
        binMap.put(featureVal[0], new ArrayList<String>());
        //Extracting Values
        String[] cols = featureVal[1].split("t");
        for(int i = 0; i < cols.length; i++) {
            if(attributeVal.get(i).equals(cols[i])){
                System.out.println("in foorlop");
                List<String> tmpList = binMap.get(attributeVal.get(i));
                if(tmpList == null) {
                    tmpList = new ArrayList<String>();
                }
                System.out.println("cols[i]"+cols[i]);
                tmpList.add(cols[i]);
                //Get the list and add to that list instead of creating new temp list
                binMap.put(attributeVal.get(i), tmpList);
            }
        }
        System.out.println("binMap: "+binMap);
    }

但是我的输出为空

binMap: {Petal_Width=[], Sepal_Length=[], Petal_Length=[], Sepal_Width=[]}

请指教。

这是您的示例代码,请注意如何使用范围和属性等域类来方便字符串解析。所有的分组都是通过常规的java映射完成的。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PetalGrouping {
    private static final String input = "Petal_Lengtht0t1.3 - 2.42n"
        + "Petal_Lengtht1t2.42 - 3.54n"
        + "Petal_Lengtht2t3.54 - 4.66n"
        + "Petal_Lengtht3t4.66 - 5.78n"
        + "Petal_Lengtht4t5.78 - 6.9n"
        + "Petal_Widtht 5t0.3 - 0.76n"
        + "Petal_Widtht 6t0.76 - 1.2200000000000002n"
        + "Petal_Widtht 7t1.2200000000000002 - 1.6800000000000002n"
        + "Petal_Widtht 8t1.6800000000000002 - 2.14n"
        + "Petal_Widtht 9t2.14 - 2.6n"
        + "Sepal_Lengtht10t4.3 - 5.02n"
        + "Sepal_Lengtht11t5.02 - 5.739999999999999n"
        + "Sepal_Lengtht12t5.739999999999999 - 6.459999999999999n"
        + "Sepal_Lengtht13t6.459999999999999 - 7.179999999999999n"
        + "Sepal_Lengtht14t7.179999999999999 - 7.899999999999999n"
        + "Sepal_Widtht 15t2.3 - 2.76n"
        + "Sepal_Widtht 16t2.76 - 3.2199999999999998n"
        + "Sepal_Widtht 17t3.2199999999999998 - 3.6799999999999997n"
        + "Sepal_Widtht 18t3.6799999999999997 - 4.14n"
        + "Sepal_Widtht 19t4.14 - 4.6";
public static void main(String... args) {
    Map<String, List<Attribute>> map = new HashMap<String, List<Attribute>>();
    String[] lines = input.split("n");
    for (String line : lines) {
        Attribute attribute = Attribute.parse(line);
        List<Attribute> attributeList = map.get(attribute.getName());
        if (attributeList == null) {
            attributeList = new ArrayList<Attribute>();
            map.put(attribute.getName(), attributeList);
        }
        attributeList.add(attribute);
    }
    System.out.println(map);
}

}
class Range {
private double from;
private double to;
private Range(double from, double to) {
    this.from = from;
    this.to = to;
}
public static Range parse(String string) {
    String[] parts = string.split(" ");
    if (parts.length != 3) { throw new RuntimeException("Parsing failed for line: " + string); }
    return new Range(Double.parseDouble(parts[0].trim()), Double.parseDouble(parts[2].trim()));
}
@Override
public String toString() {
    return "{from=" + from + ", to=" + to + '}';
}
}
class Attribute {
private String name;
private int index;
private Range range;
protected Attribute(String name, int index, Range range) {
    this.name = name;
    this.index = index;
    this.range = range;
}
public static Attribute parse(String line) {
    String[] lineParts = line.split("t");
    if (lineParts.length != 3) { throw new RuntimeException("Parsing failed for line: " + line); }
    String name = lineParts[0].trim();
    int index = Integer.parseInt(lineParts[1].trim());
    Range range = Range.parse(lineParts[2].trim());
    return new Attribute(name, index, range);
}
@Override
public String toString() {
    return "index=" + index + " " + range + '}';
}
public String getName() {
    return name;
}
}

我宁愿使用JSON对象或自定义Java对象,如下所示:

Class Flower{
  List<String> Petal_length;
  List<String> Petal_Width;
  List<String> Sepal_length;
  List<String> Sepal_Width;
}

如果你想说,花瓣长度的范围,在0指数,那么我们可以做一些事情,比如字符串范围 = 花。Petal_length.获取(0)

使用对象更灵活,如果以后您获得新文件或计划添加新属性

最新更新