import java.util.ArrayList;
import java.util.Iterator; public class StringSampleDemo {
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add(new Student("zhangsan1", 20, "男"));
al.add(new Student("zhangsan1", 20, "男"));
al.add(new Student("lilin1", 21, "女"));
al.add(new Student("lilin1", 21, "女"));
al.add(new Student("lisi", 25, "男")); al = getUniqueList(al); for (Object o : al) {
Student s = (Student) o;
System.out.println(s.getName() + "...." + s.getAge() + "...." + s.getSex());
} /** 去重后的集合数据
*
* zhangsan1....20....男
* lilin1....21....女
* lisi....25....男
*/ } /**
* 去除重复对象
*
* @param al
* @return
*/
public static ArrayList getUniqueList(ArrayList al) {
ArrayList tempAl = new ArrayList(); Iterator it = al.iterator();
while (it.hasNext()) {
Object obj = it.next();
if (!tempAl.contains(obj)) //不存在则添加
{
tempAl.add(obj);
}
}
return tempAl;
}
} class Student {
private String name;
private int age;
private String sex; public Student(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
} 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 String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} /**
* 重点是重写比较方法
*
* @param obj
* @return
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof Student) {
Student s = (Student) obj;
return this.name.equals(s.name) && this.age == s.age && this.sex.equals(s.sex);
} else {
return false;
}
}
}

  

最新文章

  1. remove name="ProxyModule“会导致重复执行
  2. 51NOD算法马拉松11 B君的竞技场
  3. [转]div与span区别及用法
  4. 关于tomcat的远程调试
  5. SuperSocket源码解析之开篇
  6. canvas绘制百分比圆环进度条
  7. 【SAP HANA】新建账户和数据库(2)
  8. Mysql查询报错:Illegal mix of collations (gbk_chinese_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='
  9. MySQL高级知识(十六)——小表驱动大表
  10. vue2.x + vux采坑总结(一)
  11. 用嵌入式块RAM IP核配置一个双口RAM
  12. 8. String to Integer (整数的溢出)
  13. ELK简单部署
  14. innodb_file_per_table - 转换为InnoDB
  15. 网络获取json数据并解析
  16. C# •MouseDown •MouseDown •MouseUp 的先后顺序
  17. JDK 泛型
  18. HeadFirst jsp 08 无脚本JSP
  19. 关于Html5中的单选与多选
  20. Python3.6全栈开发实例[008]

热门文章

  1. 使用jQuery开发accordion手风琴插件
  2. ToLua Timer机制
  3. HTML5的常用的标签
  4. HTML5新增元素和移除的元素?
  5. SetCurrentCellAddressCore 函数的可重入调用
  6. mysql57重新安装后无法再次启动mysql57服务“本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动。”--解决方法
  7. Nginx配置文件示例
  8. kubernetes使用Traefik暴露web服务-转载51cto
  9. ISCC之Re1
  10. 2013.5.4 - KDD第十六天