天天看点

第三次学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;
  }
  @Override
  public String toString() {
    return "Person [name=" + name + ", age=" + age + "]";
  }

  @Override
  public 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]