Check if a value exists in ArrayList

后端 未结 7 2568
长发绾君心
长发绾君心 2020-11-27 13:37

How can I check if a value that is written in scanner exists in an ArrayList?

List lista = new ArrayList

        
7条回答
  •  暖寄归人
    2020-11-27 13:59

    Better to use a HashSet than an ArrayList when you are checking for existence of a value. Java docs for HashSet says: "This class offers constant time performance for the basic operations (add, remove, contains and size)"

    ArrayList.contains() might have to iterate the whole list to find the instance you are looking for.

提交回复
热议问题