zoukankan      html  css  js  c++  java
  • 判断ArryaList有没有重复对象的方法

      ArrayList类是List类下一种常用的子类,如果要判断容器里面的对象是否有相等,有两种方法。

      下面是自定义的一个Student类,假设容器里重复是按照对象的两个属性都相等。

    /**
     * @author Wangchengan
     *
     */
    public class Student {
        private String name;
        private int 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;
        }
        public Student(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
        @Override
        public String toString() {
            return "Student [name=" + name + ", age=" + age + "]";
        }
        public boolean equals(Object obj) {
            //增加效率,如果是相同对象就不用比较了
            if(this==obj)
            return true;
            //如果要比较的不是跟这个对象一样类型的就抛出一个运行时异常
            if (!(obj instanceof Student)) {
                throw new ClassCastException("类型错误");
            }
            //向下转型才能使用子类的方法
            Student student=(Student)obj;
            return this.name.equals(student.name)&&this.age==student.age;
        }
        
    }

      下面是判断的两种方法,先使用了字符串作为演示。

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    public class ArrayListTest {
    
        public static void main(String[] args) {
            /*
             * ArrayList两种判断重复方法
            1.遍历,使用两个循环
            2.创建一个新的容器,旧容器中使用迭代器,没有则加入新容器
            (其实contains方法里面也是使用了equls方法,只不过字符串的equals被复写了)
            */
            List list=new ArrayList();
            list.add("abc1");
            list.add("abc4");
            list.add("abc2");
            list.add("abc1");
            list.add("abc4");
            list.add("abc4");
            list.add("abc2");
            list.add("abc1");
            list.add("abc4");
            list.add("abc2");
            
            System.out.println(list);
            singleElement2(list);
            System.out.println(list);
            System.out.println("------------");
            /*
             * ArrayList判断对象的方法
             * 其实Arraylist里的contains方法是继承自Collection接口里的contains方法,使用的是equals方法
             * 所以要判断对象里的属性的话,还是要复写对象类里的euals方法,同时它会自动调用
             * 
             */
            List studentList=new ArrayList();
            Student stu1=new Student("lisi", 20);
            Student stu2=new Student("wangwu", 22);
            Student stu3=new Student("liqing", 23);
            Student stu4=new Student("liuhu", 27);
            Student stu5=new Student("lisi", 20);
            Student stu6=new Student("liqing", 23);
            Student stu7=new Student("lisi", 20);
            studentList.add(stu1);
            studentList.add(stu2);
            studentList.add(stu3);
            studentList.add(stu4);
            studentList.add(stu5);
            System.out.println(studentList);
            singleObject(studentList);
            System.out.println(studentList);
            
            
    
        }
        //判断Arraylist里面有没有重复对象的方法(给对象使用)
        private static void singleObject(List studentList) {
            singleElement2(studentList);
            
        }
        
        //判断Arraylist里面有没有重复对象的方法2
        private static void singleElement2(List list) {
            List newList=new ArrayList();
            for (Iterator iterator = list.iterator(); iterator.hasNext();) {
                Object obj = (Object) iterator.next();
                
                if (!newList.contains(obj)) {
                    newList.add(obj);
                }
            }
            list.clear();
            list.addAll(newList);
            
        }
        //判断Arraylist里面有没有重复对象的方法1
        private static void singleElement(List list) {
            for (int i = 0; i < list.size()-1; i++) {
                Object obj_x=list.get(i);
                for (int j = i+1; j< list.size(); j++) {
                    if (obj_x.equals(list.get(j))) {
                        list.remove(j);
                        //每次都要减去一个,因为remove后数据元素会改变
                        j--;
                    }
                    
                }
            }
            
        }
    
    }
  • 相关阅读:
    Vagrant安装virtualbox
    SQLSERVER排查CPU占用高的情况
    删除重复记录,只留一条
    ASCII码对应表chr(9)、chr(10)、chr(13)、chr(32)、chr(34)、chr(39)、……
    手机和PC端的录屏软件
    2017年初面试总结
    Python面向对象
    Python字体颜色
    Python第二模块总结
    Fiddler使用教程(转)
  • 原文地址:https://www.cnblogs.com/mugglean/p/9229300.html
Copyright © 2011-2022 走看看