?一、導言
???? Java中的對象去重操作?跟著小編一起學習吧!??
在處理對象集合時,有時候我們需要根據(jù)對象的某個屬性進行去重操作。Java給我們提供了多種方法來實現(xiàn)這個功能。今天,小編就來給大家介紹一下如何使用Java根據(jù)對象的某個屬性進行去重操作。??
方案一:使用自定義equals()和hashCode()方法
I.原理講解
- 提供一個自定義的類,包含需要去重的屬性。
- 重寫equals()方法,比較對象的name屬性是否相等。
- 重寫hashCode()方法,根據(jù)屬性生成哈希碼。
- 使用HashSet或LinkedHashSet進行去重操作。
II. 代碼示例
// 重新equals和hashCode方法
public class Person {
public String name;
public Integer age;
public Sex sex;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
······
}
public class ObjectDistinctTest {
public Person[] peoples;
public List<Person> personList;
// 構建數(shù)據(jù)源
@Before
public void before() {
Person person = new Chinese("大白", 21, Sex.man);
Person person1 = new Chinese("小蘭", 25, Sex.woman);
Person person2 = new Chinese("小紅", 23, Sex.woman);
Person person3 = new Chinese("小趙", 20, Sex.man);
Person person4 = new Chinese("小青", 18, Sex.woman);
Person person5 = new Chinese("小趙", 17, Sex.man);
Person person6 = new Chinese("小麻", 50, Sex.man);
this.peoples =new Person[]{ person, person1, person2, person3, person4, person5, person6};
this.personList = Arrays.asList(peoples);
}
/**
* 使用自定義equals()和hashCode()方法
*/
@Test
public void testCase01() {
// 需求:根據(jù)Person對象的name屬性進行去重,name屬性相同,則視為重復元素
// 存入順序和取出順序不一致
Set<Person> distinctPersonSet = new HashSet<>();
for (Person person : personList) {
distinctPersonSet.add(person); //
}
List<Person> distinctPersonList = new ArrayList<>(distinctPersonSet);
distinctPersonList.forEach(System.out::println); // 打印去重后的數(shù)據(jù)
}
/**
* 使用自定義equals()和hashCode()方法
*/
@Test
public void testCase02() {
// 或使用 LinkedHashSet 保持插入順序
Set<Person> distinctPersonSet = new LinkedHashSet<>();
for (Person person : personList) {
distinctPersonSet.add(person);
}
List<Person> distinctPersonList = new ArrayList<>(distinctPersonSet);
distinctPersonList.forEach(System.out::println); // 打印去重后的數(shù)據(jù)
}
}
方案二:使用循環(huán)和集合
I.原理講解
- 創(chuàng)建了一個新的集合來存儲去重后的對象
- 遍歷集合列表
- 判斷對象屬性是否重復
- 去除重復對象
II. 代碼示例
public class ObjectDistinctTest {
public Person[] peoples;
public List<Person> personList;
// 構建數(shù)據(jù)源
@Before
public void before() {
Person person = new Chinese("大白", 21, Sex.man);
Person person1 = new Chinese("小蘭", 25, Sex.woman);
Person person2 = new Chinese("小紅", 23, Sex.woman);
Person person3 = new Chinese("小趙", 20, Sex.man);
Person person4 = new Chinese("小青", 18, Sex.woman);
Person person5 = new Chinese("小趙", 17, Sex.man);
Person person6 = new Chinese("小麻", 50, Sex.man);
this.peoples =new Person[]{ person, person1, person2, person3, person4, person5, person6};
this.personList = Arrays.asList(peoples);
}
@Test
public void testCase() {
// 此方式無需實現(xiàn)equals方法
List<Person> newList = new ArrayList<>();
for (Person person: this.personList
) {
if (newList.stream().noneMatch(x -> x.getName().equals(person.getName()))) { // 判斷新集合中是否包含此對象,不包含才能加入到新集合中
newList.add(person);
}
}
List<Person> distinctList = newList.stream().sorted(Comparator.comparing(Person::getAge)).collect(Collectors.toList());
distinctList.forEach(System.out::println);
}
}
方案三:使用自定義equals()和Java 8中的distinct方法
I.原理講解
- 提供一個自定義的類,包含需要去重的屬性。
- 重寫equals()方法,比較對象的name屬性是否相等。
- 重寫hashCode()方法,根據(jù)屬性生成哈希碼。
- 使用Java 8Stream API中的distinct方法進行去重操作
II. 代碼示例
// 重新equals和hashCode方法
public class Person {
public String name;
public Integer age;
public Sex sex;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
······
}
public class ObjectDistinctTest {
public Person[] peoples;
public List<Person> personList;
// 構建數(shù)據(jù)源
@Before
public void before() {
Person person = new Chinese("大白", 21, Sex.man);
Person person1 = new Chinese("小蘭", 25, Sex.woman);
Person person2 = new Chinese("小紅", 23, Sex.woman);
Person person3 = new Chinese("小趙", 20, Sex.man);
Person person4 = new Chinese("小青", 18, Sex.woman);
Person person5 = new Chinese("小趙", 17, Sex.man);
Person person6 = new Chinese("小麻", 50, Sex.man);
this.peoples =new Person[]{ person, person1, person2, person3, person4, person5, person6};
this.personList = Arrays.asList(peoples);
}
/**
* 使用自定義equals()和Stream API中的distinct方法實現(xiàn)元素的去重
*/
@Test
public void testCase01() {
// 需求:根據(jù)Person對象的name屬性進行去重,name屬性相同,則視為重復元素
this.personList.stream() // 獲取流對象
.distinct() // 去除重復元素
.collect(Collectors.toList()) // 收集為List集合
.forEach(System.out::println); // 打印去重后的元素
}
}
方案四:將List以name屬性為key轉換為Map
I.原理講解
- 使用Stream API中的終結方法collect
- 將List轉為Map,key為Person類中的name屬性,value為Person對象
- 獲取使用values方法,獲取Map中的value,即為去重后的List
II. 代碼示例
public class ObjectDistinctTest {
public Person[] peoples;
public List<Person> personList;
// 構建數(shù)據(jù)源
@Before
public void before() {
Person person = new Chinese("大白", 21, Sex.man);
Person person1 = new Chinese("小蘭", 25, Sex.woman);
Person person2 = new Chinese("小紅", 23, Sex.woman);
Person person3 = new Chinese("小趙", 20, Sex.man);
Person person4 = new Chinese("小青", 18, Sex.woman);
Person person5 = new Chinese("小趙", 17, Sex.man);
Person person6 = new Chinese("小麻", 50, Sex.man);
this.peoples =new Person[]{ person, person1, person2, person3, person4, person5, person6};
this.personList = Arrays.asList(peoples);
}
@Test
public void testCase03() {
this.personList.stream() // 獲取流對象
.collect(Collectors.toMap(Person::getName, y -> y,(s, a) -> s))// 將List根據(jù)Person類中的name屬性收集為Map,key為name,value為Person對象,當后面對象name屬性跟前者相同時,使用前者。
.values() // 獲取Map中的key
.stream()
.collect(Collectors.toList())
.forEach(System.out::println);
}
}
五、總結
想象一下,現(xiàn)在你可以輕松地根據(jù)對象的某一屬性去除重復對象了!??
不僅如此,這個技巧還可以應用在各種不同的場景中,讓你的代碼更加簡潔高效!??文章來源:http://www.zghlxwxcb.cn/news/detail-757383.html
不管是初學者還是資深開發(fā)者,都會對這個技巧愛不釋手!快來試試吧!??文章來源地址http://www.zghlxwxcb.cn/news/detail-757383.html
到了這里,關于快速去重:使用Java根據(jù)對象某一屬性去除重復對象的實現(xiàn)指南的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!