【JAVA系列】JAVA与C#中List分组、排序方法

C# 中List分组、排序、动态分组

定义实体类

language 复制代码
public class Student
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Grade { get; set; }
} 

按单个属性分组

language 复制代码
class Program
{
    static void Main()
    {
        List<Student> students = new List<Student>
        {
            new Student { Name = "Alice", Age = 25, Grade = "A" },
            new Student { Name = "Bob", Age = 22, Grade = "B" },
            new Student { Name = "Charlie", Age = 24, Grade = "A" },
            new Student { Name = "David", Age = 23, Grade = "B" }
        };
     //1、语言集成查询 (LINQ)  group 分组
     var groupedByGrade =
              from student in students
              group student by student.Grade into newGroup
              //orderby newGroup.Key
              select newGroup;

        foreach (var group in groupedByGrade)
        {
            Console.WriteLine($"Grade: {group.Key}");
            foreach (var student in group)
            {
                Console.WriteLine($"Name: {student.Name}, Age: {student.Age}");
            }
        }
    }
}
使用方法语法的等效代码分组
language 复制代码
v ar groupByYearQuery = students
    .GroupBy(student => student.Grade)
    //.OrderBy(newGroup => newGroup.Key);

foreach (var yearGroup in groupByYearQuery)
{
    Console.WriteLine($"Key: {yearGroup.Key}");
    foreach (var student in yearGroup)
    {
        Console.WriteLine($"\t{student.LastName}, {student.FirstName}");
    }
}
排序
复制代码
 记住这么用就对了
 OrderBy:students.OrderBy(s => s.Age).ToList(); // 升序
 OrderByDescending: students.OrderByDescending(s => s.Age).ToList();//倒序
分组

这是个扩展方法,因为使用this。keySelector 就是个函数 ,表示用哪个键分组、并返回一个TKey类型的结果。 GroupedEnumerable 类来进行实际的分组操作返回一个实现了 IEnumerable<IGrouping<TKey, TSource>> 接口的结果集合。

language 复制代码
public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
    return new GroupedEnumerable<TSource, TKey, TSource>(source, keySelector, IdentityFunction<TSource>.Instance, null);
}
官网链接

其中也有要多个属性分组的示例,也就是嵌套分组吧
对数据分组 (C#)

JAVA 中的List分组、排序及动态分组排序

分组

language 复制代码
class Main {
    public static void main(String[] args) {
        List<Student> students = Arrays.asList(
            new Student("Alice", 25, "A"),
            new Student("Bob", 22, "B"),
            new Student("Charlie", 24, "A"),
            new Student("David", 23, "B")
        );

        Map<String, List<Student>> groupedByGrade = students.stream()
            .collect(Collectors.groupingBy(Student::getGrade));

        groupedByGrade.forEach((grade, list) -> {
            System.out.println("Grade: " + grade);
            list.forEach(s -> System.out.println("Name: " + s.getName() + ", Age: " + s.getAge()));
        });
    }
}

升序和倒序

原理就慢慢再懂吧。记住用就行

language 复制代码
List<Student> sortedStudent = students.stream()
    .sorted(Comparator.comparing(Student::getAge))
    .collect(Collectors.toList()); //升序

List<Student> sortedStudent = students.stream()
    .sorted(Comparator.comparing(Student::getAge).reversed())
    .collect(Collectors.toList()); //倒序

简简单单的记录一下自己在.net转java过程中遇到的一些知识点,不是很深
写的有不对或者理解不深刻的地方,请各位老师多多的提,非常感谢!
老师们,同伟太想进步了!!!

相关推荐
李少兄9 小时前
IDEA Apifox Helper 插件踩坑:2.0.15-243 版本“导出指定接口”参数描述丢失问题解析与修复方案
java·ide·intellij-idea
努力攀登的小k9 小时前
《新手练表:从建表到删表全操作|从零学会表操作》
java·数据库·mysql
Yetian_200010 小时前
Postman测试上传图片文件接口
java·postman
爪哇碎碎念10 小时前
【Java】从一次 cache.get(key) 进去:缓存击穿一瞬间打出 200 次 DB,JDK 17 实测三种解法
java·redis·缓存
csdn_aspnet10 小时前
Java 使总距离最短的最佳点位置(Optimum location of point to minimize total distance)
java·算法
源码_空间10 小时前
基于Springboot的在线编程练习判题平台(源码+文档)
java·spring boot·后端
小手cool12 小时前
二分查找:在有序数组中查找最接近 target 的元素索引
java·数据结构·算法
李白的手机12 小时前
深入理解 Spring @RefreshScope:从入门用法到源码原理,再到举一反三实战扩展
java·后端·spring
深入云栈12 小时前
Netty 4.2.x 源码深度解析 (二十五):PoolThreadCache 与引用计数 —— 线程缓存与内存泄漏检测
java·后端·架构
砚底藏山河13 小时前
量化行情横截面中性化闭式与回归实现
android·java·python·金融·kotlin