雇员实体类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace TestList
{
/// <summary>
/// 雇员类
/// </summary>
public class Employee
{
/// <summary>
/// 雇员姓名
/// </summary>
public string EmpName { get; set; }
/// <summary>
/// 雇员性别
/// </summary>
public string EmpSex { get; set; }
/// <summary>
/// 雇员年龄
/// </summary>
public int EmpAge { get; set; }
/// <summary>
/// 雇员部门
/// </summary>
public string DeptName { get; set; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="empName"></param>
/// <param name="empSex"></param>
/// <param name="empAge"></param>
/// <param name="deptName"></param>
public Employee(string empName, string empSex, int empAge, string deptName)
{
EmpName = empName;
EmpSex = empSex;
EmpAge = empAge;
DeptName = deptName;
} public override string ToString()
{
return "Employee[EmpName=" + EmpName + ",EmpSex=" + EmpSex + ",EmpAge=" + EmpAge + ",DeptName=" + DeptName + "]";
}
}
}

   1.ForEach(Action<T> action)

      对集合的每个元素执行指定操作

List<Employee> listEmps = new List<Employee>();
//ForEach(Action<T> action) 对 System.Collections.Generic.List`1 的每个元素执行指定操作
employees.ForEach(p =>
{
if (p.EmpSex.Equals("女"))
{
listEmps.Add(p);
}
});

   2.FindAll(Predicate<T> match)

      检索与指定谓词定义的条件匹配的所有元素

 List<Employee> empList = employees.FindAll(p=>(p.EmpAge>));
if (empList.Count>0)
{
foreach (Employee emp in empList)
{
Console.WriteLine(emp.ToString());
}
}

3.Where

基于谓词筛选值序列

List<Employee> listEmployee = employees.Where(p => (p.EmpName.Contains("王"))).ToList();
if (listEmployee.Count>0)
{
foreach (Employee emp in listEmployee)
{
Console.WriteLine(emp.ToString());
}
}

4.RemoveAll(Predicate<T> match)

移除与指定的谓词所定义的条件相匹配的所有元素

employees.RemoveAll(p => (p.EmpAge >= ));
if (employees.Count>)
{
foreach (Employee emp in employees)
{
Console.WriteLine(emp.ToString());
}
}

   5.RemoveAt(int index) 

移除指定索引处的元素

if (employees.Count>)
{
for (int i=;i<employees.Count;i++)
{
Employee employee = employees[i];
if (employee.DeptName.Equals("市场部"))
{
employees.RemoveAt(i);
}
}
if (employees.Count > )
{
foreach (Employee emp in employees)
{
Console.WriteLine(emp.ToString());
}
}
}

完整Demo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace TestList
{
class Program
{
static void Main(string[] args)
{
List<Employee> employees = new List<Employee>();
Employee empOne = new Employee("王晶", "男", , "市场部");
Employee empTwo = new Employee("陈浩民", "男", , "技术部");
Employee empThree = new Employee("王诗玲", "女", , "市场部");
Employee empFour = new Employee("陈绍聪", "男", , "技术部");
Employee empFive = new Employee("张倩", "女", , "行政部");
employees.Add(empOne);
employees.Add(empTwo);
employees.Add(empThree);
employees.Add(empFour);
employees.Add(empFive); List<Employee> listEmps = new List<Employee>();
//ForEach(Action<T> action) 对 System.Collections.Generic.List`1 的每个元素执行指定操作
employees.ForEach(p =>
{
if (p.EmpSex.Equals("女"))
{
listEmps.Add(p);
}
});
if (listEmps.Count>)
{
foreach (Employee emp in listEmps)
{
Console.WriteLine(emp.ToString());
}
}
Console.WriteLine("========================="); //Where 基于谓词筛选值序列
List<Employee> listEmployee = employees.Where(p => (p.EmpName.Contains("王"))).ToList();
if (listEmployee.Count>)
{
foreach (Employee emp in listEmployee)
{
Console.WriteLine(emp.ToString());
}
}
Console.WriteLine("========================="); //FindAll(Predicate<T> match) 检索与指定谓词定义的条件匹配的所有元素
List<Employee> empList = employees.FindAll(p=>(p.EmpAge>));
if (empList.Count>)
{
foreach (Employee emp in empList)
{
Console.WriteLine(emp.ToString());
}
}
Console.WriteLine("========================="); //RemoveAll(Predicate<T> match) 移除与指定的谓词所定义的条件相匹配的所有元素
employees.RemoveAll(p => (p.EmpAge >= ));
if (employees.Count>)
{
foreach (Employee emp in employees)
{
Console.WriteLine(emp.ToString());
}
}
Console.WriteLine("========================="); //RemoveAt(int index) 移除指定索引处的元素
if (employees.Count>)
{
for (int i=;i<employees.Count;i++)
{
Employee employee = employees[i];
if (employee.DeptName.Equals("市场部"))
{
employees.RemoveAt(i);
}
}
if (employees.Count > )
{
foreach (Employee emp in employees)
{
Console.WriteLine(emp.ToString());
}
}
} Console.ReadLine();
}
}
}

C#-----集合List<T>的常用方法的更多相关文章

  1. python学习笔记_集合的定义和常用方法

    1.认识集合 定义: s={1,2,3,4,5} s=set("hello") s=set(["steven","job","da ...

  2. list集合的介绍和常用方法

    List接口介绍 java.util.List接口继承自Collection接口,是单列集合的一个重要分支,习惯性地会将实现了List接口的对象成为List集合.在List集合中允许出现重复的元素,所 ...

  3. python数据类型之集合(set)和其常用方法

    集合是一个无序的,不重复的数据组合 作用(集合的重点):1.去重,把一个列表变成集合就自动去重了2.关系测试,测试两组数据库之前的交集.差集.并集等关系 s = {1, 1, 2, 2, 3, 4, ...

  4. Java List集合的介绍与常用方法

    List接口的介绍 List接口简介: java.util.List接口继承自Collection接口,是单列集合的一个重要分支,习惯性地会将实现了List接口的对象称为List集合. 在List集合 ...

  5. Collections集合工具类的常用方法

    Collections集合工具类的方法 addAll与shuffle import java.util.ArrayList; import java.util.Collections; /* - ja ...

  6. 集合、set以及HASH

    集合的数据结构数据结构就是内存中保存输出数据的形式,不同的数据结构会有不同的特征.堆栈结构:先进后出 代表类(stack):应用场景:java中的方法运行时所占用的空间就是这种结构.队列结构:先进先出 ...

  7. Map接口下的集合和泛型理解

    一.Map接口 1. Map接口就是最顶层了,上面没有继承了.Map是一个容器接口,它与前面学的List.Set容器不同的是前面学的这些容器,一次只能传入一个元素,但是Map容器一次可以传入一对元素( ...

  8. 06_Java基础语法_第6天(自定义类、ArrayList集合)_讲义

    今日内容介绍 1.自定义类型的定义及使用 2.自定义类的内存图 3.ArrayList集合的基本功能 4.随机点名器案例及库存案例代码优化 01引用数据类型_类 * A: 数据类型 * a: java ...

  9. Java从零开始学二十三(集合Map接口)

    一.Map接口 Collection.Set.List接口都属于单值的操作,即:每次只能操作一个对象,而Map与它们不同的是,每次操作的是一对对象,即二元偶对象,Map中的每个元素都使用key à v ...

随机推荐

  1. ubuntu创建新用户

    ubuntu和windows一样,可以任意创建或者删除新的用户,windows下比较简单,ubuntu下需要使用命令,不过操作起来不是很繁琐,所以我尽量写的详细一些.  如何创建ubuntu新用户? ...

  2. 做rl_abs过程中遇到的问题

    问题一 运行 train_abstractor.py就出现这个问题 nohup: ignoring input start training with the following hyper-para ...

  3. 红帽 Red Hat Linux相关产品iso镜像下载【百度云】【更新7.2】

    RedHat Enterprise Server 6.7 for i386 Boot Disk:rhel-server-6.7-i386-boot.iso SHA-256 Checksum: 798d ...

  4. [LeetCode] Soup Servings 供应汤

    There are two types of soup: type A and type B. Initially we have N ml of each type of soup. There a ...

  5. SSM的,日常错误

    org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.imooc ...

  6. [JAVA] TicTacToe实现Socket通信(一)

    先来两张预览,大家可以试试jar包了,有什么问题评论哈,过两天贴代码 jar包这里下载 https://github.com/Andy-ZYA/TicTacToe_JAVA_Socket_Swing

  7. eclipse spring-boot-mybatis 的记录

    例子来源: https://gitee.com/lfalex/spring-boot-example.git spring-boot-mybatis 例子使用 mysql5.1.46 版本; 环境:e ...

  8. 总结-shell脚本

    执行脚本从 svn 检出项目 vi ace.sh #!/bin/bash svn export svn://127.0.0.1/ace/demo /ace/demo 设置脚本可执行 chmod +x ...

  9. centos下设置nodejs开机启动

    node环境的安装便不再赘述了,网上有很多教程,也非常简单. 上一篇博客介绍了用nginx代理nodejs.这一篇是使用pm2实现nodejs的自动重启. 什么是pm2? 如官网介绍的,pm2是nod ...

  10. 被sleep开了个小玩笑

    本案例转载自李大玉老师分享 Ⅰ.问题背景 探活脚本连续8次探测,判断主库异常,触发切换(判断备机是否有延迟,kill原主,VIP飘到备机,设置新主可写) 切换后,业务还是异常,SQL查询没返回,DB连 ...