2014-02-11 61 views
3

我有這樣的/分組數據在列表的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_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] 

是該分組的方式。 我的目標是獲得屬性名稱索引和範圍。

是否使用散列表?

UPDATE

我所做的是 -

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

    } 

但我的輸出爲null

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

請建議。

回答

1

這裏是您的示例代碼,請注意如何有域類像範圍和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; 
} 
} 
+0

非常感謝:) @Igor Katkov –

+0

是的,我做了同樣的事情。但我在密鑰中獲得重複值。 –

+0

不確定是什麼意思 - 「在我的密鑰中重複的值」如果您發佈的輸入數據給你帶來的悲傷和結果,你會得到/想得到 –

1

我寧願使用JSON對象或自定義Java對象,如:

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

} 

如果你想要說的,範圍花瓣長度,在0指數的話,我們可以做一些事情像 字符串範圍=花。 Petal_length.get(0)

有了對象,但它更靈活,如果以後你會得到新的文件,或者你打算增加新的屬性