当前位置 : 主页 > 编程语言 > c++ >

ArrayList去除集合中自定义对象元素的重复值

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt package day16.ArrayList;import java.util.ArrayList;import java.util.Iterator;/** * 描述: * ArrayList去除集合中自定义对象元素的重复值 * * @outhor 71948 * @create 2017-10-01 15:51 */public class Demo2_ArrayList
gistfile1.txt
package day16.ArrayList;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * 描述:
 * ArrayList去除集合中自定义对象元素的重复值
 *
 * @outhor 71948
 * @create 2017-10-01 15:51
 */
public class Demo2_ArrayList {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(new Person("zhan",21));
        list.add(new Person("zhan",21));
        list.add(new Person("san",22));
        list.add(new Person("san",22));

        ArrayList newList = getSingel(list);
        System.out.println(newList);
    }

    public static ArrayList getSingel(ArrayList list){
       ArrayList  newList = new ArrayList();
        Iterator it = list.iterator();

        while (it.hasNext()){
            Object obj = it.next();
            if (!newList.contains(obj)){
                newList.add(obj);
            }
        }
        return  newList;
    }
}
网友评论