my first post here & I'm only starting to learn Java so please bear with me.
I have a HashMap which stores a key and an instance of a class as the corresponding value (from a class I called Main
). The object has a two variables. What I'd like to do is to get a print out based on a particular object variable, in this example by year. Here is my code:
public class Main {
private String name;
private int year;
public Main(String name, int year) {
this.name = name;
this.year = year;
}
protected static Map<Integer, Main> input = new LinkedHashMap<Integer, Main>();
input.put(1, new Main("Chris", 1980);
input.put(2, new Main("Daphne", 1981);
input.put(3, new Main("Sandra", 1976);
input.put(4, new Main("Adele", 1980);
So now what I'd like to be able to do is to list everyone by year. So my expected output would look like this:
1976: Sandra
1980: Chris, Adele
1981: Daphne
Many thanks