From 8fdd2d7647767bcb625ba35e533511f71ff0141b Mon Sep 17 00:00:00 2001 From: MichsSS-G Date: Fri, 20 Jun 2025 18:30:19 +0500 Subject: [PATCH 1/3] Added solution for task 1 --- .../LikeCounter.java" | 27 +++++++++++++++++++ .../Solution.java" | 16 ++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 "src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/LikeCounter.java" diff --git "a/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/LikeCounter.java" "b/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/LikeCounter.java" new file mode 100644 index 0000000..763532f --- /dev/null +++ "b/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/LikeCounter.java" @@ -0,0 +1,27 @@ +package com.school.faang.hashmap.задача_1; + +import java.util.*; + +public class LikeCounter { + private final Map countDict; + + public LikeCounter() { + countDict = new HashMap<>(); + } + + public void setLike(String videoID) { + int value = 0; + if (countDict.containsKey(videoID)) { + value = countDict.get(videoID); + } + countDict.put(videoID, ++value); + } + + public int getLikes(String videoID) { + return countDict.getOrDefault(videoID, 0); + } + + public List getVideosId() { + return new ArrayList<>(countDict.keySet()); + } +} diff --git "a/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/Solution.java" "b/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/Solution.java" index 577aa6a..78babab 100644 --- "a/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/Solution.java" +++ "b/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/Solution.java" @@ -1,7 +1,21 @@ package com.school.faang.hashmap.задача_1; +import java.util.*; + public class Solution { public static void main(String[] args) { - + String videoID1 = "imwbpodBWTE"; + String videoID2 = "Gpnd7f7L4xE"; + String videoID3 = "sPSlRTkFXao"; + LikeCounter likeCounter = new LikeCounter(); + likeCounter.setLike(videoID1); + likeCounter.setLike(videoID2); + likeCounter.setLike(videoID3); + likeCounter.setLike(videoID2); + likeCounter.setLike(videoID3); + likeCounter.setLike(videoID3); + for (String videoID : likeCounter.getVideosId()) { + System.out.println(videoID + " " + likeCounter.getLikes(videoID)); + } } } From fa052799673cef31fca487978db9e221c5f316ff Mon Sep 17 00:00:00 2001 From: MichsSS-G Date: Fri, 20 Jun 2025 18:35:26 +0500 Subject: [PATCH 2/3] Revert "Added solution for task 1" This reverts commit 8fdd2d7647767bcb625ba35e533511f71ff0141b. --- .../LikeCounter.java" | 27 ------------------- .../Solution.java" | 16 +---------- 2 files changed, 1 insertion(+), 42 deletions(-) delete mode 100644 "src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/LikeCounter.java" diff --git "a/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/LikeCounter.java" "b/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/LikeCounter.java" deleted file mode 100644 index 763532f..0000000 --- "a/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/LikeCounter.java" +++ /dev/null @@ -1,27 +0,0 @@ -package com.school.faang.hashmap.задача_1; - -import java.util.*; - -public class LikeCounter { - private final Map countDict; - - public LikeCounter() { - countDict = new HashMap<>(); - } - - public void setLike(String videoID) { - int value = 0; - if (countDict.containsKey(videoID)) { - value = countDict.get(videoID); - } - countDict.put(videoID, ++value); - } - - public int getLikes(String videoID) { - return countDict.getOrDefault(videoID, 0); - } - - public List getVideosId() { - return new ArrayList<>(countDict.keySet()); - } -} diff --git "a/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/Solution.java" "b/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/Solution.java" index 78babab..577aa6a 100644 --- "a/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/Solution.java" +++ "b/src/com/school/faang/hashmap/\320\267\320\260\320\264\320\260\321\207\320\260_1/Solution.java" @@ -1,21 +1,7 @@ package com.school.faang.hashmap.задача_1; -import java.util.*; - public class Solution { public static void main(String[] args) { - String videoID1 = "imwbpodBWTE"; - String videoID2 = "Gpnd7f7L4xE"; - String videoID3 = "sPSlRTkFXao"; - LikeCounter likeCounter = new LikeCounter(); - likeCounter.setLike(videoID1); - likeCounter.setLike(videoID2); - likeCounter.setLike(videoID3); - likeCounter.setLike(videoID2); - likeCounter.setLike(videoID3); - likeCounter.setLike(videoID3); - for (String videoID : likeCounter.getVideosId()) { - System.out.println(videoID + " " + likeCounter.getLikes(videoID)); - } + } } From 44b49886c4501f6432c11bd9b69384ab6933d1a6 Mon Sep 17 00:00:00 2001 From: MichsSS-G Date: Tue, 24 Jun 2025 20:45:39 +0500 Subject: [PATCH 3/3] Added solution for task 3 --- .../ContactList.java" | 35 ++++++++++++ .../Solution.java" | 14 ++++- .../User.java" | 53 +++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 "src/com/school/faang/hashset/\320\267\320\260\320\264\320\260\321\207\320\260_3/ContactList.java" create mode 100644 "src/com/school/faang/hashset/\320\267\320\260\320\264\320\260\321\207\320\260_3/User.java" diff --git "a/src/com/school/faang/hashset/\320\267\320\260\320\264\320\260\321\207\320\260_3/ContactList.java" "b/src/com/school/faang/hashset/\320\267\320\260\320\264\320\260\321\207\320\260_3/ContactList.java" new file mode 100644 index 0000000..e773658 --- /dev/null +++ "b/src/com/school/faang/hashset/\320\267\320\260\320\264\320\260\321\207\320\260_3/ContactList.java" @@ -0,0 +1,35 @@ +package com.school.faang.hashset.задача_3; + +import java.util.HashSet; +import java.util.Set; + +public class ContactList { + + private final Set contacts; + + public ContactList() { + contacts = new HashSet<>(); + } + + public ContactList(Set 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 findMutualContacts(ContactList contactList1, ContactList contactList2) { + Set result = new HashSet<>(contactList1.contacts); + result.retainAll(contactList2.contacts); + return result; + } +} diff --git "a/src/com/school/faang/hashset/\320\267\320\260\320\264\320\260\321\207\320\260_3/Solution.java" "b/src/com/school/faang/hashset/\320\267\320\260\320\264\320\260\321\207\320\260_3/Solution.java" index 3b56bed..5ef12c2 100644 --- "a/src/com/school/faang/hashset/\320\267\320\260\320\264\320\260\321\207\320\260_3/Solution.java" +++ "b/src/com/school/faang/hashset/\320\267\320\260\320\264\320\260\321\207\320\260_3/Solution.java" @@ -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 mutualContactsForFirstAndThird = User.findMutualContacts(firstUser, thirdUser); + System.out.println(mutualContactsForFirstAndThird.size()); + mutualContactsForFirstAndThird.forEach(System.out::println); } } diff --git "a/src/com/school/faang/hashset/\320\267\320\260\320\264\320\260\321\207\320\260_3/User.java" "b/src/com/school/faang/hashset/\320\267\320\260\320\264\320\260\321\207\320\260_3/User.java" new file mode 100644 index 0000000..c8f54d9 --- /dev/null +++ "b/src/com/school/faang/hashset/\320\267\320\260\320\264\320\260\321\207\320\260_3/User.java" @@ -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 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 findMutualContacts(User user1, User user2) { + return ContactList.findMutualContacts(user1.contactList, user2.contactList); + } + + @Override + public String toString() { + return username; + } +}