Java从集合中选择某些值

时间:2017-04-03 16:30:10

标签: java set

我正在阅读包含Hills数据的CSV文件。我可以从文件中读取数据,并将相关的列标题与6个字段相关联。目前我的输出打印如下:

### County:Argyll and Bute
[48,Deinn a'Choin,Argyll and Bute,768.2,56.281081,-4.659943, 49,Feinn a'Choin,Argyll and Bute,768.5,56.281043,-4.659924, 50,Zeinn a'Choin,Argyll and Bute,768.7,56.281034,-4.659981]
### County:Stirling
[19,Beinn Each,Stirling,813.0,56.313957,-4.262199, 11,Creag Gharbh,Stirling,637.4,56.46677,-4.221506, 7,Meall Buidhe,Stirling,719.0,56.419004,-4.308645]
### County:Aberdeen
    [19,Beinn Each,Aberdeen,813.0,56.313957,-4.262199, 11,Creag Gharbh,Aberdeen,637.4,56.46677,-4.221506, 7,Meall Buidhe,Aberdeen,719.0,56.419004,-4.308645]

但我想尝试输出:

### County:Argyll and Bute
NameOfHill#1 Height
NameOfHill#2 Height
NameOfHill#3 Height
### County:Stirling
NameOfHill#1 Height
NameOfHill#2 Height
NameOfHill#3 Height
### County:Aberdeen
NameOfHill#1 Height
NameOfHill#2 Height
NameOfHill#3 Height

从技术上讲,我想要实现的是,从我的SET中选择实际位于地图中的3个值,并打印出名称和高度。

我的代码目前是:

    for (int i = 0; i < 3; i++) {
        System.out.println("### County:" + hillsByCounty.keySet().toArray()[i]);
        System.out.println(hillsByCounty.get((hillsByCounty.keySet().toArray())[i]));
    }
}

我相信我必须使用另一个for循环,并以某种方式从Set中仅选择3个值并使用&#34; .getName&#34;和&#34; .getHeight&#34;在我最后的印刷声明中

我对某些事情有所了解

            Set<Hill> getHills = hillsByCounty.get((hillsByCounty.keySet().toArray())[i]);
            for (int j = 0 ; j < 3 ; j++){
                        //write to somehow code to pull the hill in pos j in this set
                        System.out.println(h.getName() + " " + h.getHeight());
             }
        }
    }

但我不确定的是,如何获得这些设定值。我的Map值是一个集合,因为我之前的方法启动如下:

public static Map<String, Set<Hill>> hillsByCounty(List<Hill> hills) {

2 个答案:

答案 0 :(得分:0)

集合没有get方法。你可以将它包装在一个列表中,例如。

List<Hill> hills = new ArrayList<>(hillsByCounty.get((hillsByCounty.keySet().toArray())[i]));

或使用for-each循环代替。如果你只需要打印前3个,你可以添加一个计数器来处理它。

int i = 0;
for (Hill h : hills) {
    //Can print here with h however you like
    if (++i == 3) {
        break;
    }
}

答案 1 :(得分:0)

您需要了解增强型for循环,也称为 for-each 循环。

您的代码应为:

int countyCount = 0;
for (Map.Entry<String, Set<Hill>> entry : hillsByCounty(readHills()).entrySet()) {
    System.out.println("### County:" + entry.getKey());
    int hillCount = 0;
    for (Hill hill : entry.getValue()) {
        System.out.println(hill.getName() + " " + hill.getHeight());
        if (++hillCount == 3)
            break;
    }
    if (++countyCount == 5)
        break;
}

在Java 8中,您还可以使用流式传输:

hillsByCounty(readHills()).entrySet()
                          .stream()
                          .limit(5)
                          .forEach(entry -> {
    System.out.println("### County:" + entry.getKey());
    entry.getValue()
         .stream()
         .limit(3)
         .map(h -> h.getName() + " " + h.getHeight())
         .forEach(System.out::println);
});

请参阅:
Javadoc - Map.entrySet()
Java™教程 - The for Statement
StackOverflow - What is the syntax of enhanced for loop in Java?
StackOverflow - Why is enhanced for loop efficient than normal for loop?