第三次学JAVA再学不好就吃翔(part81)--去除ArrayList中重复元素
生活随笔
收集整理的這篇文章主要介紹了
第三次学JAVA再学不好就吃翔(part81)--去除ArrayList中重复元素
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
學習筆記,僅供參考
因為我突然懶了,所以這個Blog以代碼為主,解釋為輔
文章目錄
- 集合
- 去除ArrayList中重復的字符串元素
- 去除ArrayList中重復的自定義對象元素
- LinkedList的特有功能
集合
去除ArrayList中重復的字符串元素
package com.guiyang.object;import java.util.ArrayList; import java.util.Iterator;@SuppressWarnings({ "rawtypes", "unchecked" }) public class Demo1_ArrayList {public static void main(String[] args) {ArrayList alist = new ArrayList();alist.add("a");alist.add("a");alist.add("a");alist.add("b");alist.add("b");alist.add("c");ArrayList newlist = getSingle(alist);System.out.println(newlist);}public static ArrayList getSingle(ArrayList list) {ArrayList newlist = new ArrayList();Iterator iterator = list.iterator();while (iterator.hasNext()) {Object object = iterator.next();if (!newlist.contains(object)) {newlist.add(object);}}return newlist;}}
輸出:
[a, b, c]去除ArrayList中重復的自定義對象元素
注意,contains方法判斷集合中是否包含某個元素,底層依賴的是equals方法。
所以,在進行定義對象的比較時,我們需要改寫自定義類的equals方法.
自定義Person類:
package com.guiyang.bean;public class Person {private String name;private int age;public Person() {super();}public Person(String name, int age) {super();this.name = name;this.age = age;}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;}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + "]";}@Overridepublic boolean equals(Object obj) {Person p = (Person)obj;return this.name.equals(p.name) && this.age == p.age;}}Java:
package com.guiyang.restudy3; import java.util.ArrayList; import java.util.Iterator;import com.guiyang.bean.Person;@SuppressWarnings({"rawtypes", "unchecked"}) public class D2ArrayListRepeat {public static void main(String[] args) {ArrayList list = new ArrayList(); //創建集合對象list.add(new Person("小白", 23));list.add(new Person("小白", 23));list.add(new Person("大黃", 24));list.add(new Person("大黃", 24));list.add(new Person("王子", 24));ArrayList newList = getSingle(list); //調用方法去除重復System.out.println(newList);}public static ArrayList getSingle(ArrayList list) {ArrayList newList = new ArrayList<>(); //1,創建新集合Iterator it = list.iterator(); //2,根據傳入的集合(老集合)獲取迭代器while(it.hasNext()) { //3,遍歷老集合Object obj = it.next(); //記錄住每一個元素if(!newList.contains(obj)) {//如果新集合中不包含老集合中的元素newList.add(obj); //將該元素添加}}return newList;}}輸出:
[Person [name=小白, age=23], Person [name=大黃, age=24], Person [name=王子, age=24]]同樣,remove方法移除集合中的某個元素時,底層依賴的還是equals方法。
package com.guiyang.restudy3; import java.util.ArrayList; import java.util.Iterator;import com.guiyang.bean.Person;@SuppressWarnings({"rawtypes", "unchecked"}) public class D2ArrayListRepeat {public static void main(String[] args) {ArrayList list = new ArrayList(); //創建集合對象list.add(new Person("小白", 23));list.add(new Person("小白", 23));list.add(new Person("大黃", 24));list.add(new Person("大黃", 24));list.add(new Person("王子", 24));list.remove(new Person("王子", 24));System.out.println(list);}public static ArrayList getSingle(ArrayList list) {ArrayList newList = new ArrayList<>(); //1,創建新集合Iterator it = list.iterator(); //2,根據傳入的集合(老集合)獲取迭代器while(it.hasNext()) { //3,遍歷老集合Object obj = it.next(); //記錄住每一個元素if(!newList.contains(obj)) { //如果新集合中不包含老集合中的元素newList.add(obj); //將該元素添加}}return newList;} }輸出:
[Person [name=小白, age=23], Person [name=小白, age=23], Person [name=大黃, age=24], Person [name=大黃, age=24]]LinkedList的特有功能
package com.guiyang.restudy3;import java.util.LinkedList;@SuppressWarnings({ "rawtypes", "unchecked" }) public class D1Linkedlist {public static void main(String[] args) {LinkedList list = new LinkedList();list.addFirst("a");list.addFirst("b");list.addFirst("c");list.addFirst("d");list.addLast("e");list.addLast("f");System.out.println(list.getFirst());System.out.println(list.getLast());System.out.println(list.removeFirst());System.out.println(list.removeLast());System.out.println("---------");System.out.println(list.get(0));System.out.println(list);}}
輸出:
d f d f --------- c [c, b, a, e]總結
以上是生活随笔為你收集整理的第三次学JAVA再学不好就吃翔(part81)--去除ArrayList中重复元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 十里扬州灯火不休意思
- 下一篇: 水星 MW310R V5 无线路由器Wi