1、集合使用stream
可以使用Comparator接口来实现根据某个字段排序集合。下面是一个示例代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 创建一个包含对象的集合
List<Person> personList = new ArrayList<>();
personList.add(new Person("Alice", 25));
personList.add(new Person("Bob", 30));
personList.add(new Person("Charlie", 20));
// 使用Comparator接口实现根据年龄字段排序
Collections.sort(personList, new Comparator<Person>() {
@Override
public int compare(Person p1, Person p2) {
return p1.getAge() - p2.getAge();
}
});
// 打印排序后的结果
for (Person person : personList) {
System.out.println(person.getName() + " - " + person.getAge());
}
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
在上面的示例中,我们创建了一个Person类,包含姓名和年龄字段。然后创建了一个personList集合,并添加了几个Person对象。接下来,我们使用Collections.sort()方法对集合进行排序,传入一个实现了Comparator接口的匿名类,重写了compare()方法来指定根据年龄字段进行排序。最后,我们遍历排序后的集合,打印每个Person对象的姓名和年龄。
使用Java 8的Stream API,可以更简洁地实现集合根据某个字段排序。下面是使用Stream API实现的示例代码:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 创建一个包含对象的集合
List<Person> personList = new ArrayList<>();
personList.add(new Person("Alice", 25));
personList.add(new Person("Bob", 30));
personList.add(new Person("Charlie", 20));
// 使用Stream API实现根据年龄字段排序
List<Person> sortedList = personList.stream()
.sorted(Comparator.comparingInt(Person::getAge))
.toList();
// 打印排序后的结果
for (Person person : sortedList) {
System.out.println(person.getName() + " - " + person.getAge());
}
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
在上面的示例中,我们使用stream()方法将集合转换为Stream对象。然后使用sorted()方法传入一个Comparator对象,通过Comparator.comparingInt()方法指定根据年龄字段进行排序。最后,使用toList()方法将排序后的Stream转换为List对象。
如果你想要按照某个字段进行降序排序,可以使用Comparator的comparing方法,并结合Collections.sort或Stream.sorted方法,并使用Comparator.reverseOrder()方法来实现。下面是一个示例代码:
使用Collections.sort方法进行降序排序:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 创建一个包含对象的集合
List<Person> personList = new ArrayList<>();
personList.add(new Person("Alice", 25));
personList.add(new Person("Bob", 30));
personList.add(new Person("Charlie", 20));
// 使用Comparator.comparing方法实现根据年龄字段降序排序
Collections.sort(personList, Comparator.comparing(Person::getAge).reversed());
// 打印排序后的结果
for (Person person : personList) {
System.out.println(person.getName() + " - " + person.getAge());
}
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
1.2
使用Stream.sorted方法进行降序排序:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 创建一个包含对象的集合
List<Person> personList = new ArrayList<>();
personList.add(new Person("Alice", 25));
personList.add(new Person("Bob", 30));
personList.add(new Person("Charlie", 20));
// 使用Stream.sorted方法实现根据年龄字段降序排序
List<Person> sortedList = personList.stream()
.sorted(Comparator.comparing(Person::getAge).reversed())
.toList();
// 打印排序后的结果
for (Person person : sortedList) {
System.out.println(person.getName() + " - " + person.getAge());
}
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
2、数组使用stream
1、求和
使用Java 8的Stream流可以更简洁地计算引用类型数组中某一属性的和。以下是使用Stream流计算的示例代码:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// 创建一个Person类的数组
Person[] people = new Person[3];
people[0] = new Person("Alice", 25);
people[1] = new Person("Bob", 30);
people[2] = new Person("Charlie", 35);
// 使用Stream流计算年龄的总和
int totalAge = Arrays.stream(people)
.mapToInt(Person::getAge)
.sum();
System.out.println("总年龄:" + totalAge);
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
}
在上面的示例中,我们使用Arrays.stream()方法将Person类的数组转换为一个Stream流。然后,使用mapToInt()方法将每个Person对象映射为其年龄属性值的IntStream。最后,使用sum()方法计算IntStream中所有元素的和,得到年龄的总和。
2、添加过滤条件
如果你想要计算引用类型数组中年龄大于18的人的年龄总和,你可以在Stream流中添加一个过滤条件。以下是修改后的示例代码:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// 创建一个Person类的数组
Person[] people = new Person[5];
people[0] = new Person("Alice", 25);
people[1] = new Person("Bob", 30);
people[2] = new Person("Charlie", 35);
people[3] = new Person("David", 15);
people[4] = new Person("Eve", 20);
// 使用Stream流计算年龄大于18的人的年龄总和
int totalAge = Arrays.stream(people)
.filter(person -> person.getAge() > 18)
.mapToInt(Person::getAge)
.sum();
System.out.println("年龄大于18的人的年龄总和:" + totalAge);
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
}
在上面的示例中,我们添加了一个过滤条件.filter(person -> person.getAge() > 18),它会保留年龄大于18的人。然后,我们继续使用mapToInt()方法将每个符合条件的Person对象映射为其年龄属性值的IntStream,并使用sum()方法计算年龄总和。