玩法
msg | link | |
---|---|---|
groupby | link | |
sort | 就在下面 | link |
link | ||
link | ||
link | ||
link | ||
link | ||
link | ||
link | ||
link | ||
link | ||
link |
java lamuda 进行排序,但是属性是字符串数字,怎么办
charge = charge.stream().sorted().sorted(Comparator.comparing(e->Double.parseDouble(e.getNum()))).collect(Collectors.toList());
But 排序的实体,不能是静态内部类
如果是静态内部类呢
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class OuterClass {
public static void main(String[] args) {
List<GetChargePageDataRes.ChargeData> chargeList = new ArrayList<>();
// 添加ChargeData对象到列表中
chargeList.add(new GetChargePageDataRes.ChargeData(30));
chargeList.add(new GetChargePageDataRes.ChargeData(10));
chargeList.add(new GetChargePageDataRes.ChargeData(50));
chargeList.add(new GetChargePageDataRes.ChargeData(20));
// 使用Collections.sort进行排序
Collections.sort(chargeList);
// 打印排序结果
for (GetChargePageDataRes.ChargeData charge : chargeList) {
System.out.println(charge.getAmount());
}
}
static class GetChargePageDataRes {
static class ChargeData implements Comparable<ChargeData> {
private int amount;
public ChargeData(int amount) {
this.amount = amount;
}
public int getAmount() {
return amount;
}
@Override
public int compareTo(ChargeData other) {
// 自定义比较逻辑,这里按amount进行升序排序
return Integer.compare(this.amount, other.amount);
}
}
}
}
List中内容相加
long sum = list.stream().mapToLong(User::getAge).sum();
List<Integer>降序
public static void main(String[] args) {
List<Integer> list = Arrays.asList(3, 1, 5, 100);
List<Integer> newList = list.stream().sorted(Comparator.comparing(Integer::intValue).reversed())
.collect(Collectors.toList());
}
实体降序排序
List<User> newList = list.stream().sorted(Comparator.comparing(User::getAge).reversed())
.collect(Collectors.toList());
实体升序
List<User> newList = list.stream().sorted(Comparator.comparing(User::getAge))
.collect(Collectors.toList());
先降序后升序排序
package com.jiediankeji.test_action.start.javatest.lam.sort;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class TestSort {
@Data
@Accessors(chain = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class A{
private String cc;
private int aa;
private int bb;
}
public static void main(String[] args) {
ArrayList<A> as = CollUtil.newArrayList(List.of(
A.builder().aa(1).bb(0).cc("aa").build(),
A.builder().aa(9).bb(99).cc("aa").build(),
A.builder().aa(8).bb(99).cc("aa").build(),
A.builder().aa(1).bb(4).cc("aa").build(),
A.builder().aa(9).bb(1).cc("aa").build()
));
//先降序后升序
List<A> collect = as.stream().sorted(Comparator.comparing(A::getAa).reversed().thenComparing(Comparator.comparing(A::getBb))).collect(Collectors.toList());
System.out.println(JSON.toJSONString(as));
System.out.println(JSON.toJSONString(collect));
}
}
自己写的一个工具类,对比蓝不打,是真的难用
/**
* 对任意集合的排序方法
* @param object 要排序的实体类List集合
* @param sortField 排序字段
* @param sortMode true升序,false降序序
*/
public static <T> void sortList(List<T> object, final String sortField, final boolean sortMode) {
if (CollectionUtils.isEmpty(object) || object.size() < 2 || StringUtils.isBlank(sortField)) {
return;
}
Collections.sort(object, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
int retVal = 0;
String methodStr = "get" + sortField.substring(0, 1).toUpperCase() + sortField.substring(1);
try {
Method method1 = ((T) o1).getClass().getMethod(methodStr, null);
Method method2 = ((T) o2).getClass().getMethod(methodStr,null);
if (sortMode) {
retVal = method1.invoke(((T) o1), null).toString().compareTo(method2.invoke(((T) o2), null).toString());
} else {
retVal = method2.invoke(((T) o2), null).toString().compareTo(method2.invoke(((T) o1), null).toString());
}
} catch (Exception e) {
e.printStackTrace();
}
return retVal;
}
});
}
removeIf
List list =new ArrayList();
list.add(“1”);
list.add(“12”);
list.add(“13”);
list.add(“0”);
System.out.println(“初始时:”+ list.toString());
list.removeIf(s -> s.contains(“1”)); //对集合进行过滤,删除集合中包含“1”的元素
System.out.println(“过滤完:” + list.toString());