Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/com/school/faang/hashset/задача_3/ContactList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.school.faang.hashset.задача_3;

import java.util.HashSet;
import java.util.Set;

public class ContactList {

private final Set<String> contacts;

public ContactList() {
contacts = new HashSet<>();
}

public ContactList(Set<String> contacts) {
this.contacts = new HashSet<>(contacts);
}

public void addContact(String contact) {
contacts.add(contact);
}

public void removeContact(String contact) {
contacts.remove(contact);
}

public boolean hasContact(String contact) {
return contacts.contains(contact);
}

public static Set<String> findMutualContacts(ContactList contactList1, ContactList contactList2) {
Set<String> result = new HashSet<>(contactList1.contacts);
result.retainAll(contactList2.contacts);
return result;
}
}
14 changes: 13 additions & 1 deletion src/com/school/faang/hashset/задача_3/Solution.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package com.school.faang.hashset.задача_3;

import java.util.Collections;
import java.util.Set;

public class Solution {
public static void main(String[] args) {

User firstUser = new User("first");
User secondUser = new User("second");
User thirdUser = new User("third");
firstUser.addContact(secondUser);
thirdUser.addContact(secondUser);
thirdUser.addContact(firstUser);
firstUser.addContact(thirdUser);
Set<String> mutualContactsForFirstAndThird = User.findMutualContacts(firstUser, thirdUser);
System.out.println(mutualContactsForFirstAndThird.size());
mutualContactsForFirstAndThird.forEach(System.out::println);
}
}
53 changes: 53 additions & 0 deletions src/com/school/faang/hashset/задача_3/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.school.faang.hashset.задача_3;

import java.util.Set;

public class User {

private final String username;

private final ContactList contactList;

public User(String username) {
this.username = username;
contactList = new ContactList();
}

public User(String username, Set<String> contacts) {
this.contactList = new ContactList(contacts);
this.username = username;
}

public void addContact(String username) {
contactList.addContact(username);
}

public void addContact(User user) {
addContact(user.toString());
}

public void removeContact(String username) {
contactList.removeContact(username);
}

public void removeContact(User user) {
removeContact(user.toString());
}

public boolean hasContact(String username) {
return contactList.hasContact(username);
}

public boolean hasContact(User user) {
return hasContact(user.toString());
}

public static Set<String> findMutualContacts(User user1, User user2) {
return ContactList.findMutualContacts(user1.contactList, user2.contactList);
}

@Override
public String toString() {
return username;
}
}