在下面的代码中,我将一个人的firstname和emailId保存在hashmap中。我希望打印以' gmail.com'结尾的emailId结尾的条目的名字。按升序排列。因为我使用了Java的TreeMap类。
但问题是打印emailId模式匹配的键..
public class RegExSolution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
Map<String, String> emailDetail = new HashMap<>();
for (int a0 = 0; a0 < N; a0++) {
String firstName = in.next();
String emailID = in.next();
emailDetail.put(firstName, emailID);
}
Map<String, String> emailDetailTree = new TreeMap<>(emailDetail);
Iterator i = emailDetailTree.entrySet().iterator();
while (i.hasNext()) {
i.next();
if (Pattern.matches("[a-z]+@gmail\\.com$", "here I wish to get emaild from entry(i.e value from TreeMap)")) {
System.out.println("here I wish to print the firstname(i.e. key from TreeMap) ");
} else {
continue;
}
}
}
}
提前致谢。
答案 0 :(得分:2)
你丢弃了迭代器的结果。保留输入,您可以访问打印的键和值:
Iterator<Map.Entry<String, String>> i = emailDetailTree.entrySet().iterator();
while(i.hasNext()) {
Map.Entry<String, String> entry = i.next();
...