Java8list对象字段升序降序
原⽂:/java/jdk-8/java-8-stream-sorted-example
Stream sorted() using Natural Ordering, Comparator and Rever Ordering
Find the syntax of sorted() method.
1. sorted(): It sorts the elements of stream using natural ordering. The element class must implement Comparable interface.
2. sorted(Comparator<? super T> comparator): Here we create an instance of Comparator using lambda expression. We can sort the stream elements in ascending and descending order.
The following line of code will sort the list in natural ordering.
list.stream().sorted()
To rever the natural ordering Comparator provides reverOrder() method. We u it as follows.
list.stream().verOrder())
The following line of code is using Comparator to sort the list.
list.stream().paring(Student::getAge))驳船英文
To rever the ordering, Comparator provides reverd() method. We u this method as follows.
list.stream().paring(Student::getAge).reverd())
Stream sorted() with List
Here we are sorting a List of objects of Student class. First we will sort by natural ordering and then using Comparator. We will rever both ordering natural ordering as well as ordering provided by Comparator in our example.
SortList.java
tepage;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class SortList {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
list.add(new Student(1, "Mahesh", 12));
list.add(new Student(2, "Suresh", 15));
imfilllist.add(new Student(3, "Nilesh", 10));
System.out.println("---Natural Sorting by Name---");
List<Student> slist = list.stream().sorted().List());
slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));
System.out.println("---Natural Sorting by Name in rever order---");
slist = list.stream().verOrder()).List());
slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));
System.out.println("---Sorting using Comparator by Age---");
slist = list.stream().paring(Student::getAge)).List());
slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));
System.out.println("---Sorting using Comparator by Age with rever order---");
slist = list.stream().paring(Student::getAge).reverd()).List());
slist.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));
}
}
Student.java
tepage;
public class Student implements Comparable<Student> {
private int id;
private String name;
private 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 String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public int compareTo(Student ob) {
Name());
}
@Override
public boolean equals(final Object obj) {凯特 戴琳斯
if (obj == null) {
return fal;
}
final Student std = (Student) obj;
if (this == std) {
return true;
} el {
return (this.name.equals(std.name) && (this.age == std.age));
}
}
@Override
经济技术开发区英文
public int hashCode() {
int hashno = 7;
hashno = 13 * hashno + (name == null ? 0 : name.hashCode());
return hashno;
}
}
Output
-
--Natural Sorting by Name---
Id:1, Name: Mahesh, Age:12
Id:3, Name: Nilesh, Age:10
Id:2, Name: Suresh, Age:15
---Natural Sorting by Name in rever order---
Id:2, Name: Suresh, Age:15
Id:3, Name: Nilesh, Age:10
Id:1, Name: Mahesh, Age:12
---Sorting using Comparator by Age---
Id:3, Name: Nilesh, Age:10
Id:1, Name: Mahesh, Age:12
Id:2, Name: Suresh, Age:15
---Sorting using Comparator by Age with rever order---
Id:2, Name: Suresh, Age:15
Id:1, Name: Mahesh, Age:12
Id:3, Name: Nilesh, Age:10
Stream sorted() with Set
Here we are sorting the Set of objects of Student class. This class must override equals() and hashCode()methods to identify unique elements. For natural ordering Student class needs to implement Comparable interface. In our example we will sort our Set using natural ordering as well as ordering provided by Comparator.
SortSet.java
tepage;
import java.util.Comparator;
import java.util.HashSet;accommodating
import java.util.Set;
public class SortSet {
public static void main(String[] args) {
Set<Student> t = new HashSet<Student>();
t.add(new Student(1, "Mahesh", 12));
t.add(new Student(2, "Suresh", 15));
t.add(new Student(3, "Nilesh", 10));
System.out.println("---Natural Sorting by Name---");
t.stream().sorted().forEach(e -> System.out.println("Id:"
+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));
System.out.println("---Natural Sorting by Name in rever order---");
t.stream().verOrder()).forEach(e -> System.out.println("Id:"
+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));
System.out.println("---Sorting using Comparator by Age---");
t.stream().paring(Student::getAge))
.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));
System.out.println("---Sorting using Comparator by Age in rever order---");
t.stream().paring(Student::getAge).reverd())
.forEach(e -> System.out.println("Id:"+ e.getId()+", Name: "+e.getName()+", Age:"+e.getAge()));
着哩}
}
Output
---Natural Sorting by Name---
Id:1, Name: Mahesh, Age:12
Id:3, Name: Nilesh, Age:10
Id:2, Name: Suresh, Age:15
---Natural Sorting by Name in rever order---
Id:2, Name: Suresh, Age:15
Id:3, Name: Nilesh, Age:10
Id:1, Name: Mahesh, Age:12
---Sorting using Comparator by Age---
Id:3, Name: Nilesh, Age:10
Id:1, Name: Mahesh, Age:12
Id:2, Name: Suresh, Age:15
---Sorting using Comparator by Age in rever order---
Id:2, Name: Suresh, Age:15
Id:1, Name: Mahesh, Age:12
Id:3, Name: Nilesh, Age:10
Stream sorted() with Map
Here we are sorting a Map by key as well as value.
SortMap.java
tepage;
sit downimport java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
public class SortMap {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(15, "Mahesh");
map.put(10, "Suresh");
map.put(30, "Nilesh");
System.out.println("---Sort by Map Value---");
.
forEach(e -> System.out.println("Key: "+ e.getKey() +", Value: "+ e.getValue()));
System.out.println("---Sort by Map Key---");
.forEach(e -> System.out.println("Key: "+ e.getKey() +", Value: "+ e.getValue()));
}
}
Output
---Sort by Map Value---
Key: 15, Value: Mahesh
Key: 30, Value: Nilesh
Key: 10, Value: Suresh
-
--Sort by Map Key---
Key: 10, Value: Suresh
Key: 15, Value: Mahesh
Key: 30, Value: Nilesh
Here we are sorting a map who values are custom objects.中英翻译网站
SortMapOfCustomObject.java
tepage;
import java.util.Comparator;
import java.util.HashMap;
cellpadding
import java.util.Map;
public class SortMapOfCustomObject {
2015六级真题
public static void main(String[] args) {
Map<Integer, Student> map = new HashMap<>();
map.put(1, new Student(1, "Mahesh", 12));
map.put(2, new Student(2, "Suresh", 15));
map.put(3, new Student(3, "Nilesh", 10));
//Map Sorting by Value i.e student's natural ordering i.e by name
.forEach(e -> {
Integer key = (Key();
Student std = (Value();
System.out.println("Key: " + key +", value: ("+ Id() +", "+ Name()+", "+ Age()+")");
});
}
}
Output
Key: 1, value: (1, Mahesh, 12)
Key: 3, value: (3, Nilesh, 10)
Key: 2, value: (2, Suresh, 15)