public void equals(Object o){
if(this == o) retun true;
if(!(o instanceof Student)) return false;
Student stu = (Studend)o;
if(id!=stu.id) return false;
if(age!=stu.age) return false;
return name!=null ? name.equals(stu.name) : stu.name ==null;
}
public int hashCode(){
int result = id;
reuslt = 31*id +(name!=null?name.hashCode():0);
reuslt = 31*age;
return reuslt;
}
package com.test.arithmetic.listequals;
/**
* 这里id,name,age相同则Student相同,
* 若有其他相同
* Created by Administrator on 2016/3/29.
*/
public class Student {
int id;
String name;
int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
if (id != student.id) return false;
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + age;
return result;
}
}
package com.test.arithmetic.listequals;
import org.junit.Assert;
import java.util.*;
/**
* 取出list中重复的Student对象
* Created by Administrator on 2016/3/29.
*/
public class ObtainListEquals {
public static void main(String[] args){
//原始数据
List<Student> list = new ArrayList<>();
//重复数据
List<Student> list2 = new ArrayList<>();
//填充
for (int i = 0; i < 10 ; i++) {
list.add(new Student(i,"_"+i,18+i));
Random random = new Random();
if (random.nextBoolean()){
list.add(new Student(i,"_"+i,18+i));
}
}
//使用hashset去重复,set为重复的集合,可以通过new ArrayList(set)转换成list
HashSet<Student> set = new HashSet<>();
for (Student student : list) {
boolean add = set.add(student);
if (!add){
list2.add(student);
}
}
//比较
Assert.assertEquals(list.size(),list2.size()+set.size());
}
}
package com.test.arithmetic.listequals;
/**
* 这里id,name,age相同则Student相同,
* 若有其他相同
* Created by Administrator on 2016/3/29.
*/
public class Student implements Comparable<Student>{
int id;
String name;
int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
if (id != student.id) return false;
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + age;
return result;
}
@Override
public int compareTo(Student o) {
return this.id-o.id;
}
}
package com.test.arithmetic.list.sort;
import com.test.arithmetic.list.Student;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* 对list中对象排序
* Created by Administrator on 2016/3/29.
*/
public class SortList {
List<Student> list;
@Before
public void setUp(){
list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
int v = (int)(Math.random() * 100);
list.add(new Student(v,"_"+v,18+v));
}
System.out.println("原list:"+list);
}
//方法一,对象实现Comparable接口
@Test
public void byImplements(){
Collections.sort(list);
System.out.println("排序后:"+list);
}
}
package com.test.arithmetic.list;
/**
* 这里id,name,age相同则Student相同,
* 若有其他相同
* Created by Administrator on 2016/3/29.
*/
public class Student{
int id;
String name;
int age;
public Student(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public Student(int id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
if (id != student.id) return false;
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + age;
return result;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
package com.test.arithmetic.list.sort;
import com.test.arithmetic.list.Student;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* 对list中对象排序
* Created by Administrator on 2016/3/29.
*/
public class SortList {
List<Student> list;
@Before
public void setUp(){
list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
int v = (int)(Math.random() * 100);
list.add(new Student(v,"_"+v,18+v));
}
System.out.println("原list:"+list);
}
//方法一,对象实现Comparable接口
@Test
public void byImplements(){
// Collections.sort(list);
System.out.println("排序后:"+list);
}
/*方法二,添加比较器*/
@Test
public void byOverideCompare(){
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getId()-o2.getId();
}
});
System.out.println(list);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有