【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过程中遇到的一些知识点,不是很深
写的有不对或者理解不深刻的地方,请各位老师多多的提,非常感谢!
老师们,同伟太想进步了!!!

相关推荐
武子康2 小时前
Java-72 深入浅出 RPC Dubbo 上手 生产者模块详解
java·spring boot·分布式·后端·rpc·dubbo·nio
_殊途2 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
椰椰椰耶3 小时前
【Spring】拦截器详解
java·后端·spring
没有bug.的程序员4 小时前
JAVA面试宝典 - 《MyBatis 进阶:插件开发与二级缓存》
java·面试·mybatis
没有羊的王K5 小时前
SSM框架学习——day1
java·学习
又菜又爱coding6 小时前
安装Keycloak并启动服务(macOS)
java·keycloak
时光追逐者6 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 46 期(2025年7.7-7.13)
c#·.net·.netcore
不知道叫什么呀6 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc
mit6.8246 小时前
Why C# and .NET are still relevant in 2025
c#
liulilittle6 小时前
.NET ExpandoObject 技术原理解析
开发语言·网络·windows·c#·.net·net·动态编程