Junior — Middle
Tell about your experience with data structures like Set and how you have used them in your projects.
sobes.tech AI
Answer from AI
In my projects, I often used data structures like Set to store unique elements without duplication. For example, when processing user data, when it was necessary to save a list of unique email addresses or identifiers.
In Java, I most often used HashSet for quick access and checking the presence of an element, as it provides add, remove, and search operations in amortized O(1) time. When the insertion order was important, I used LinkedHashSet.
Example of using HashSet:
import java.util.HashSet;
public class Example {
public static void main(String[] args) {
HashSet<String> emails = new HashSet<>();
emails.add("user1@example.com");
emails.add("user2@example.com");
emails.add("user1@example.com"); // will not be added, as it already exists
System.out.println("Unique email addresses: " + emails);
}
}
This approach helps efficiently filter out duplicate data and quickly check for the presence of elements.