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--;
} }
} } }

判断ArryaList有没有重复对象的方法的更多相关文章

  1. List集合去除重复对象及equals()、hashCode()方法的作用

    原文:https://blog.csdn.net/freelander_j/article/details/52211010 在java中,要将一个集合中重复的对象除去,如果这个集合中的数据类型是基本 ...

  2. 使用ObjectInputStream的readObject()方法如何判断读取到多个对象的结尾

    摘自http://blog.csdn.net/fjdingsd/article/details/46765803 使用ObjectInputStream的readObject()方法如何判断读取到多个 ...

  3. 为Array对象添加一个去除重复项的方法

    输入例子 [false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN].uniq() 输出例子 [false, true, unde ...

  4. js数组中去除重复对象及去除空对象的方法

    (function(){//去除数组中重复对象 var unique = {}; arr.forEach(function(a){ unique[ JSON.stringify(a) ] = 1 }) ...

  5. javaScript定义对象的方法

    转自souhu新闻http://news.sohu.com/20110215/n279335637.shtml? javascript定义对象的几种简单方法 1.构造函数方式,全部属性及对象的方法都放 ...

  6. Python判断列表是否已排序的各种方法及其性能分析

    目录 Python判断列表是否已排序的各种方法及其性能分析 声明 一. 问题提出 二. 代码实现 2.1 guess 2.2 sorted 2.3 for-loop 2.4 all 2.5 numpy ...

  7. [UE4]复制引起的重复对象

    一.在角色的BeginPlay事件中,在角色正前方1米到2米处生成一立方体. 二.开启2个玩家,第一个创建是服务器端,第二个窗口是客户端.可以看到:服务器端窗口创建了2个灰色的立方体,客户端却创建了4 ...

  8. C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志

    C#实现多级子目录Zip压缩解压实例 参考 https://blog.csdn.net/lki_suidongdong/article/details/20942977 重点: 实现多级子目录的压缩, ...

  9. JavaScript (内置对象及方法)

    JavaScript中的对象分为3种:内置对象.浏览器对象.自定义对象 JavaScript 提供多个内置对象:Math/Array/Number/String/Boolean... 对象只是带有属性 ...

随机推荐

  1. Git错误日志--! [rejected]

    当出现下面错误时 ,是因为和仓库两端版本不一致时,常见于刚创建仓库时,中只有readme文件的情况.执行下面的运行完毕之后.再push到仓库即可 ! [rejected] master -> m ...

  2. 预处理命令使用详解----#if、#endif、#undef、#ifdef、#else、#elif

    预处理命令 在接触#if.#undef这类预处理指令前,大部分都都接触过#define.#include等预处理命令,通俗来讲预处理命令的作用就是在编译和链接之前,对源文件进行一些文本方面的操作,比如 ...

  3. jQuery基础(1)

    一.jQuery的介绍 1.为什么要使用jQuery? 在用js写代码时,会遇到一些问题,如下: 1)window.onload 事件有事件覆盖的问题,因此只能写一个事件: 2)代码容错性差: 3)浏 ...

  4. shapefile的使用和地理信息的获得

    Shapefile文件是美国ESRI公司发布的文件格式,因其ArcGIS软件的推广而得到了普遍的使用,是现在GIS领域使用最为广泛的矢量数据格式.官方称Shapefile是一种用于存储地理要素的几何位 ...

  5. AWD比赛常规套路

    作者:Leafer   本文属于Arctic shell原创内容计划文章,转载请注明原文地址! 比赛环境:纯净win10,最新版kali,securecrt或者WinSCP 在进入服务器后应进行的常规 ...

  6. python -猜字小游戏

    代码运行效果如下: 注意: 1.必须要在python3环境想使用 2.QQ:3084276329(一起交流学习) 3.还请大家评论 Guess the word game代码如下: #! /usr/b ...

  7. POJ 2656

    #include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("a ...

  8. Storm的acker确认机制

    Storm的acker消息确认机制... ack/fail消息确认机制(确保一个tuple被完全处理) 在spout中发射tuple的时候需要同时发送messageid,这样才相当于开启了消息确认机制 ...

  9. Git学习系列之Windows上安装Git之后的一些配置(图文详解)

    不多说,直接上干货! 前面博客 Git学习系列之Windows上安装Git详细步骤(图文详解) 第一次使用Git时,需要对Git进行一些配置,以方便使用Git. 不过,这种配置工作只需要进行一次便可, ...

  10. 执行bin/hdfs haadmin -transitionToActive nn1时出现,Automatic failover is enabled for NameNode at bigdata-pro02.kfk.com/192.168.80.152:8020 Refusing to manually manage HA state的解决办法(图文详解)

    不多说,直接上干货! 首先, 那么,你也许,第一感觉,是想到的是 全网最详细的Hadoop HA集群启动后,两个namenode都是standby的解决办法(图文详解) 这里,nn1,不多赘述了.很简 ...