For the database to go heavy is quite simple, just add the DISTINCT keyword on the query statement can be done, but for the NoSQL database distinct not necessarily support, this time we need to manually implement. Here is the simplest principle of data duplication: The hashcode comparison of strings, of course, the only indicator of the record is not the ID, but the data itself, we remove the ID to compare whether or not to repeat it.
Java Impersonation Code:
Package com.boonya.mongo.client;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map; public class Lovetest {public static void main (string[] args) {list<person> list=new arraylist<person> (
);
Person P=new person ("1", "Boonya", 20);
Person P2=new person ("2", "Boonya", 20);
Person P3=new person ("3", "Boonya", 20);
Person P4=new person ("4", "Boonya", 20);
List.add (P);
List.add (p2);
List.add (p3);
List.add (p4);
Map<string,object> map=new hashmap<string, object> (); for (person person:list) {if (!map.containskey () +person.tostring (). Hashcode ())) {Map.put (Person.tostring (). HASHC
Ode () + "", person);
} for (Object person:map.values ()) {person uniqueperson= person;
System.out.println (Uniqueperson.getid () + "" +uniqueperson);
Class person{private String ID;
private String name;
private int age;
Public String GetId () {return id; } public void SetId (String id) {this.id = ID;
Public String GetName () {return name;
public void SetName (String name) {this.name = name;
public int getage () {return age;
public void Setage (int age) {this.age = age;
Public person (string ID, string name, int age) {super ();
This.id = ID;
THIS.name = name;
This.age = age; @Override public String toString () {return ' person ' + (name!= null?)
Name= "+ name +", ":") + "age=" + Age + "]";
}
}
Test Results:
1 person [Name=boonya, age=20]
The case of the hash collision is not considered in this paper.
Java8 to weigh :
list<string> unique = List.stream (). Distinct (). Collect (Collectors.tolist ());