Junior — Middle
What methods of data implementation do you know?
sobes.tech AI
Answer from AI
In Java, a set of data is usually implemented using the Set interface. Main implementations:
HashSet— based on a hash table, provides fast access (O(1) on average), does not guarantee element order.LinkedHashSet— extendsHashSet, preserves insertion order.TreeSet— based on a red-black tree, stores elements in sorted order, operations have a complexity of O(log n).
Example of using HashSet:
Set<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
set.add("apple"); // duplicate will not be added
for (String fruit : set) {
System.out.println(fruit);
}
It is also possible to implement a set based on other structures, such as lists with a check for uniqueness, but this is less efficient.