我目前正在使用Jersey REST来创建一个网页,其中包含鸟类和分类号的列表,以及指向该网页的链接,该链接专门针对相关鸟类。虽然我的链接在两个页面之间起作用,并且我的鸟名和分类号出现,但我无法显示订单或姓氏。以下是有问题的代码。
@Path("/birdslist")
public class BirdsList extends Birds {
@GET
@Path("/all")
@Produces("text/html")
public String all() {
Iterator iterator = birdnames.keySet().iterator();
String page = "<html><title>All Birds</title><body>";
page += "<p>This is the list of all birds. <br> Click the taxonomy number of the bird you wish to view in detail.</p>";
while(iterator.hasNext()){
Object key = iterator.next();
String value = birdnames.get(key);
HashSet fam = family.get(key);
HashSet ord = order.get(key);
}
for (String key : birdnames.keySet()) {
page += String.format("<p>Name:%s <br> Taxonomy Number:<a href=%s>%s</a></p>",birdnames.get(key),key,
key);
getBird(key);
}
page += "</body></html>";
return page;
}
@GET
@Path("{key}")
@Produces("text/html")
public String getBird(@PathParam("key") String key) {
String page = "<html><title>Bird #: {key}</title><body>";
page += String.format("<p>This page contains info on the %s</p>",birdnames.get(key));
page += String.format("<p>Name:%s <br> Taxonomy Number:%s <br> Family:%s <br> Order:%s</p>",birdnames.get(key),key,family.get(key),order.get(key));
page += "<p>Please click <a href=all>here</a> to return to the list of all birds.</p>";
page += "</body></html>";
return page;
}
}
族和顺序保存在散列图内的HashSet中,而鸟名在散列映射中。它是从csv文件写入并转换为哈希映射。以下是该代码。
public class Birds {
HashMap<String,String> birdnames;
HashMap<String,HashSet<String>> family;
HashMap<String,HashSet<String>> order;
/**
Constructor reads the CSV of all birds
*/
public Birds() {
// long path to eBirds assuming Maven "mvn exec:java" is many levels up
String fileName = "src/main/java/com/example/rest/eBirds.csv";
boolean firstLine = true;
this.birdnames = new HashMap<String,String>();
this.family = new HashMap<String,HashSet<String>>();
this.order = new HashMap<String,HashSet<String>>();
try {
BufferedReader R = new BufferedReader(new FileReader(fileName));
String line;
while (true) {
line = R.readLine();
if (line == null) break;
if (firstLine) { // ignore the first line, it's not a bird
firstLine = false;
continue;
}
String[] fields = line.split(",");
if (!fields[1].equalsIgnoreCase("species")) continue; // ignore all but species records
birdnames.put(fields[0],fields[4]); // add this bird to name table
// extract the order name from fields[6]
String ordername = fields[6];
if (!order.containsKey(ordername)) { // if needed, create first-time order set
order.put(ordername,new HashSet<String>());
}
order.get(ordername).add(fields[0]); // new order member by number for lookup
// extract the family name from fields[7] -- removing quotes first if needed
String famname = fields[7].replace("\"","");
if (!family.containsKey(famname)) { // if needed, create first-time family set
family.put(famname,new HashSet<String>());
}
family.get(famname).add(fields[0]); // new family member by number for lookup
}
}
catch (IOException e) { System.out.println("Stack trace: " + e); }
}
...
}
我之前从未使用过HashSets,这是我们给定信息的一部分。我们的任务是创建一个列表页面和特定于每只鸟的页面以及两者之间的链接。我只是无法正确显示最后两个值。有人可以帮忙吗?
答案 0 :(得分:0)
在这里,您对所有值,鸟类,家庭和秩序使用相同的键:
while(iterator.hasNext()){
Object key = iterator.next();
String value = birdnames.get(key);
HashSet fam = family.get(key);
HashSet ord = order.get(key);
}
但是你用不同的键初始化它们:
// extract the order name from fields[6]
String ordername = fields[6];
if (!order.containsKey(ordername))
{ // if needed, create first-time order set
order.put(ordername, new HashSet<>());
}
order.get(ordername).add(fields[0]); // new order member by number for lookup
这里的密钥是字段[6]而不是鸟名密钥。
如果您想继续使用相同的密钥,可以对订单执行以下操作:
if (!order.containsKey(fields[0]))
{
order.put(fields[0], new HashSet<>());
}
order.get(fields[0]).add(fields[6]);
然后你可以使用:
HashSet ord = order.get(key);
您将收到该鸟名的所有订单。
如果你不想改变它并仍然使用相同的键,你可以做类似下面的事情,但是这是非常气馁的,因为它破坏了首先使用地图的目的:
Set<String> ord = new HashSet<>();
for (String tmp : order.keySet())
{
if (order.get(tmp).contains(key))
ord.add(tmp);
}
此处ord将包含&#34;键&#34;。
的所有订单正如您所看到的,如果您没有切换价值和&#34;键&#34;那么您需要做更多冗余的工作。