Java 哈希集搜索

发布于 2024-11-14 06:26:43 字数 1614 浏览 2 评论 0原文

下午好在

Java中,我有HashSet,其中包含对象User的列表,其具有属性:

  • 电子邮件
  • 机器名

现在我的哈希集具有以下值(上述对象的列表)

email            | group   | machinename
----------------------------------------
[email protected] | hewitt  | AP1
[email protected] | test    | AP1
[email protected]   | test    | AP1
[email protected]       | test    | AP1
[email protected] | project | AP1
[email protected]       | project | AP1

现在我必须找到那些具有相同的记录电子邮件和机器,但组名称不同,在上面的情况下是:

[email protected] (which has "project" and "test" group)
[email protected] (which has "hewitt" and "test" groups)

How can I find that using java code?

Good afternoon

In Java, I have HashSet which contain list of Object User which has properties:

  • email
  • group
  • machinename

now my hashset has following values (list of above object)

email            | group   | machinename
----------------------------------------
[email protected] | hewitt  | AP1
[email protected] | test    | AP1
[email protected]   | test    | AP1
[email protected]       | test    | AP1
[email protected] | project | AP1
[email protected]       | project | AP1

Now I have to find those records which has same email and machine but different group name which in above case are:

[email protected] (which has "project" and "test" group)
[email protected] (which has "hewitt" and "test" groups)

How can I find that using java code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘴硬脾气大 2024-11-21 06:26:43

这将完全满足您的要求:

Set<User> users = new HashSet<User>();
// ...

Map<String, List<User>> hits = new HashMap<String, List<User>>(); 

for (User user : users) {
    String key = user.getMachineName() + user.getEmail();
    List<User> list = hits.get(key);
    if (list == null) {
        list = new ArrayList<User>();
        hits.put(key, list);
    }
    list.add(user);
}

// Users are now grouped by their "machine name + email" as a single key

for (Map.Entry<String, List<User>> hit : hits.entrySet()) {
    if (hit.getValue().size() < 2) continue;
    System.out.println("These users share the same email and machine name: " 
        + hit.getValue()); // hit.getValue() is an ArrayList<User>
}

This will do exactly what you want:

Set<User> users = new HashSet<User>();
// ...

Map<String, List<User>> hits = new HashMap<String, List<User>>(); 

for (User user : users) {
    String key = user.getMachineName() + user.getEmail();
    List<User> list = hits.get(key);
    if (list == null) {
        list = new ArrayList<User>();
        hits.put(key, list);
    }
    list.add(user);
}

// Users are now grouped by their "machine name + email" as a single key

for (Map.Entry<String, List<User>> hit : hits.entrySet()) {
    if (hit.getValue().size() < 2) continue;
    System.out.println("These users share the same email and machine name: " 
        + hit.getValue()); // hit.getValue() is an ArrayList<User>
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文