Why doesn't java.util.Set have get(int index)?

前端 未结 18 1287
长情又很酷
长情又很酷 2020-11-28 18:14

I\'m sure there\'s a good reason, but could someone please explain why the java.util.Set interface lacks get(int Index), or any similar get()

18条回答
  •  离开以前
    2020-11-28 18:43

    That's true, element in Set are not ordered, by definition of the Set Collection. So they can't be access by an index.

    But why don't we have a get(object) method, not by providing the index as parameter, but an object that is equal to the one we are looking for? By this way, we can access the data of the element inside the Set, just by knowing its attributes used by the equal method.

提交回复
热议问题