Converting an array to a Set in Java: simple and efficient solution
Hello everyone! I'm new to Java and encountered a problem when working with arrays and collections. I need to convert an array of strings to a Set to remove duplicates. I know that arrays in Java have a fixed size, and Set is a collection that does not allow duplicate elements. But how do I properly perform this conversion?
Here's my current code:
```java
String[] array = {"apple", "banana", "apple", "orange", "banana"};
// How do I get a Set from this array?
```
I tried using Arrays.asList(), but I got a Set that doesn't work as expected. When I add elements, an exception occurs. Maybe I'm doing something wrong? I've heard there are several ways: through a for loop, through HashSet, through Collections.addAll(). Which one is the most correct and performant?