Before the interview was asked about how the Java to the weight of the problem, did not pay much attention to the project today just used, so record.
Entity class:
/*** User Class*/classuser{PrivateString username;PrivateString password; PublicUser (string Username, string password) { This. Username =username; This. Password =password; } PublicString GetUserName () {returnusername; } Public voidSetusername (String username) { This. Username =username; } PublicString GetPassword () {returnpassword; } Public voidSetPassword (String password) { This. Password =password; }}
Test class:
Public Static voidMain (string[] args) {ArrayList<User> arrayList =NewArraylist<>(); LongCurrenttimemillis =System.currenttimemillis (); intf = 100000; for(inti = 0; i < F; i++) {Arraylist.add (NewUser ("" +i, "7878")); } Iterator<User> iterator =Arraylist.iterator (); Set<String> HashSet =NewHashset<>(); LinkedList<User> NewList =NewLinkedlist<>(); //The First kind: Set set to go heavy, do not change the original order, according to the username unique identity to remove the weight while(Iterator.hasnext ()) {User U=Iterator.next (); //properties that cannot hold the same value according to the set if(!Stringutils.isempty (U.getusername ())) { if(Hashset.add (U.getusername ())) {//Put it back in the LinkedListnewlist.add (U); } } } LongCurrenttimemillis1_1 =System.currenttimemillis (); System.out.println ("The first set of sets goes heavy and does not change the original order:" + (Currenttimemillis1_1-currenttimemillis)); //The second type: After the traversal of the judgment assigned to another list collection, using the Contains method of the list loop traversalList<user> listnew=NewArraylist<>(); for(User str:arraylist) {if(!listnew.contains (str)) {Listnew.add (str); } } LongCurrenttimemillis1_2 =System.currenttimemillis (); System.out.println ("The second type: After the traversal of the judgment assigned to another list collection, using the Contains method of the list loop traversal:" + (Currenttimemillis1_2-currenttimemillis1_1)); //The third type: set to remove weightSet set =NewHashSet (); List<String> listnew2=NewArraylist<>(); Set.addall (arrayList); Listnew2.addall (set); LongCurrenttimemillis1_3 =System.currenttimemillis (); System.out.println ("Third type: Set de-weight" + (Currenttimemillis1_3-currenttimemillis1_2)); //Fourth type: Set de-weight (reduced to one line)List<string> listnew3=NewArraylist<> (NewHashSet (arrayList)); LongCurrenttimemillis1_4 =System.currenttimemillis (); System.out.println ("Fourth type: Set de-weight (reduced to one line)" + (Currenttimemillis1_4-currenttimemillis1_3)); //Fifth: de-weight and sort by natural order /*TreeSet TreeSet = new TreeSet (arrayList); List templist = new ArrayList (); Templist.addall (TreeSet); List<user> listnew4=new arraylist<> (New treeset<user> (ArrayList)); Long currenttimemillis1_5 = System.currenttimemillis (); System.out.println (currenttimemillis1_5-currenttimemillis1_4);*/ //The sixth type: double for loop, go to heavy for(inti = 0; I < Arraylist.size ()-1; i + +){ for(intj = arraylist.size ()-1; J > i; J--){ if(Arraylist.get (j). Equals (Arraylist.get (i))) {Arraylist.remove (j); } } } LongCurrenttimemillis1_6 =System.currenttimemillis (); System.out.println ("Sixth type: double for loop, de-weight" + (Currenttimemillis1_6-currenttimemillis1_4)); //Seventh: The use of hashset can not add duplicate data characteristics because HashSet can not guarantee the order of addition, so only as a criterionHashset<user> Set2 =NewHashset<>(Arraylist.size ()); List<User> result =NewArraylist<>(Arraylist.size ()); for(User str3:arraylist) {if(Set2.add (STR3)) {Result.add (STR3); }} arraylist.clear (); Arraylist.addall (result); LongCurrenttimemillis1_7 =System.currenttimemillis (); System.out.println ("The seventh type: the use of HashSet cannot add duplicate data because HashSet cannot guarantee the order of addition, so it can only be judged as a condition" + (Currenttimemillis1_7-currenttimemillis1_6)); //Eighth: The ability to use Linkedhashset to add duplicate data and to guarantee the order of additionsLinkedhashset<user> Set5 =NewLinkedhashset<>(Arraylist.size ()); Set5.addall (arrayList); Arraylist.clear (); Arraylist.addall (SET5); LongCurrenttimemillis1_8 =System.currenttimemillis (); System.out.println ("Eighth type: the ability to use Linkedhashset to add duplicate data and to guarantee the order of additions" + (Currenttimemillis1_8-currenttimemillis1_7)); }
Console output:
Tip: In the case of large amounts of data, it is best not to use the second and sixth, the speed is too slow
As for the efficiency problem, we should choose according to the actual situation.
Java's de-weight approach and efficiency issues