Java从集合中选择某些值

Java从集合中选择某些值,java,set,Java,Set,我正在读取一个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

我正在读取一个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
因此,从技术上讲,我试图实现的是,从我的集合中选择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循环,并从集合中选择3个值,然后在最后的print语句中使用.getName和.getHeight

我有一个大致的想法

            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());
             }
        }
    }
但我不确定的是,如何获得这些设定值。我的映射值是一个集合,因为我以前的方法启动如下:

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

集合没有get方法。你可以把它写在一个列表中

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

看起来您需要了解增强的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;
}
见: Javadoc- 爪哇™ 教程- 堆垛溢出-
StackOverflow-

谢谢你的提示,entry是Java中的关键词还是指我的set/map?@godlypythonoops!第一行更改为正确声明条目。我明白了,当前此代码打印集合中的每个项目是否正确?因为如果我将初始for循环保留在定义i的位置,它不会打印前3个元素,而只是重复输出3次,因为整数不能应用于Entry/EntrySet/Entry Value,那么我将如何定义需要多少个值?我尝试对前3个值再次使用for循环,但它不允许我应用integer@godlypython抱歉,我没有看到您希望将结果限制为最多3个山的部分。代码已添加到限制结果。
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;
}
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);
});