Java list to redo the implementation of the way _java

Source: Internet
Author: User
The list in Java can contain duplicate elements (hash code and equals), then there are two ways to do the list redo:
Solution One: can be implemented through HashSet, the code is as follows:
Copy Code code as follows:

Class Student {
Private String ID;
private String name;
Public Student (string ID, string name) {
Super ();
This.id = ID;
THIS.name = name;
}
@Override
Public String toString () {
Return "Student [id=" + ID + ", name=" + name + "]";
}
@Override
public int hashcode () {
final int prime = 31;
int result = 1;
result = Prime * result + ((id = = null) 0:id.hashcode ());
result = Prime * result + ((name = = null) 0:name.hashcode ());
return result;
}
@Override
public boolean equals (Object obj) {
if (this = = obj) {
return true;
}
if (obj = = null) {
return false;
}
if (GetClass ()!= Obj.getclass ()) {
return false;
}
Student other = (Student) obj;
if (id = = NULL) {
if (other.id!= null) {
return false;
}
else if (!id.equals (other.id)) {
return false;
}
if (name = = null) {
if (other.name!= null) {
return false;
}
else if (!name.equals (Other.name)) {
return false;
}
return true;
}
}

Hashcode and equals two methods must be implemented, and for a while we will see why we must implement
The specific operation code is as follows:
Copy Code code as follows:

private static void Removelistduplicateobject () {
list<student> list = new arraylist<student> ();
for (int i = 0; i < i++) {
Student Student = new Student ("id", "name");
List.add (student);
}
System.out.println (Arrays.tostring (List.toarray ()));
set<student> set = new hashset<student> ();
Set.addall (list);
System.out.println (Arrays.tostring (Set.toarray ()));
List.removeall (list);
Set.removeall (set);
System.out.println (Arrays.tostring (List.toarray ()));
System.out.println (Arrays.tostring (Set.toarray ()));
}

Calling code:
Copy Code code as follows:

public static void Main (string[] args) {
Removelistduplicateobject ();
}

With HashSet, why do you have to overwrite the hashcode and equals two methods?
We view the HashSet add Operation source code as follows:
Copy Code code as follows:

Public boolean Add (E e) {
Return Map.put (E, PRESENT) ==null;
}

When HashMap is invoked, we look at the put action of the HashMap:
Copy Code code as follows:

Public V-Put (K key, V value) {
if (key = null)
return Putfornullkey (value);
int hash = hash (Key.hashcode ());
int i = indexfor (hash, table.length);
for (entry<k,v> e = table[i]; e!= null; e = e.next) {
Object K;
if (E.hash = = Hash && ((k = e.key) = = Key | | key.equals (k))) {
V oldValue = E.value;
E.value = value;
E.recordaccess (this);
return oldValue;
}
}
modcount++;
AddEntry (hash, key, value, I);
return null;
}

It is to be noted that:
Copy Code code as follows:

if (E.hash = = Hash && ((k = e.key) = = Key | | key.equals (k))) {
......
}

This means that the hash code is equal and equals (= =).
Complexity: Side traversal can be, O (n)
Scenario Two: Directly traverse the list for contains and add operations
The code is as follows:
Copy Code code as follows:

private static void Removelistduplicateobjectbylist () {
list<student> list = new arraylist<student> ();
for (int i = 0; i < i++) {
Student Student = new Student ("id", "name");
List.add (student);
}
System.out.println (Arrays.tostring (List.toarray ()));
list<student> Listuniq = new arraylist<student> ();
for (Student student:list) {
if (!listuniq.contains (student)) {
Listuniq.add (student);
}
}
System.out.println (Arrays.tostring (Listuniq.toarray ()));
List.removeall (list);
Listuniq.removeall (Listuniq);
System.out.println (Arrays.tostring (List.toarray ()));
System.out.println (Arrays.tostring (Listuniq.toarray ()));
}

Other equivalent above.
Degree of complexity:
While traversing, while calling the Contains method, we look at the source code as follows:
Copy Code code as follows:

Public Boolean contains (Object o) {
return IndexOf (O) >= 0;
}
public int indexOf (Object o) {
if (o = = null) {
for (int i = 0; i < size; i++)
if (elementdata[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (O.equals (Elementdata[i]))
return i;
}
return-1;
}

You can see and do a traversal of the new list. That is, 1+2+....+n is so complex as O (n*n)
Conclusion:
The efficiency of the scheme is high, that is, the HashSet method is used to carry out heavy operation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.