I would like to ask if how can I print out my arraylist
by using map.get().get()
? The output should be:
"Aguinaldo, Marcos, Quezon"
And the way to print it should be:
System.out.println(map.get("Philippines").get("President"));
I didn't get any problem with the other output except on this one.
Here's the code for my main:
import java.util.*;
public class PresMain{
public static void main(String args[]){
HashMap<String, HashMap<String, Object>> map = new HashMap<String, HashMap<String, Object>>();
map.put("Philippines", new HashMap<String, Object>());
map.get("Philippines").put("capital", "Manila");
map.get("Philippines").put("continent", "Asia");
System.out.println(map.get("Philippines").get("capital"));
System.out.println(map.get("Philippines").get("continent"));
List<President> list = new ArrayList<President>();
list.add(new President("Aguinaldo", 1));
list.add(new President("Marcos", 10));
list.add(new President("Quezon", 2));
Collections.sort(list);
for(President a: list)
System.out.print(a.getPresName() + ", ");
Collections.sort(list, new President());
System.out.println(" ");
for(President a: list)
System.out.println(a.getPresName() +" , "+ a.getPresYear());
}
}
Sorry for not including my other class awhile ago, so here it is:
enter code here
import java.util.*;
class President implements Comparator<President>, Comparable<President>{
private String name;
private int year;
int x;
President(){
}
President(String n, int a){
name = n;
year = a;
}
public String getPresName(){
return name;
}
public int getPresYear(){
return year;
}
public int getPresX(){
return x;
}
// Overriding the compareTo method; sort(list)
public int compareTo(President d){
//return (this.name).compareTo(d.name);
return this.year - d.year;
}
// Overriding the compare method to sort the age
public int compare(President d, President d1){
//return (d.name).compareTo(d1.name);
// return d.age - d1.age;
return d.x - d1.x;
}
}