java list去重操作實(shí)現(xiàn)方式
更新時(shí)間:2012年12月05日 15:57:46 作者:
Java中的List是可以包含重復(fù)元素的(hash code 和equals),接下來將介紹兩種方式實(shí)現(xiàn)java list去重操作,感興趣的朋友可以參考下
Java中的List是可以包含重復(fù)元素的(hash code 和equals),那么對List進(jìn)行去重操作有兩種方式實(shí)現(xiàn):
方案一:可以通過HashSet來實(shí)現(xiàn),代碼如下:
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;
}
}
必須實(shí)現(xiàn)hashCode和equals兩個(gè)方法,一會我們會看為啥必須實(shí)現(xiàn)
具體的操作代碼如下:
private static void removeListDuplicateObject() {
List<Student> list = new ArrayList<Student>();
for (int i = 0; i < 10; 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()));
}
調(diào)用代碼:
public static void main(String[] args) {
removeListDuplicateObject();
}
利用HashSet進(jìn)行去重操作,為啥必須覆蓋hashCode和equals兩個(gè)方法呢?
我們查看HashSet的add操作源碼如下:
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
調(diào)用了HashMap進(jìn)行操作的,我們看HashMap的put操作:
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;
}
需要注意的是:
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
......
}
也就是說hash code相等且equals(==)。
復(fù)雜度:一邊遍歷即可,O(n)
方案二:直接遍歷一遍List進(jìn)行通過contains和add操作實(shí)現(xiàn)
代碼如下:
private static void removeListDuplicateObjectByList() {
List<Student> list = new ArrayList<Student>();
for (int i = 0; i < 10; 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()));
}
其他等同上面。
復(fù)雜度:
一邊遍歷,同時(shí)調(diào)用了contains方法,我們查看源碼如下:
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;
}
可以看到又對新的list做了一次遍歷操作。也就是1+2+....+n這樣復(fù)雜度為O(n*n)
結(jié)論:
方案一效率高,即采用HashSet的方式進(jìn)行去重操作
方案一:可以通過HashSet來實(shí)現(xiàn),代碼如下:
復(fù)制代碼 代碼如下:
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;
}
}
必須實(shí)現(xiàn)hashCode和equals兩個(gè)方法,一會我們會看為啥必須實(shí)現(xiàn)
具體的操作代碼如下:
復(fù)制代碼 代碼如下:
private static void removeListDuplicateObject() {
List<Student> list = new ArrayList<Student>();
for (int i = 0; i < 10; 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()));
}
調(diào)用代碼:
復(fù)制代碼 代碼如下:
public static void main(String[] args) {
removeListDuplicateObject();
}
利用HashSet進(jìn)行去重操作,為啥必須覆蓋hashCode和equals兩個(gè)方法呢?
我們查看HashSet的add操作源碼如下:
復(fù)制代碼 代碼如下:
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
調(diào)用了HashMap進(jìn)行操作的,我們看HashMap的put操作:
復(fù)制代碼 代碼如下:
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;
}
需要注意的是:
復(fù)制代碼 代碼如下:
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
......
}
也就是說hash code相等且equals(==)。
復(fù)雜度:一邊遍歷即可,O(n)
方案二:直接遍歷一遍List進(jìn)行通過contains和add操作實(shí)現(xiàn)
代碼如下:
復(fù)制代碼 代碼如下:
private static void removeListDuplicateObjectByList() {
List<Student> list = new ArrayList<Student>();
for (int i = 0; i < 10; 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()));
}
其他等同上面。
復(fù)雜度:
一邊遍歷,同時(shí)調(diào)用了contains方法,我們查看源碼如下:
復(fù)制代碼 代碼如下:
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;
}
可以看到又對新的list做了一次遍歷操作。也就是1+2+....+n這樣復(fù)雜度為O(n*n)
結(jié)論:
方案一效率高,即采用HashSet的方式進(jìn)行去重操作
您可能感興趣的文章:
- Java中對List去重 Stream去重的解決方法
- 如何實(shí)現(xiàn)java8 list按照元素的某個(gè)字段去重
- java中List對象列表實(shí)現(xiàn)去重或取出及排序的方法
- Java實(shí)現(xiàn)對兩個(gè)List快速去重并排序操作示例
- Java中List根據(jù)map的某個(gè)key去重的代碼
- java正則表達(dá)式實(shí)現(xiàn)提取需要的字符并放入數(shù)組【ArrayList數(shù)組去重復(fù)功能】
- Java List中數(shù)據(jù)的去重
- Java中List集合對象去重及按屬性去重的8種方法
- java list去重操作實(shí)現(xiàn)方式
- Java中List集合去除重復(fù)數(shù)據(jù)的方法匯總
相關(guān)文章
Jackson反序列化@JsonFormat 不生效的解決方案
這篇文章主要介紹了Jackson反序列化@JsonFormat 不生效的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
springboot+dubbo+zookeeper的簡單實(shí)例詳解
本文主要介紹了springboot+dubbo+zookeeper的簡單實(shí)例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
java中BigDecimal的介紹及使用教程BigDecimal格式化及BigDecimal常見問題
BigDecimal是Java在java.math包中提供的線程安全的API類,用來對超過16位有效位的數(shù)進(jìn)行精確的運(yùn)算,這篇文章主要介紹了java中BigDecimal的介紹及使用,BigDecimal格式化,BigDecimal常見問題,需要的朋友可以參考下2023-08-08

