【问题标题】:Java Selecting certain values from a setJava从集合中选择某些值
【发布时间】:2017-04-03 16:30:09
【问题描述】:

我正在读取一个包含 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 个值并在我的最终打印语句中使用“.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());
             }
        }
    }

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

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

【问题讨论】:

    标签: java set


    【解决方案1】:

    Set 没有 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;
        }
    }
    

    【讨论】:

      【解决方案2】:

      看来您需要了解增强的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?

      【讨论】:

      • 谢谢你的提示,“入口”是Java中的一个关键词还是你指的是我的集合/地图?
      • @godlypython 哎呀!第一行更改为正确声明 entry
      • 我明白了,目前此代码打印集合中的每个项目是否正确?因为如果我将初始 for 循环留在我定义 i 的位置,它不会打印前 3 个元素,而只是重复输出 3 次
      • 由于整数不能应用于Entry/EntrySet/Entry Value,那么我将如何定义我想要多少个值?我尝试对前 3 个值再次使用 for 循环,但它不允许我应用整数
      • @godlypython 抱歉,我没有看到您希望将结果限制为最多 3 个山丘的部分。已添加代码以限制结果。
      猜你喜欢
      • 2017-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多