Byron R. Boydstun Jr.
Byron R. Boydstun Jr.

Reputation: 51

How to print out the contents of a LinkedHashMap

Here is my code,

import java.util.LinkedHashMap;
import java.util.Scanner;

public class Project8Q3 extends President {

    private static LinkedHashMap<String, President> presidents;

    public static void main(String[] args) {
        String selection = null;
        Scanner in = new Scanner(System.in);
        do {
            presidents = new LinkedHashMap<String, President>();

            addPresident("16", "Lincoln", "Abraham", null);
            //a few more president's info but you get the idea

            System.out.println("******************************************");
            System.out.println("* G - Get a president's information      *");
            System.out.println("* D - display all president's information*");
            System.out.println("* X - Exit the  program                  *");
            System.out.println("******************************************");
            System.out.println("Enter your selection: ");

            selection = in.next();

            if (selection.trim().equalsIgnoreCase("G")) {
                System.out.println("Enter the president's ID: ");
                showPresident(in.next());
            } else if (selection.equalsIgnoreCase("D")) {
                showPresidents();
            } else if (selection.equalsIgnoreCase("X")) {
                System.out.println("\nThank you for using the PRESIDENT DATABASE. Good-bye!");
            } else {
                System.out.println("INVALID SELECTION! TRY AGAIN!\n");
            }
        } while (!selection.equalsIgnoreCase("X"));
        in.close();
    }

    public static void addPresident(String id, String lastName,
            String firstName, String middleInitial) {
        President president = new President(id, lastName, firstName, middleInitial);
        presidents.put(president.id, president);
    }

    public static void showPresidents() {
        //presidents = new LinkedHashMap<String, President>();

    }

    private static void showPresident(String string) {
        // TODO Auto-generated method stub

    }

}

I am trying to print out the president's information, in the "showPresidents()" function. (clearly). I'm taking too many programming language course right now and I think I'm confused about how to print out the LinkedHashMap. Can anyone help me out?

Upvotes: 4

Views: 9669

Answers (4)

Nikolai  Shevchenko
Nikolai Shevchenko

Reputation: 7521

Define toString method for your class

class President {
    .....

    @Override
    public String toString() {
        return "President{" +
                "id=" + id +
                ", lastName=" + lastName +
                ", firstName=" + firstName +
                ", middleInitial=" + middleInitial +
                '}';
    }
}

And then just call

Java 8 and higher:

presidents.forEach((id, president) -> System.out.println(president));

Java 7 and lower:

for (President p : presidents.values()) {
    System.out.println(p);
}

Upvotes: 2

Zoe - Save the data dump
Zoe - Save the data dump

Reputation: 28238

Just call toString:

System.out.println(presidents.toString());

Note that this will call toString on the President object and give you com.package.President@123abc when printed (the part after @ varies by the instance). If you want to print the content of the president you can either override toString in President:

@Override public String toString(){
    return some formatted version of your object
}

Alternatively, you could of course iterate over the map and print it like that:

for(Entry<String, President> entry : presidents.entrySet()){
    String key = entry.getKey();
    President value = entry.getValue();
}

Upvotes: 1

Dorian Gray
Dorian Gray

Reputation: 2981

The recommended way to do so is using an iterator over the map:

for (Map.Entry<String, President> entry : presidents.entrySet()) {
    President president = entry.getValue();
    String name = entry.getKey();
    //Do something
 }

Upvotes: 3

k_ssb
k_ssb

Reputation: 6252

To print a particular President given the name, you need to access the presidents map using the name as the key, and print out the value. To print out all presidents, we can loop over keys in the presidents map as follows:

public static void showPresidents() {
    for (String key : presidents.keySet()) {
        showPresident(key);
    }
}

private static void showPresident(String string) {
    System.out.println(presidents.get(string));
}

Upvotes: 4

Related Questions