从树形图对象中提取键 [英] extracting keys from treemap object

查看:125
本文介绍了从树形图对象中提取键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面这段代码中,我将一个人的firstname和emailId保存在一个hashmap中。我希望按照升序打印以'gmail.com'结尾的emailId的条目的名字。为此,我使用了java的TreeMap类。

In the following piece of code, I am saving firstname and emailId of a person in a hashmap.I wish to print the firstname of the entries that have emailId ending with 'gmail.com' in ascending order. for that I have used TreeMap class of java.

但问题是在emailId模式匹配的地方打印键。

but the problem is printing the keys where emailId pattern matches..

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;
        }
    }
}
}

谢谢提前。

Thanks in advance.

推荐答案

你抛弃了你的迭代器的结果。保留条目,您可以访问打印的键和值:

You're throwing away the results from your iterator. Keep the entry and you can access the key and value for printing:

Iterator<Map.Entry<String, String>> i = emailDetailTree.entrySet().iterator();
while(i.hasNext()) {
    Map.Entry<String, String> entry = i.next();
    ...

这篇关于从树形图对象中提取键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆