Java 以特定格式显示嵌套地图的内容

Java 以特定格式显示嵌套地图的内容,java,collections,map,hashmap,Java,Collections,Map,Hashmap,我使用嵌套映射来存储值。我需要以特定的格式打印它 输入数据: University Department DeptHead A X Tim B X Jim C X John A Y Alex C Z

我使用嵌套映射来存储值。我需要以特定的格式打印它

输入数据:

University      Department    DeptHead
A                X               Tim
B                X               Jim
C                X               John
A                Y               Alex
C                Z               Peter
D                Z               Dan
B                Z               Ashley
B                Y               Peter
D                Y               Maria
代码:

我需要以以下格式显示输出(预期输出):

使用
HashMap
实现此目的的最佳方法是什么


如果没有
HashMap
,解决此问题的最佳方法是什么?

哈希映射将始终以随机顺序打印。如果您需要某种形式的订单格式,我建议您提供一个地图列表,或者在抽象层次上再进一步,您可以将其作为一个地图集合。这取决于您需要对数据做什么。在分布式设置中,HashMap通常足以进行这种类型的数据收集,然后由数据库进行排序,而不是HashMap本身。
下面是我所说内容的简要说明:

    private Collection<Map<String, String>> people  = 
                             new ArrayList<Map<String, String>>();
那你需要一所大学

public class University{
    // in here you can have a some sort of collection or map of dept heads and match them up with a collection of people. ASgain a Map is the fastest way to do this, but this is where you have to put some thought into the problem and think about what is really the best way to do this. 

您可能希望尝试使用format对象将其放入测试文件中。。。Idk,它;it’这是你的决定,但它需要大量的思考和反复试验。如果你遇到另一个困境,请发回邮件

地图的收集将帮助我解决随机顺序格式,但我不确定地图的收集将如何帮助我,因为我需要沿-ve Y轴显示各个大学(A、B、C、D)的deptHead和沿X轴显示课程(X、Y、Z)。您能告诉我如何显示上述数据吗?集合是一个相当抽象的术语,可能有很多东西。我建议查看Java Collections API以了解使用它们的不同方式,重点关注列表和ArrayList,因为我认为这会有所帮助。其余的似乎只是格式化;我会尽量把数据和格式分开。您可以通过引入更多能够处理这些方面的对象来实现这一点。祝你好运
University      X      Y         Z      
A               Tim    Alex      -  
B               Jim    Peter   Ashley  
C               John   -         Peter  
D               -       Maria     Dan  
    private Collection<Map<String, String>> people  = 
                             new ArrayList<Map<String, String>>();
    private Set<Map<String, String>> peopleNoDuplicates = 
                            new TreeSet<Map<String, String>>(people);
public class Person{
    private String name;
    private int id;
    private static instanceCounter = 1000;
//always use the constructor to make sure you get mandatory values in your objects 
public Person(String name){
    setName(name);
    this.id = instanceCounter++;

public final void setName(String name)throws IllegalArg....{
    if(name == null || name.isEmpty()){
        throw new IllegalArg..Ex...("Sorry name is not valid");
    }
    this.name = name;
}

 public String getName.....
public class University{
    // in here you can have a some sort of collection or map of dept heads and match them up with a collection of people. ASgain a Map is the fastest way to do this, but this is where you have to put some thought into the problem and think about what is really the best way to do this.