gpt4 book ai didi

java - 我应该使用 "Set"还是 "List"

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:45:29 27 4
gpt4 key购买 nike

我正在学习 Java.util.Collection 并遇到了以下问答。

Consider the four core interfaces, Set, List, Queue, and Map. For each of the following four assignments, specify which of the four core interfaces is best-suited, and explain how to use it to implement the assignment.

1) Whimsical Toys Inc (WTI) needs to record the names of all its employees. Every month, an employee will be chosen at random from these records to receive a free toy.

教程提供的答案是

Use a List. Choose a random employee by picking a number between 0 and size()-1.

但我只是想知道不应该使用 Set 而不是 List 吗?因为员工是独一无二的个体,我们不希望多次输入同一员工。我的想法是正确的还是我错过了什么?有没有人有什么建议?在此先感谢您的帮助!

最佳答案

But I was just wondering shouldn't Set be used instead of List? Cause employees are unique individuals and we don't want the same employee to be entered multiple times.

虽然员工是独特的个体,但他们的名字可能并不独特。使用 Set 会消除重复项,错误地删除具有相同姓名的不同员工的姓名。

关于java - 我应该使用 "Set"还是 "List",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36122638/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com