Intern — Junior
Have you worked with the Set collection in your projects?
sobes.tech AI
Answer from AI
Yes, in my projects I have used the Set collection in Java to store unique elements without a specific order. For example, when I needed to quickly check for the presence of an element or remove duplicates from a list.
Example of usage:
import java.util.HashSet;
import java.util.Set;
public class Example {
public static void main(String[] args) {
Set<String> uniqueNames = new HashSet<>();
uniqueNames.add("Anna");
uniqueNames.add("Ivan");
uniqueNames.add("Anna"); // duplicate will not be added
for (String name : uniqueNames) {
System.out.println(name);
}
}
}
In this example, HashSet automatically excludes duplicate values, which is convenient for working with unique data.