【问题标题】:Get value from hashmap/keyset in java?从java中的hashmap/keyset获取值?
【发布时间】:2014-10-19 10:00:54
【问题描述】:

我有代码将两个值放入 Hashmap,然后从另一个方法中访问它们。我正在遍历一个值“狗”,但在方法结束时,我需要打印出与该“狗”值相关的“种族”...

这是我目前所拥有的:

    DecimalFormat df = new DecimalFormat("#.##");
    for (String dog: data.keySet()) { // use the dog 



    String dogPage = "http://www.gbgb.org.uk/raceCard.aspx?dogName=" + dog;
    Document doc1 = Jsoup.connect(dogPage).get();
//  System.out.println("Dog name: " + dog);



    Element tblHeader = doc1.select("tbody").first();
    for (Element element1 : tblHeader.children()){
        String position = element1.select("td:eq(4)").text();
        int starts = (position.length() + 1) / 4;
        int starts1 = starts;
         //         System.out.println("Starts: " + starts);

        Pattern p = Pattern.compile("1st");
        Matcher m = p.matcher(position);
        int count = 0;
        while (m.find()){
            count +=1;
        }
        double firsts = count / (double)starts1 * 100;
        String firstsStr = (df.format(firsts));
        //          System.out.println("Firsts: " + firstsStr + "%");

        Pattern p2 = Pattern.compile("2nd");
        Matcher m2 = p2.matcher(position);
        int count2 = 0;
        while (m2.find()){
            count2 +=1;
        }
        double seconds = count2 / (double)starts1 * 100;
        String secondsStr = (df.format(seconds));
//      System.out.println("Seconds: " + secondsStr + "%");

        Pattern p3 = Pattern.compile("3rd");
        Matcher m3 = p3.matcher(position);
        int count3 = 0;
        while (m3.find()){ 
            count3 +=1;
        }

        double thirds = count3 / (double)starts1 * 100;
        String thirdsStr = (df.format(thirds));
         //     System.out.println("Thirds: " + thirdsStr + "%");

        if (starts1 > 20 && firsts < 20 && seconds > 30 && thirds > 20){

            System.out.println("Dog name: " + dog);
    //      System.out.println("Race: " + race);
            System.out.println("Firsts: " + firstsStr + "%");
            System.out.println("Seconds: " + secondsStr + "%");
            System.out.println("Thirds: " + thirdsStr + "%");
            System.out.println("");
        }


    }

我可以使用类似于“String dog: data.keySet())”的东西来获取“Race”的值吗?例如:字符串竞赛:data.keySet())?

上一个方法:

    Document doc = Jsoup.connect(
            "http://www.sportinglife.com/greyhounds/abc-guide").get();

    Element tableHeader = doc.select("tbody").first();
    Map<String, String> data = new HashMap<>();
    for (Element element : tableHeader.children()) {
        // Here you can do something with each element
        if (element.text().indexOf("Pelaw Grange") > 0
                || element.text().indexOf("Shawfield") > 0
                || element.text().indexOf("Shelbourne Park") > 0
                || element.text().indexOf("Harolds Cross") > 0) {
            // do nothing
        } else {

            String dog = element.select("td:eq(0)").text();
            String race = element.select("td:eq(1)").text();
            data.put(dog, race);

        }

非常感谢任何帮助,谢谢!

罗伯

【问题讨论】:

  • HashMap的value部分是什么?
  • 你在哪里参加比赛?您如何将数据放入data hashmap?可能您需要遍历data.entrySet()。阅读更多 abot 地图。
  • 我已经用其他方法中的代码更新了我的原始帖子,显示了添加种族的位置...谢谢

标签: java hashmap keyset


【解决方案1】:

我假设HashMap 的值部分是Race

如果是,那么您可以执行以下操作:

String race = data.get(dog);

在您当前的代码中,您正在执行以下操作:

for (String dog: data.keySet()) { // use the dog 
    String race = data.get(dog); // this will give the value of race for the key dog
    // using dog to do fetch details from site...
}

您还可以执行以下操作:

for (Entry<String, String> entry: data.entrySet()) {
   String dog = entry.getKey();
   String race = entry.getValue();
   // using dog to do fetch details from site...
}

【讨论】:

  • 您好,感谢您的回答...它似乎给出了一个语法错误,说它需要一个“;”而不是“:”,但如果我这样做,它似乎仍然不起作用......有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-25
  • 2016-03-08
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 2017-09-19
相关资源
最近更新 更多