2014-04-21 28 views
-1

我已经使用嵌套地图来存储值。我需要以特定的格式打印它。以特定格式显示嵌套地图的内容

输入数据:

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 

代码:

import java.io.OutputStream; 
import java.util.HashMap; 
import java.util.Iterator; 
import java.util.Map; 

public class testNested { 

public static void printMap(Map<String, String> map,Object key) 
{ 
    Iterator it = map.entrySet().iterator(); 

    // System.out.print(key.toString()); 

    while(it.hasNext()) 
    { 
     Map.Entry pairs = (Map.Entry)it.next(); 
     System.out.print(key.toString() + " "+ pairs.getKey() + " " +   pairs.getValue()); 
    } 
    System.out.println(); 
} 

public static void main(String arg[]) 
{ 
    Map<String,Map<String,String>> map= new HashMap<String,Map<String, String>>(); 
    Map<String,String>A=new HashMap<String, String>(); 
    Map<String,String>B=new HashMap<String, String>(); 
    Map<String,String>C=new HashMap<String, String>(); 
    Map<String,String>D=new HashMap<String, String>(); 
    A.put("X", "Tim"); 
    A.put("Y", "Alex"); 
    B.put("X", "Jim"); 
    B.put("Z", "Ashley"); 
    B.put("Y", "Peter"); 
    C.put("X", "John"); 
    C.put("Z", "Peter"); 
    D.put("Y", "Maria"); 
    D.put("Z", "Dan"); 
    map.put("A",A); 
    map.put("B", B); 
    map.put("C", C); 
    map.put("D", D); 

    Iterator it = map.entrySet().iterator(); 
    while (it.hasNext()) { 
     Map.Entry pairs = (Map.Entry)it.next(); 
     //System.out.println(pairs.getKey()); 
     printMap((Map<String, String>)pairs.getValue(),pairs.getKey()); 
    } 
} 
} 

输出:

D Y Maria Z Dan 
A Y Alex X Tim 
B Y Peter X Jim Z Ashley 
C X John Z Peter 

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

University  X  Y   Z  
A    Tim Alex  - 
B    Jim Peter Ashley 
C    John -   Peter 
D    -  Maria  Dan 

使用HashMap这样做的最佳方法是什么?

没有HashMap这个问题的最佳方法是什么?

回答

1

散列图将始终以随机顺序打印。如果您需要持有某种订单格式,我会建议一张地图列表,或者在抽象阶梯上向前走一步,您可以将其制作为地图集合。这取决于你需要怎样处理数据。在分布式设置中,对于这种类型的数据收集,HashMap通常是足够的,然后数据库执行排序,而不是HashMap本身。
这是我所说的速写:

private Collection<Map<String, String>> people = 
          new ArrayList<Map<String, String>>(); 

这样,你只要把你的地图,并将其添加到列表中,你需要的顺序。
如果你让一个集合作为建议你可以通过它身边经过,像这样的构造,做任何你需要做的:

private Set<Map<String, String>> peopleNoDuplicates = 
          new TreeSet<Map<String, String>>(people); 

问题补充进行修改后: 我不是由于时间的限制,能够一步一步展示一切。对不起,如果您仍有问题,我会再回来编辑。

问题域中确实没有足够的对象来正确执行此操作。你真的应该有一个名为“Person”的对象,你还应该有一个对象“University”和一个Department对象,它是“University”中的一个字段,然后你可以使用对象来传递信息,否则你错过了在所有伟大的事情OOP可以那么你需要一个大学

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. 

你可能想尝试把它变成一个测试文件中使用的格式对象做

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..... 

... IDK,它。你的电话,但它需要很多的思想和试验和错误。回来,如果你遇到另一个卡纸。

+0

收集地图将帮助我解决随机顺序格式,但我不确定如何收集地图将有所帮助,因为我需要显示各个大学(A,B,C,D)的deptHead - 沿Y轴和课程(X, Y,Z)沿X轴。你能告诉我如何显示上述数据吗? –

+0

集合是一个非常抽象的术语,可能是很多事情。我建议寻找Java Collections API来查看使用它们的不同方式,关注List和ArrayList,因为我认为这将有所帮助。其余的似乎只是格式化;我真的会试图保持数据和格式分开。你可以通过引入更多可以处理这些方面的对象来做到这一点。祝你好运 – nckbrz