package com.tuling.kafka.kafkaDemo.Command;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @USER: hx
* @DATE: 2022/4/27
**/
public class SetTest {
private String name;
private String age;
private String aa;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAa() {
return aa;
}
public void setAa(String aa) {
this.aa = aa;
}
public static void main(String[] args) {
SetTest setTest1 = new SetTest();
setTest1.setAa("aa");
setTest1.setAge("aaAge");
setTest1.setName("aaName");
SetTest setTest2 = new SetTest();
setTest2.setAa("aa");
setTest2.setAge("aaAge");
setTest2.setName("aaName");
SetTest setTest3 = new SetTest();
setTest3.setAa("aa");
setTest3.setAge("aaAge");
setTest3.setName("aaName");
SetTest setTest4 = null;
List<SetTest> setTestList = new ArrayList<SetTest>();
setTestList.add(setTest4);
setTestList.add(setTest1);
setTestList.add(setTest2);
setTestList.add(setTest4);
setTestList.add(setTest3);
setTestList.add(setTest4);
Set<SetTest> setTestSet = new HashSet<SetTest>();
setTestSet.addAll(setTestList);
// setTestSet.add(setTest1);
// setTestSet.add(setTest2);
// setTestSet.add(setTest3);
System.out.println("setTestSet.size: " + setTestSet.size());
System.out.println("setTestList.size: " + setTestList.size());
for (SetTest setTest : setTestList) {
if (setTest == null){
continue;
}
System.out.println("setTestList输出:" + setTest.getAa() + ":" + setTest.getAge() + ":" + setTest.getName());
}
System.out.println("-----------------------------------------------------------------");
System.out.println("-----------------------------------------------------------------");
System.out.println("-----------------------------------------------------------------");
for (SetTest setTest : setTestSet) {
if (setTest == null){
continue;
}
System.out.println("setTestSet输出:" + setTest.getAa() + ":" + setTest.getAge() + ":" + setTest.getName());
}
Set<String> set = new HashSet<String>();
set.add("1");
set.add("1");
set.add("1");
for (String setTest : set) {
System.out.println("测试set是否有重复数据:" + setTest);
}
}
/**
* 2,在比较equals的属性值,实现去重
* @param obj
* @return
*/
@Override
public boolean equals(Object obj) {
if (obj == null) {
return true;
}
SetTest setTest = (SetTest) obj;
if (String.valueOf(this.getName()).equals(String.valueOf(setTest.getName()))) {
return true;
}
return false;
}
/**
* 1.先重写hashcode方法,让其一样
* @return
*/
@Override
public int hashCode() {
return 1;
}
// public int compareTo(SetTest o) {
// if (String.valueOf(this.getName()).equals(String.valueOf(o.getName()))) {
// return 0;
// }
// else {
// return -1;
// }
// }
}
|