1, In the development work , We need to judge sometimes List Does the collection contain duplicate elements
2, We don't need to find repetitive elements , We just need to return one Boolean Type is OK
3, If you use loop traversal , Will consume a lot of performance
4, We know ,Set There are no duplicate data stored in , So you just need to List Set to Set aggregate , Then compare them
The code is as follows :
package com.test; import java.util.ArrayList; import java.util.HashSet; import
java.util.List; public class Test { public static void main(String[] args) { //
Define a list aggregate List<String> list = new ArrayList<String>(); // to list Add data to it list.
add("a"); list.add("b"); list.add("a"); list.add("c"); // take list Into set HashSet
<String> set = new HashSet<>(list); // compare list And set Length of Boolean result = set.
size() == list.size() ? true : false; // Output comparison results System.out.println(result); } }
The result is, of course :false
If you find a deficiency in your reading , Welcome to leave a message !!!
Technology