In the development process, the encounter to generate a list object, you need to check every object inside it. However, this list sometimes has duplicate data. Therefore, it is necessary to go heavy to avoid duplicate checksums.
1. Writing without type:
List listwithoutdup = new ArrayList (new HashSet (Listwithdup));
2. With type (for example, String type):
1 Java 7 The following wording:
list<string> listwithoutdup = new arraylist<string> (new Hashset<string> (ListWithDup));
2 Java 7 and above:
list<string> listwithoutdup = new arraylist<> (new Hashset<> (Listwithdup));
Sample Example:
Import java.util.ArrayList;
Import Java.util.HashSet;
Import java.util.List;
public class Test {public
static void Main (string[] args) {
list<string> listwithdup = new Arraylist<str Ing> ();
Listwithdup.add ("1");
Listwithdup.add ("2");
Listwithdup.add ("3");
Listwithdup.add ("1");
list<string> listwithoutdup = new arraylist<string> (new Hashset<string> (ListWithDup));
System.out.println ("List with DUP:" + listwithdup);
SYSTEM.OUT.PRINTLN ("List without DUP:" + listwithoutdup);
}
Sample Run Result:
List with Dup:[1, 2, 3, 1]
List without dup:[3, 2, 1]