我有一张地图:
private HashMap<String, CompactDisc> database;每个CompactDisc对象都有一个艺术家,我希望用户输入一个字符串并搜索哈希映射并打印出包含该字符串的所有值。 因此,如果我搜索"Jackson" ,我会得到杰克逊5和迈克尔杰克逊(假设他们在CD中)。
I have a map:
private HashMap<String, CompactDisc> database;Every CompactDisc object has an artist, and I want to have a user enter a string and search through the hash-map and print out ALL values containing the string. So if I searched "Jackson", I would get both The Jackson 5 and Michael Jackson (assuming they are in the CD).
最满意答案
迭代HashMap的值并检查CompactDisc的艺术家名称是否包含指定的字符串。
for (CompactDisc cd : database.values()) { if(cd.getArtist().contains(searchString)){ System.out.println(cd.getArtist()); } }假设CompactDisk有一个返回String的getArtist()方法。 而searchString是用户指定的字符串。
Iterate through the values of the HashMap and check if the CompactDisc's artist name contains the specified string.
for (CompactDisc cd : database.values()) { if(cd.getArtist().contains(searchString)){ System.out.println(cd.getArtist()); } }assuming CompactDisk has a getArtist() method that returns a String. And searchString is the string specified by user.
更多推荐
发布评论