Why is there no get(int index) in java.util.Set?

Asked 1 years ago, Updated 1 years ago, 139 views

I understand that there is no get method in Set for a reason, but please explain the reason. I think it's good to put elements in the set, but I don't know a good way to get a single item from there.

So if you know I want the first element, you can use set.iterator.next(), but in other cases, do I have to use an array that searches for specific indexes?

What is a good way to retrieve data from Set? And please tell me the reason why there is no get method.

java data-structuress collections set

2022-09-22 22:24

1 Answers

Set has no order. Although implemented on some interfaces, such as java.util.SortedSet, it is not a common property of Set.
If you want to search sequentially, I recommend using the list.


2022-09-22 22:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.