1

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;
}
}
Sid Minas
  • 21
  • 1
  • 5

3 Answers3

0

I'd try something like:

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

//Don't know why you're doing this, have to sort them? Then President class needs to implement Comparator Interface.
//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());
// }

map.get("Philippines".put("president", list);
//To get this working you may override the toString method in your presidents list.
System.out.println(map.get("Philippines").get("president").toString());

}

Is that close what you're looking for?

unmultimedio
  • 1,224
  • 2
  • 13
  • 39
0

This line should not even work:

Collections.sort(list, new President());

If anything, President should either implement Comparable<T>

http://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

How to implement the Java comparable interface?

or you should give it a Comparator<T> implementation, like so:

http://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html

How to use Comparator in Java to sort

Afterwards, the code should work as intended.

Community
  • 1
  • 1
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
0

If you want to print the presidents names and the president years sorted:

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, new President());

map.get("Philippines").put("presidents", list);

for(Entry<String, HashMap<String, Object>> entry: map.getEntrySet()){
    HasMap<String, Object> attributes = entry.getValue();
    List<President> pList = (List<President>) attributes.get("presidents");
    for(President p: pList) System.out.print(p.getPresName() + ", " + p.getYear() + " ");
}

Output is:

"Aguinaldo, 1 Quezon, 2 Marcos, 10" 

You have to do the checks to be sure you are not getting a NPE (like, there is always a "presidents" key? and so on).

In this way you have a Map which contains a Map which contains a List. It's a bit messy.. Why don't you use Java classes?

public class State{
    String name;
    String capital;
    List<President> presidents;

    public State(String name){
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCapital() {
        return capital;
    }
    public void setCapital(String capital) {
        this.capital = capital;
    }
    public List<String> getPresidents() {
        return presidents;
    }
}

In your main:

State philippines = new State("Philippines");
philippines.setCapital("Manila");
philippines.getPresidents().add(new President("Aguinaldo", 1));
philippines.getPresidents().add(new President("Marcos", 10));
philippines.getPresidents().add(new President("Quezon", 2));

Collections.sort(philippines.getPresidents(), new President());    

for(President p: philippines.getPresidents())
    System.out.print(p.getPresName());

And to store your states:

Map<String, State> states = new HashMap<String, State>();
//create philippines
states.add("Philippines", philippines);

Collections.sort(states.get("Philippines").getPresidents(), new President());

for(President p: states.get("Philippines").getPresidents())
    System.out.print(p.getPresName());
Narmer
  • 1,414
  • 7
  • 16