Him*_*shu 335 java sorting collections arraylist
我在java中有一个双打列表,我想按降序排序ArrayList.
输入ArrayList如下:
List<Double> testList = new ArrayList();
testList.add(0.5);
testList.add(0.2);
testList.add(0.9);
testList.add(0.1);
testList.add(0.1);
testList.add(0.1);
testList.add(0.54);
testList.add(0.71);
testList.add(0.71);
testList.add(0.71);
testList.add(0.92);
testList.add(0.12);
testList.add(0.65);
testList.add(0.34);
testList.add(0.62);
Run Code Online (Sandbox Code Playgroud)
输出应该是这样的
0.92
0.9
0.71
0.71
0.71
0.65
0.62
0.54
0.5
0.34
0.2
0.12
0.1
0.1
0.1
Run Code Online (Sandbox Code Playgroud)
Doo*_*nob 505
Collections.sort(testList);
Collections.reverse(testList);
Run Code Online (Sandbox Code Playgroud)
这将做你想要的.记得要导入Collections
!
bon*_*hoe 124
降:
Collections.sort(mArrayList, new Comparator<CustomData>() {
@Override
public int compare(CustomData lhs, CustomData rhs) {
// -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
return lhs.customInt > rhs.customInt ? -1 : (lhs.customInt < rhs.customInt) ? 1 : 0;
}
});
Run Code Online (Sandbox Code Playgroud)
M S*_*ach 92
使用java.util.Collections类的util方法,即
Collections.sort(list)
Run Code Online (Sandbox Code Playgroud)
实际上,如果要对自定义对象进行排序,则可以使用
Collections.sort(List<T> list, Comparator<? super T> c)
Run Code Online (Sandbox Code Playgroud)
看到集合api
krm*_*007 85
对于您的示例,这将在Java 8中发挥作用
List<Double> testList = new ArrayList();
testList.sort(Comparator.naturalOrder());
Run Code Online (Sandbox Code Playgroud)
但是,如果要按要排序的对象的某些字段进行排序,可以通过以下方式轻松完成:
testList.sort(Comparator.comparing(ClassName::getFieldName));
Run Code Online (Sandbox Code Playgroud)
要么
testList.sort(Comparator.comparing(ClassName::getFieldName).reversed());
Run Code Online (Sandbox Code Playgroud)
要么
testList.stream().sorted(Comparator.comparing(ClassName::getFieldName).reversed()).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
来源:https: //docs.oracle.com/javase/8/docs/api/java/util/Comparator.html
小智 51
使用lambdas(Java8),并将其剥离为最基本的语法(在这种情况下JVM将推断出很多),您得到:
Collections.sort(testList, (a, b) -> b.compareTo(a));
Run Code Online (Sandbox Code Playgroud)
一个更详细的版本:
// Implement a reverse-order Comparator by lambda function
Comparator<Double> comp = (Double a, Double b) -> {
return b.compareTo(a);
};
Collections.sort(testList, comp);
Run Code Online (Sandbox Code Playgroud)
可以使用lambda,因为Comparator接口只有一个方法可以实现,因此VM可以推断出正在实现的方法.由于可以推断出params的类型,所以不需要说明它们(即(a, b)
代替(Double a, Double b)
.而且由于lambda体只有一条线,并且该方法预期会返回一个值,因此return
推断并且支撑没有必要.
rob*_*ins 29
使用Java8,List接口上有一个默认的排序方法,如果您提供Comparator,它将允许您对集合进行排序.您可以轻松地按如下方式对问题中的示例进行排序:
testList.sort((a, b) -> Double.compare(b, a));
Run Code Online (Sandbox Code Playgroud)
注意:传递给Double.compare时,lambda中的args会被交换,以确保排序是降序的
小智 26
您可以使用它Collections.sort(list)
来排序list
您是否list
包含Comparable
元素.否则我会建议你像这里一样实现该接口:
public class Circle implements Comparable<Circle> {}
Run Code Online (Sandbox Code Playgroud)
当然,提供您自己的compareTo
方法实现,如下所示:
@Override
public int compareTo(Circle another) {
if (this.getD()<another.getD()){
return -1;
}else{
return 1;
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以再次使用,Colection.sort(list)
因为现在列表包含可比较类型的对象,可以进行排序.顺序取决于compareTo
方法.有关更多详细信息,请查看此https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html.
Zhe*_*lov 12
这是一个简短的备忘单,涵盖了典型案例:
import static java.util.Comparator.comparing;
// sort
list.sort(naturalOrder());
// sort (reversed)
list.sort(reverseOrder());
// sort by field
list.sort(comparing(Type::getField));
// sort by field (reversed)
list.sort(comparing(Type::getField).reversed());
// sort by int field
list.sort(comparingInt(Type::getIntField));
// sort by double field (reversed)
list.sort(comparingDouble(Type::getDoubleField).reversed());
// sort by nullable field (nulls last)
list.sort(comparing(Type::getNullableField, nullsLast(naturalOrder())));
// two-level sort
list.sort(comparing(Type::getField1).thenComparing(Type::getField2));
Run Code Online (Sandbox Code Playgroud)
Mat*_*att 11
Collections.sort
允许您传递Comparator
定义排序逻辑的实例.因此,与其在排序自然顺序列表,然后扭转它,人们可以简单地传递Collections.reverseOrder()
到sort
以逆向排序列表:
// import java.util.Collections;
Collections.sort(testList, Collections.reverseOrder());
Run Code Online (Sandbox Code Playgroud)
正如@ Marco13所提到的,除了更惯用(并且可能更有效)之外,使用逆序比较器可确保排序稳定(意味着元素的顺序在根据比较器相等时不会改变,而倒车将改变顺序)
小智 9
//Here is sorted List alphabetically with syncronized
package com.mnas.technology.automation.utility;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
/**
* @author manoj.kumar
*/
public class SynchronizedArrayList {
static Logger log = Logger.getLogger(SynchronizedArrayList.class.getName());
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List<Employee> synchronizedList = Collections.synchronizedList(new ArrayList<Employee>());
synchronizedList.add(new Employee("Aditya"));
synchronizedList.add(new Employee("Siddharth"));
synchronizedList.add(new Employee("Manoj"));
Collections.sort(synchronizedList, new Comparator() {
public int compare(Object synchronizedListOne, Object synchronizedListTwo) {
//use instanceof to verify the references are indeed of the type in question
return ((Employee) synchronizedListOne).name
.compareTo(((Employee) synchronizedListTwo).name);
}
});
/*for( Employee sd : synchronizedList) {
log.info("Sorted Synchronized Array List..."+sd.name);
}*/
// when iterating over a synchronized list, we need to synchronize access to the synchronized list
synchronized (synchronizedList) {
Iterator<Employee> iterator = synchronizedList.iterator();
while (iterator.hasNext()) {
log.info("Sorted Synchronized Array List Items: " + iterator.next().name);
}
}
}
}
class Employee {
String name;
Employee(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
List<String> yourList = new ArrayList<String>();
Collections.sort(yourList, Collections.reverseOrder());
Run Code Online (Sandbox Code Playgroud)
Collection 有一个默认的比较器可以帮助您。
另外,如果你想使用 Java 8 的一些新功能,你可以这样做:
List<String> yourList = new ArrayList<String>();
yourList = yourList.stream().sorted(Collections.reverseOrder()).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Java SE 8,那么这可能会有所帮助.
//create a comparator object using a Lambda expression
Comparator<Double> compareDouble = (d1, d2) -> d1.compareTo(d2);
//Sort the Collection in this case 'testList' in reverse order
Collections.sort(testList, Collections.reverseOrder(compareDouble));
//print the sorted list using method reference only applicable in SE 8
testList.forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
|*| 排序列表:
import java.util.Collections;
Run Code Online (Sandbox Code Playgroud)
| =>排序Asc订单:
Collections.sort(NamAryVar);
Run Code Online (Sandbox Code Playgroud)
| =>排序Dsc顺序:
Collections.sort(NamAryVar, Collections.reverseOrder());
Run Code Online (Sandbox Code Playgroud)
|*| 反转List的顺序:
Collections.reverse(NamAryVar);
Run Code Online (Sandbox Code Playgroud)
在 JAVA 8 中,现在变得容易多了。
List<String> alphaNumbers = Arrays.asList("one", "two", "three", "four");
List<String> alphaNumbersUpperCase = alphaNumbers.stream()
.map(String::toUpperCase)
.sorted()
.collect(Collectors.toList());
System.out.println(alphaNumbersUpperCase); // [FOUR, ONE, THREE, TWO]
Run Code Online (Sandbox Code Playgroud)
-- 反向使用这个
.sorted(Comparator.reverseOrder())
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
907123 次 |
最近记录: |