【发布时间】:2018-09-16 02:12:36
【问题描述】:
在下面的代码中,我将一个人的名字和 emailId 保存在 hashmap 中。我希望按升序打印 emailId 以“gmail.com”结尾的条目的名字。为此,我使用了 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;
}
}
}
}
提前致谢。
【问题讨论】: