A
A
Andrey Kulagin2020-08-21 16:23:46
Java
Andrey Kulagin, 2020-08-21 16:23:46

How to combine users with a common email?

Can someone tell me, I'm doing a task where you need to combine users with a common email
using two hashmaps, I did one, where the key is the user's email, and the value is the user's name, but in the same place the values ​​​​are overwritten with a common key, which I'm doing wrong ?
There are n users, each of them corresponds to a list of emails
(all users have m emails).
For example:
user1 ->[email protected],[email protected],[email protected] ([email protected],[email protected],[email protected])
user2 ->[email protected], [email protected] ([email protected],[email protected])
user3 ->[email protected],[email protected] ([email protected],[email protected])
user4 ->[email protected] pisem.net,[email protected] ([email protected],[email protected])
user5 ->[email protected]
It is believed that if two users have a common email, then this is
the same user. It is required to build
and implement an algorithm that performs user merging. The output
should be a list of users with their emails (same as the
input).
Any of the
original names can be used as the merged user name. The list of user emails should contain only
unique emails.
Parameters n and m are arbitrary, the length of a particular list of emails is
not limited in any way.
It is required that the asymptotic running time of the obtained solution be
linear, or close to linear.
Possible answer to the problem in the given example:
user1 ->[email protected],[email protected],[email protected],[email protected],[email protected] ([email protected],[email protected],[email protected] ,[email protected],[email protected])
user3 ->[email protected],[email protected] ([email protected],[email protected])

public class Email implements Sort {
public void convert( List source) {
Map map1 = new HashMap<>();
Mapmap2 = new HashMap<>();
for (int i = 0; i < source.size(); i++) {
String[] list1 = source.get(i).getUser().split(":");
String[] list2 = list1[1].split(",");
for (int j = 0; j < list2.length; j++) {
//key - user's email
String key = list2[j];
String value = list1[0];
map1 put(key, value);
}
}

for (String key : map1.keySet()) {
System.out.println(key + " " + map1.get(key));
}
}

public static void main(String[] args) {
Email email = new Email();
List source = Arrays.asList(
new User("user1:[email protected],[email protected],[email protected]"),
new User("user2:[email protected],[email protected] "),
new User("user3:[email protected],[email protected]"),
new User("user4:[email protected],[email protected]"),
new User("user5:xyz @pisem.net"
email.convert(source);
}
}

Outputs this:
[email protected] user4
[email protected] user4
[email protected] user1
[email protected] user1
[email protected] user3
[email protected] user2
[email protected] user5

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan., 2020-08-21
@LaRN

You can try like this.
In the first hashmap, you can store a mail-user pair, in the second hashmap, a user - user pair.
Example:
u1-m1, m2
u2-m3, m2
u3-m4
u5-m4, m5, m1
Go through the list, process the pair u1-m1.
m1-u1 -> heshmap1
Further similarly for the second pair:
m2-u1 -> heshmap1
For the next pair:
m3-u2 ->
heshmap1 the key
m2 is already in heshmap1, then you don’t need to write anything there, but you need to add the
value u1 to heshmap2 for the key u2, because in heshmap1 the value for the key m2 is u1.
Those. we have u2-u1 -> heshmap2
Next
m4-u3 -> heshmap1
Then, because the m4 key is already in heshmap1, we go to heshmap2 again:
u5-u3 -> heshmap2
Next
m5-u5-> heshmap1
And the last step is u5 - m1, here is a nuance, heshmap1
already has the m1 key, which means we go to heshmap2, but there is already one there the key is u5, so you need to delete the u5-u3 pair already in heshmap2 and add two new ones instead:
u3-u1-> hashmap2
u5-u1-> hashmap2 We
take the value of u1 from hashmap1[m1]
As a result, we have:
heshmap1
m1-u1
m2 -u1
m3-u2
m4-u3
m5-u5
heshmap2
u2-u1
u3-u1
u5-u1
Next, go through the heshmap1 keys (this is email) and determine for each of them a value from heshmap2 (these are users).
mail = heshmap1[heshmap1.key[i]]
user = heshmap2[heshmap1[mail]]
If there is no value in heshmap2, then we take the user from heshmap1.key[i]
As a result, by combining two heshmaps we get the following list:
u1- m1
u1-m2
u1-m3
u1-m4
u1-
m5 All emails belong to the same user.

W
Wataru, 2020-08-21
@wataru

You have already asked this question .
I answered you there that this is a classical task for finding connected components in a graph. Google DFS, connectivity components, graphs. I even gave a bunch of technical details there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question