使用Java Stream实现某个字段的去重排序
在Java编程中,经常需要对数据进行去重和排序的操作,特别是在处理大量数据或者从数据库中查询出来的数据时。Java 8引入的Stream API为我们提供了便捷的方式来进行数据处理,包括去重和排序。本文将介绍如何利用Java Stream来实现对某个字段的数据去重和排序操作,并提供示例代码。
Stream去重排序操作
我们将通过以下步骤来实现对某个字段的数据去重和排序:
1. 数据源准备:准备数据源,例如一个包含对象的List集合。
2. 利用Stream进行去重:使用Stream的distinct()方法对数据进行去重。
3. 利用Stream进行排序:使用Stream的sorted()方法对数据进行排序,根据指定字段进行排序。
示例代码
假设我们有一个Person类,包含id和name两个字段,我们想要根据id字段对Person对象进行去重和排序。
```java
import java.util.List;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List
persons = prepareData();
// 使用Stream进行去重
List
distinctPersons = persons.stream()
.distinct()
.collect(Collectors.toList());
// 使用Stream进行排序
List
sortedPersons = persons.stream()
.sorted((p1, p2) -> Integer.compare(p1.getId(), p2.getId()))
.collect(Collectors.toList());
// 输出去重后的结果
System.out.println("去重后的结果:");
distinctPersons.forEach(System.out::println);
// 输出排序后的结果
System.out.println("\n排序后的结果:");
sortedPersons.forEach(System.out::println);
}
private static List
prepareData() {
List
persons = List.of(
new Person(1, "Alice"),
new Person(2, "Bob"),
new Person(3, "Alice"),
new Person(1, "David"),
new Person(4, "Alice")
);
return persons;
}
}
class Person {
private int id;
private String name;
public Person(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
// 重写equals和hashCode方法,用于去重
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return id == person.id;
}
@Override
public int hashCode() {
return Integer.hashCode(id);
}
}
```
结论
通过本文的介绍和示例代码,我们学习了如何利用Java Stream来实现对某个字段的数据去重和排序操作。Stream API提供了丰富的方法来方便地进行数据处理,使得代码更加简洁和易读。这种方法在实际开发中可以帮助我们快速处理数据,并得到符合要求的结果。 https://www.qkeke.cn/
https://www.qkeke.cn/VIP/
https://www.qkeke.cn/VIP6/
https://www.qkeke.cn/VIP66/
https://www.qkeke.cn/VIP666/
https://www.qkeke.cn/VIP8/
https://www.qkeke.cn/VIP88/
https://www.qkeke.cn/VIP888/
https://www.qkeke.cn/VIP16/
https://www.qkeke.cn/VIP86/
https://www.qkeke.cn/VIP86/62714
https://www.qkeke.cn/VIP86/62713
https://www.qkeke.cn/VIP86/62712
https://www.qkeke.cn/VIP86/62711
https://www.qkeke.cn/VIP86/62710
https://www.qkeke.cn/VIP86/62709
https://www.qkeke.cn/VIP86/62708
https://www.qkeke.cn/VIP86/62707
https://www.qkeke.cn/VIP86/62706
https://www.qkeke.cn/VIP86/62705
https://www.qkeke.cn/VIP86/62704
https://www.qkeke.cn/VIP86/62703
https://www.qkeke.cn/VIP86/62702
https://www.qkeke.cn/VIP86/62701
https://www.qkeke.cn/VIP86/62700
https://www.qkeke.cn/VIP86/62699
https://www.qkeke.cn/VIP86/62698
https://www.qkeke.cn/VIP86/62697
https://www.qkeke.cn/VIP86/62696
https://www.qkeke.cn/VIP86/62695
https://www.qkeke.cn/VIP86/62694
https://www.qkeke.cn/VIP86/62693
https://www.qkeke.cn/VIP86/62692
https://www.qkeke.cn/VIP86/62691
https://www.qkeke.cn/VIP86/62690
https://www.qkeke.cn/VIP86/62689
https://www.qkeke.cn/VIP86/62688
https://www.qkeke.cn/VIP86/62687
https://www.qkeke.cn/VIP86/62686
https://www.qkeke.cn/VIP86/62685
https://www.qkeke.cn/VIP86/62684
https://www.qkeke.cn/VIP86/62683
https://www.qkeke.cn/VIP86/62682
https://www.qkeke.cn/VIP86/62681
https://www.qkeke.cn/VIP86/62680
https://www.qkeke.cn/VIP86/62679
https://www.qkeke.cn/VIP86/62678
https://www.qkeke.cn/VIP86/62677
https://www.qkeke.cn/VIP86/62676
https://www.qkeke.cn/VIP86/62675
https://www.qkeke.cn/VIP86/62674
https://www.qkeke.cn/VIP86/62673
https://www.qkeke.cn/VIP86/62672
https://www.qkeke.cn/VIP86/62671
https://www.qkeke.cn/VIP86/62670
https://www.qkeke.cn/VIP86/62669
https://www.qkeke.cn/VIP86/62668
https://www.qkeke.cn/VIP86/62667
https://www.qkeke.cn/VIP86/62666
https://www.qkeke.cn/VIP86/62665
https://www.qkeke.cn/VIP86/62664
https://www.qkeke.cn/VIP86/62663
https://www.qkeke.cn/VIP86/62662
https://www.qkeke.cn/VIP86/62661
https://www.qkeke.cn/VIP86/62660
https://www.qkeke.cn/VIP86/62659
https://www.qkeke.cn/VIP86/62658
https://www.qkeke.cn/VIP86/62657
https://www.qkeke.cn/VIP86/62656
https://www.qkeke.cn/VIP86/62655
https://www.qkeke.cn/VIP86/62654
https://www.qkeke.cn/VIP86/62653
https://www.qkeke.cn/VIP86/62652
https://www.qkeke.cn/VIP86/62651
https://www.qkeke.cn/VIP86/62650
https://www.qkeke.cn/VIP86/62649
https://www.qkeke.cn/VIP86/62648
https://www.qkeke.cn/VIP86/62647
https://www.qkeke.cn/VIP86/62646
https://www.qkeke.cn/VIP86/62645
https://www.qkeke.cn/VIP86/62644
https://www.qkeke.cn/VIP86/62643
https://www.qkeke.cn/VIP86/62642
https://www.qkeke.cn/VIP86/62641
https://www.qkeke.cn/VIP86/62640
https://www.qkeke.cn/VIP86/62639
https://www.qkeke.cn/VIP86/62638
https://www.qkeke.cn/VIP86/62637
https://www.qkeke.cn/VIP86/62636
https://www.qkeke.cn/VIP86/62635
https://www.qkeke.cn/VIP86/62634
https://www.qkeke.cn/VIP86/62633
https://www.qkeke.cn/VIP86/62632
https://www.qkeke.cn/VIP86/62631
https://www.qkeke.cn/VIP86/62630
https://www.qkeke.cn/VIP86/62629
https://www.qkeke.cn/VIP86/62628
https://www.qkeke.cn/VIP86/62627
https://www.qkeke.cn/VIP86/62626
https://www.qkeke.cn/VIP86/62625
https://www.qkeke.cn/VIP86/62624
https://www.qkeke.cn/VIP86/62623
https://www.qkeke.cn/VIP86/62622
https://www.qkeke.cn/VIP86/62621
https://www.qkeke.cn/VIP86/62620
https://www.qkeke.cn/VIP86/62619
https://www.qkeke.cn/VIP86/62618
https://www.qkeke.cn/VIP86/62617
https://www.qkeke.cn/VIP86/62616
https://www.qkeke.cn/VIP86/62615
https://www.qkeke.cn/VIP86/62614
https://www.qkeke.cn/VIP86/62613
https://www.qkeke.cn/VIP86/62612
https://www.qkeke.cn/VIP86/62611
https://www.qkeke.cn/VIP86/62610
https://www.qkeke.cn/VIP86/62609
https://www.qkeke.cn/VIP86/62608
https://www.qkeke.cn/VIP86/62607
https://www.qkeke.cn/VIP86/62606
https://www.qkeke.cn/VIP86/62605
https://www.qkeke.cn/VIP86/62604
https://www.qkeke.cn/VIP86/62603
https://www.qkeke.cn/VIP86/62602
https://www.qkeke.cn/VIP86/62601
https://www.qkeke.cn/VIP86/62600
https://www.qkeke.cn/VIP86/62599
https://www.qkeke.cn/VIP86/62598
https://www.qkeke.cn/VIP86/62597
https://www.qkeke.cn/VIP86/62596
https://www.qkeke.cn/VIP86/62595
https://www.qkeke.cn/VIP86/62594
https://www.qkeke.cn/VIP86/62593
https://www.qkeke.cn/VIP86/62592
https://www.qkeke.cn/VIP86/62591
https://www.qkeke.cn/VIP86/62590
https://www.qkeke.cn/VIP86/62589
https://www.qkeke.cn/VIP86/62588
https://www.qkeke.cn/VIP86/62587
https://www.qkeke.cn/VIP86/62586
https://www.qkeke.cn/VIP86/62585
https://www.qkeke.cn/VIP86/62584
https://www.qkeke.cn/VIP86/62583
https://www.qkeke.cn/VIP86/62582
https://www.qkeke.cn/VIP86/62581
https://www.qkeke.cn/VIP86/62580
https://www.qkeke.cn/VIP86/62579
https://www.qkeke.cn/VIP86/62578
https://www.qkeke.cn/VIP86/62577
https://www.qkeke.cn/VIP86/62576
https://www.qkeke.cn/VIP86/62575
https://www.qkeke.cn/VIP86/62574
https://www.qkeke.cn/VIP86/62573
https://www.qkeke.cn/VIP86/62572
https://www.qkeke.cn/VIP86/62571
https://www.qkeke.cn/VIP86/62570
https://www.qkeke.cn/VIP86/62569
https://www.qkeke.cn/VIP86/62568
https://www.qkeke.cn/VIP86/62567
https://www.qkeke.cn/VIP86/62566
https://www.qkeke.cn/VIP86/62565
https://www.qkeke.cn/VIP86/62564
https://www.qkeke.cn/VIP86/62563
https://www.qkeke.cn/VIP86/62562
https://www.qkeke.cn/VIP86/62561
https://www.qkeke.cn/VIP86/62560
https://www.qkeke.cn/VIP86/62559
https://www.qkeke.cn/VIP86/62558
https://www.qkeke.cn/VIP86/62557
https://www.qkeke.cn/VIP86/62556
https://www.qkeke.cn/VIP86/62555
https://www.qkeke.cn/VIP86/62554
https://www.qkeke.cn/VIP86/62553
https://www.qkeke.cn/VIP86/62552
https://www.qkeke.cn/VIP86/62551
https://www.qkeke.cn/VIP86/62550
https://www.qkeke.cn/VIP86/62549
https://www.qkeke.cn/VIP86/62548
https://www.qkeke.cn/VIP86/62547
https://www.qkeke.cn/VIP86/62546
https://www.qkeke.cn/VIP86/62545
https://www.qkeke.cn/VIP86/62544
https://www.qkeke.cn/VIP86/62543
https://www.qkeke.cn/VIP86/62542
https://www.qkeke.cn/VIP86/62541
https://www.qkeke.cn/VIP86/62540
https://www.qkeke.cn/VIP86/62539
https://www.qkeke.cn/VIP86/62538
https://www.qkeke.cn/VIP86/62537
https://www.qkeke.cn/VIP86/62536
https://www.qkeke.cn/VIP86/62535
https://www.qkeke.cn/VIP86/62534
https://www.qkeke.cn/VIP86/62533
https://www.qkeke.cn/VIP86/62532
https://www.qkeke.cn/VIP86/62531
https://www.qkeke.cn/VIP86/62530
https://www.qkeke.cn/VIP86/62529
https://www.qkeke.cn/VIP86/62528
https://www.qkeke.cn/VIP86/62527
https://www.qkeke.cn/VIP86/62526
https://www.qkeke.cn/VIP86/62525
https://www.qkeke.cn/VIP86/62524
https://www.qkeke.cn/VIP86/62523
https://www.qkeke.cn/VIP86/62522
https://www.qkeke.cn/VIP86/62521
https://www.qkeke.cn/VIP86/62520
https://www.qkeke.cn/VIP86/62519
https://www.qkeke.cn/VIP86/62518
https://www.qkeke.cn/VIP86/62517
https://www.qkeke.cn/VIP86/62516
https://www.qkeke.cn/VIP86/62515
https://www.qkeke.cn/VIP86/62514
https://www.qkeke.cn/VIP86/62513
https://www.qkeke.cn/VIP86/62512
https://www.qkeke.cn/VIP86/62511
https://www.qkeke.cn/VIP86/62510
https://www.qkeke.cn/VIP86/62509
https://www.qkeke.cn/VIP86/62508
https://www.qkeke.cn/VIP86/62507
https://www.qkeke.cn/VIP86/62506
https://www.qkeke.cn/VIP86/62505
https://www.qkeke.cn/VIP86/62504
https://www.qkeke.cn/VIP86/62503
https://www.qkeke.cn/VIP86/62502
https://www.qkeke.cn/VIP86/62501
https://www.qkeke.cn/VIP86/62500
https://www.qkeke.cn/VIP86/62499
https://www.qkeke.cn/VIP86/62498
https://www.qkeke.cn/VIP86/62497
https://www.qkeke.cn/VIP86/62496
https://www.qkeke.cn/VIP86/62495
https://www.qkeke.cn/VIP86/62494
https://www.qkeke.cn/VIP86/62493
https://www.qkeke.cn/VIP86/62492
https://www.qkeke.cn/VIP86/62491
https://www.qkeke.cn/VIP86/62490
https://www.qkeke.cn/VIP86/62489
https://www.qkeke.cn/VIP86/62488
https://www.qkeke.cn/VIP86/62487
https://www.qkeke.cn/VIP86/62486
https://www.qkeke.cn/VIP86/62485
https://www.qkeke.cn/VIP86/62484
https://www.qkeke.cn/VIP86/62483
https://www.qkeke.cn/VIP86/62482
https://www.qkeke.cn/VIP86/62481
https://www.qkeke.cn/VIP86/62480
https://www.qkeke.cn/VIP86/62479
https://www.qkeke.cn/VIP86/62478
https://www.qkeke.cn/VIP86/62477
页:
[1]