0

I am reading from a CSV file that contains data about Hills. I can read the data from the file, and have associated column headers with 6 fields. Currently my output prints like:

### 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]

But I am trying to get my output like:

### 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

So technically what I am trying to achieve is, select 3 values from my SET which are actually inside a map and only print out the name and height.

My code currently is:

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

I believe i have to use another for loop, and somehow select only 3 values from the Set and use ".getName" and ".getHeight" in my final print statement

I had an idea of something along the lines

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

But what im not sure about is, how to get these set values. My Map value is a set because my previous method initiates like:

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

2 Answers2

0

Sets don't have a get method. You could wrap it in a list e.g.

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

or use a for-each loop instead. If you need to only print the first 3 you can add a counter to handle that.

int i = 0;
for (Hill h : hills) {
    //Can print here with h however you like
    if (++i == 3) {
        break;
    }
}
Daniel Bickler
  • 1,119
  • 13
  • 29
0

Looks like you need to learn about the enhanced for loop, also known as the for-each loop.

Your code should be:

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

In Java 8, you could also use streaming:

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

See:
Javadoc - Map.entrySet()
The Java™ Tutorials - 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?

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • Thank you for the tips, is "entry" a key word in Java or are you referring to my set/map? –  Apr 03 '17 at 16:41
  • @godlypython Oops! First line changed to declare `entry` correctly. – Andreas Apr 03 '17 at 16:42
  • I see, currently this code prints every item in the set correct? Because if i leave my initial for loop where i define i, it wont print the first 3 elements, but just repeat the output 3 times –  Apr 03 '17 at 16:45
  • since a integer cant be applied to Entry/EntrySet/Entry Value, how would i define how many values i want then? I tried using a for loop again for the first 3 values but it doesnt let me apply integer –  Apr 03 '17 at 17:25
  • 1
    @godlypython Sorry, I didn't see the part where you wanted to limit result to at most 3 hills. Code has been added to limit result. – Andreas Apr 03 '17 at 17:29
  • Oh i see, so you cant use an index directly on a entry. If i want to limit the amount of county that appear i.e. my key, can i apply the same context as done to the hills? –  Apr 03 '17 at 17:32