简体   繁体   中英

Get value from hashmap/keyset in java?

I have code where I am placing two values into a Hashmap, and then accessing them from within another method. I am iterating through one value "dog", but at the end of the method, I need to print out the "race" relating to that "dog" value...

Here's what I have so far:

    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("");
        }


    }

Am I able to use something similar to "String dog: data.keySet())" to get the value of "Race"? for example: String race: data.keySet())?

Previous method:

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

        }

Any help is much appreciated, thanks!

Rob

I am assuming that the value part of the HashMap is Race .

If yes, then you can do the following:

String race = data.get(dog);

in your current code, you are doing the following:

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...
}

You could also do the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM