Java 在列表/哈希映射中分组数据

Java 在列表/哈希映射中分组数据,java,list,hashmap,Java,List,Hashmap,我有一个这样的文件 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

我有一个这样的文件

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]
这是分组的方式吗。 我的目标是获取属性名索引和范围

是否使用hashmap

更新

我所做的是-

       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);

    }

请建议。

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

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

}
类花{
列出花瓣长度;
列出花瓣宽度;
列出萼片的长度;
列出萼片的宽度;
}
如果你想说,花瓣长度的范围,在0指数,我们可以做一些事情,比如 字符串范围=flower.Petal\u length.get(0)


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

以下是示例代码,请注意如何使用诸如Range和attribute之类的域类来方便字符串解析。所有分组都是通过常规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_Length\t0\t1.3 - 2.42\n"
        + "Petal_Length\t1\t2.42 - 3.54\n"
        + "Petal_Length\t2\t3.54 - 4.66\n"
        + "Petal_Length\t3\t4.66 - 5.78\n"
        + "Petal_Length\t4\t5.78 - 6.9\n"
        + "Petal_Width\t 5\t0.3 - 0.76\n"
        + "Petal_Width\t 6\t0.76 - 1.2200000000000002\n"
        + "Petal_Width\t 7\t1.2200000000000002 - 1.6800000000000002\n"
        + "Petal_Width\t 8\t1.6800000000000002 - 2.14\n"
        + "Petal_Width\t 9\t2.14 - 2.6\n"
        + "Sepal_Length\t10\t4.3 - 5.02\n"
        + "Sepal_Length\t11\t5.02 - 5.739999999999999\n"
        + "Sepal_Length\t12\t5.739999999999999 - 6.459999999999999\n"
        + "Sepal_Length\t13\t6.459999999999999 - 7.179999999999999\n"
        + "Sepal_Length\t14\t7.179999999999999 - 7.899999999999999\n"
        + "Sepal_Width\t 15\t2.3 - 2.76\n"
        + "Sepal_Width\t 16\t2.76 - 3.2199999999999998\n"
        + "Sepal_Width\t 17\t3.2199999999999998 - 3.6799999999999997\n"
        + "Sepal_Width\t 18\t3.6799999999999997 - 4.14\n"
        + "Sepal_Width\t 19\t4.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;
}
}
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
公共类PetalGroup{
私有静态最终字符串输入=“花瓣长度\t0\t1.3-2.42\n”
+“花瓣长度\t1\t2.42-3.54\n”
+“花瓣长度\t2\t3.54-4.66\n”
+“花瓣长度\t3\t4.66-5.78\n”
+“花瓣长度\t4\t5.78-6.9\n”
+“花瓣宽度\t 5\t0.3-0.76\n”
+“花瓣宽度\t 6\t0.76-1.2200000000000002\n”
+“花瓣宽度\t 7\t1.2200000000000002-1.680000000000002\n”
+“花瓣宽度\t 8\t1.680000000000002-2.14\n”
+“花瓣宽度\t 9\t2.14-2.6\n”
+“萼片长度\t10\t4.3-5.02\n”
+“萼片长度\t11\t5.02-5.7399999999\n”
+“萼片长度\t12\t5.73999999999-6.45999999999999\n”
+“萼片长度\t13\t6.45999999999-7.17999999999999\n”
+“萼片长度\t14\t7.17999999999-7.89999999999\n”
+“萼片宽度\t 15\t2.3-2.76\n”
+“萼片宽度\t 16\t2.76-3.219999998\n”
+“萼片宽度\t 17\t3.21999999998-3.67999999997\n”
+“萼片宽度\t 18\t3.679999997-4.14\n”
+“萼片宽度19-4.14-4.6”;
公共静态void main(字符串…参数){
Map Map=newhashmap();
字符串[]行=input.split(“\n”);
用于(字符串行:行){
Attribute=Attribute.parse(行);
List attributeList=map.get(attribute.getName());
if(attributeList==null){
attributeList=新的ArrayList();
map.put(attribute.getName(),attributeList);
}
attributeList.add(属性);
}
系统输出打印项次(map);
}
}
等级范围{
私人双起;
私人双到;
专用范围(双倍从,双倍到){
this.from=from;
这个;
}
公共静态范围解析(字符串){
String[]parts=String.split(“”);
if(parts.length!=3){抛出新的RuntimeException(“对行:“+string”的解析失败);}
返回新范围(Double.parseDouble(部分[0].trim()),Double.parseDouble(部分[2].trim());
}
@凌驾
公共字符串toString(){
返回“{from=“+from+”,to=“+to+'}”;
}
}
类属性{
私有字符串名称;
私有整数索引;
私人射程;
受保护的属性(字符串名称、整数索引、范围){
this.name=名称;
这个指数=指数;
这个范围=范围;
}
公共静态属性解析(字符串行){
字符串[]lineParts=line.split(“\t”);
如果(lineParts.length!=3){抛出新的RuntimeException(“对行:+line的解析失败);}
字符串名称=lineParts[0]。trim();
int index=Integer.parseInt(lineParts[1].trim());
Range=Range.parse(lineParts[2].trim());
返回新属性(名称、索引、范围);
}
@凌驾
公共字符串toString(){
返回“index=“+index+”“+range+”}”;
}
公共字符串getName(){
返回名称;
}
}

非常感谢:)@Igor KatkovYes我也这么做了。但是我在我的键中得到了重复的值。不确定它的意思是什么-“我的键中的重复值”如果您发布给您带来悲伤和结果的输入数据/想要getMap=new HashMap(),这会有所帮助;如何遍历map.pairs.getKey()帮助我获得键,并使用整个值获得pairs.getValues()。但是我需要单独的值如何获得它
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_Length\t0\t1.3 - 2.42\n"
        + "Petal_Length\t1\t2.42 - 3.54\n"
        + "Petal_Length\t2\t3.54 - 4.66\n"
        + "Petal_Length\t3\t4.66 - 5.78\n"
        + "Petal_Length\t4\t5.78 - 6.9\n"
        + "Petal_Width\t 5\t0.3 - 0.76\n"
        + "Petal_Width\t 6\t0.76 - 1.2200000000000002\n"
        + "Petal_Width\t 7\t1.2200000000000002 - 1.6800000000000002\n"
        + "Petal_Width\t 8\t1.6800000000000002 - 2.14\n"
        + "Petal_Width\t 9\t2.14 - 2.6\n"
        + "Sepal_Length\t10\t4.3 - 5.02\n"
        + "Sepal_Length\t11\t5.02 - 5.739999999999999\n"
        + "Sepal_Length\t12\t5.739999999999999 - 6.459999999999999\n"
        + "Sepal_Length\t13\t6.459999999999999 - 7.179999999999999\n"
        + "Sepal_Length\t14\t7.179999999999999 - 7.899999999999999\n"
        + "Sepal_Width\t 15\t2.3 - 2.76\n"
        + "Sepal_Width\t 16\t2.76 - 3.2199999999999998\n"
        + "Sepal_Width\t 17\t3.2199999999999998 - 3.6799999999999997\n"
        + "Sepal_Width\t 18\t3.6799999999999997 - 4.14\n"
        + "Sepal_Width\t 19\t4.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;
}
}