Java 查找字符串哈希集的组合
我有一个HashMap。字符串存储此人的姓名,哈希集存储此人的好友列表Java 查找字符串哈希集的组合,java,dictionary,set,combinations,Java,Dictionary,Set,Combinations,我有一个HashMap。字符串存储此人的姓名,哈希集存储此人的好友列表 KEY<STRING> VALUE<HASHSET> Dave Steve Steve Dave Bob Dalton Dalton Bob, Sue Anne Sue Sue Dalton, Anne 另一个与此主题不符的解决方案是找到好友列表的所有可能组合。e、 g.如果道尔顿的朋友列表
KEY<STRING> VALUE<HASHSET>
Dave Steve
Steve Dave
Bob Dalton
Dalton Bob, Sue
Anne Sue
Sue Dalton, Anne
另一个与此主题不符的解决方案是找到好友列表的所有可能组合。e、 g.如果道尔顿的朋友列表中有鲍勃、苏和戴夫,我可以找到一种方法,找到所有可能的双向友谊组合记住,顺序不重要:
Bob Sue
Bob Dave
Sue Dave
但是,我不知道如何编写代码。有什么建议吗?这里有一个伪代码方法,我可以在以后查找Java 假设在每次添加时,朋友的所有朋友都正确匹配 获取这两个新输入,并创建一个临时集合,其中包含他们的所有好友以及输入值 对于临时集合中的每个值,将每个其他值添加为好友。集合应该只维护唯一的值,但如果需要,可以显式检查 这可能不是每个步骤中最有效的解决方案,添加的内容有一半是重复的,但这应该是一个起点
Func (friend1, friend2)
tempSet = masterSet(friend1).Hashset
UNION
masterSet(friend2).Hashset
UNION
friend1, friend2
foreach (friend in tempSet)
foreach(otherFriend in tempSet - friend)
friend.AddFriend(otherFriend)
otherFriend.AddFriend(friend)
下面是一个伪代码方法,我可以在以后查找Java 假设在每次添加时,朋友的所有朋友都正确匹配 获取这两个新输入,并创建一个临时集合,其中包含他们的所有好友以及输入值 对于临时集合中的每个值,将每个其他值添加为好友。集合应该只维护唯一的值,但如果需要,可以显式检查 这可能不是每个步骤中最有效的解决方案,添加的内容有一半是重复的,但这应该是一个起点
Func (friend1, friend2)
tempSet = masterSet(friend1).Hashset
UNION
masterSet(friend2).Hashset
UNION
friend1, friend2
foreach (friend in tempSet)
foreach(otherFriend in tempSet - friend)
friend.AddFriend(otherFriend)
otherFriend.AddFriend(friend)
您描述的第二个解决方案相当于。你的朋友最终成为一组人,每个人都是该组其他人的朋友,而不是其他人 实现此数据结构的棘手部分是,当您发现不同集合中的两个人是朋友时,合并两个集合 这是一个幼稚的实现:
public class DisjointFriendSet {
private final Map<String, Set<String>> personToFriends = new HashMap<>();
/**
* Includes the person themselves in their group of friends.
*
* If no friendships have been registered for this person, then returns a set
* containing just themselves.
*
* @param person
* @return
*/
public Set<String> getFriends(String person) {
if(personToFriends.containsKey(person)) {
return personToFriends.get(person);
} else {
final Set<String> result = new HashSet<>();
result.add(person);
return result;
}
}
public void addFriendship(String person1, String person2) {
final Set<String> friends1 = getFriends(person1);
final Set<String> friends2 = getFriends(person2);
if(friends1 == friends2) {
return;
} else {
personToFriends.put(person1, friends1);
friends1.addAll(friends2);
for(String person: friends2) {
personToFriends.put(person, friends1);
}
}
}
/**
*
* @return All unique friendship groups
*/
public Collection<Set<String>> getAllFriendshipGroups() {
return new HashSet<>(personToFriends.values());
}
public static void main(String[] args) {
final DisjointFriendSet disjointFriendSet = new DisjointFriendSet();
disjointFriendSet.addFriendship("Alice","Beowulf");
disjointFriendSet.addFriendship("Charity","Donald");
disjointFriendSet.addFriendship("Eduardo","Frank");
disjointFriendSet.addFriendship("Grendel","Harriet");
System.out.println("Friendship groups: "+disjointFriendSet.getAllFriendshipGroups());
System.out.println("Adding friendship between Grendel and Beowulf");
disjointFriendSet.addFriendship("Grendel","Beowulf");
System.out.println("Friendship groups: "+disjointFriendSet.getAllFriendshipGroups());
System.out.println();
for(String person: new String[]{"Alice","Beowulf","Charity","Donald","Eduardo","Frank","Grendel","Harriet","Zod"}) {
System.out.println(person+"'s friends: "+disjointFriendSet.getFriends(person));
}
}
}
您描述的第二个解决方案相当于。你的朋友最终成为一组人,每个人都是该组其他人的朋友,而不是其他人 实现此数据结构的棘手部分是,当您发现不同集合中的两个人是朋友时,合并两个集合 这是一个幼稚的实现:
public class DisjointFriendSet {
private final Map<String, Set<String>> personToFriends = new HashMap<>();
/**
* Includes the person themselves in their group of friends.
*
* If no friendships have been registered for this person, then returns a set
* containing just themselves.
*
* @param person
* @return
*/
public Set<String> getFriends(String person) {
if(personToFriends.containsKey(person)) {
return personToFriends.get(person);
} else {
final Set<String> result = new HashSet<>();
result.add(person);
return result;
}
}
public void addFriendship(String person1, String person2) {
final Set<String> friends1 = getFriends(person1);
final Set<String> friends2 = getFriends(person2);
if(friends1 == friends2) {
return;
} else {
personToFriends.put(person1, friends1);
friends1.addAll(friends2);
for(String person: friends2) {
personToFriends.put(person, friends1);
}
}
}
/**
*
* @return All unique friendship groups
*/
public Collection<Set<String>> getAllFriendshipGroups() {
return new HashSet<>(personToFriends.values());
}
public static void main(String[] args) {
final DisjointFriendSet disjointFriendSet = new DisjointFriendSet();
disjointFriendSet.addFriendship("Alice","Beowulf");
disjointFriendSet.addFriendship("Charity","Donald");
disjointFriendSet.addFriendship("Eduardo","Frank");
disjointFriendSet.addFriendship("Grendel","Harriet");
System.out.println("Friendship groups: "+disjointFriendSet.getAllFriendshipGroups());
System.out.println("Adding friendship between Grendel and Beowulf");
disjointFriendSet.addFriendship("Grendel","Beowulf");
System.out.println("Friendship groups: "+disjointFriendSet.getAllFriendshipGroups());
System.out.println();
for(String person: new String[]{"Alice","Beowulf","Charity","Donald","Eduardo","Frank","Grendel","Harriet","Zod"}) {
System.out.println(person+"'s friends: "+disjointFriendSet.getFriends(person));
}
}
}
因此,如果道尔顿与鲍勃、苏和戴夫是朋友,那么你想让鲍勃与苏和戴夫成为朋友,让苏与鲍勃和戴夫成为朋友,让戴夫与鲍勃和苏成为朋友,以及让他们三人都与道尔顿成为朋友?@BrianJ是的,没错,你是否仅限于使用HashMap精确定义关系?@slambeth否,我可以使用其他结构,而不是数组/数组列表来存储数据。也不一定是这样。听起来好像需要某种类型的图形,我希望使用某种递归函数来添加朋友的朋友,但我没有足够的公式来给出答案。因此,如果道尔顿与鲍勃、苏和戴夫是朋友,那么你想让鲍勃与苏和戴夫成为朋友,苏与鲍勃和戴夫成为朋友,Dave是Bob和Sue的朋友,他们三人都是Dalton的朋友?@BrianJ是的,这是正确的,你是否仅限于使用HashMap来定义关系?@slambeth否,我可以使用其他结构,而不是数组/数组列表来存储数据。它也不一定是这样。听起来好像需要某种类型的图,我希望有某种类型的递归函数来添加朋友的朋友,但我没有足够的公式来给出答案。