骚操作之——lamda表达式

发布时间 2023-07-25 21:57:09作者: 专注视觉

Lambda表达式是一种用于简化函数定义的语法结构,它可以将一个匿名函数作为参数传递给其他函数或方法。以下是一些常见的Lambda表达式的用法,以及一个例子:

1. 列表排序:
List<Integer> numbers = Arrays.asList(5, 2, 1, 3, 4);
numbers.sort((a, b) -> a.compareTo(b));
```

2. 过滤列表:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> evenNumbers = numbers.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
```

3. 遍历列表:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.forEach(n -> System.out.println(n));
```

4. 映射列表:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<String> stringNumbers = numbers.stream()
.map(n -> String.valueOf(n))
.collect(Collectors.toList());

5. 按条件计算:
int result = calculate((a, b) -> a + b, 10, 5);

6. 计算集合的总和:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream()
.reduce(0, (a, b) -> a + b);

7. 使用自定义函数式接口:
interface MyFunction {
int calculate(int a, int b);
}

MyFunction add = (a, b) -> a + b;
int result = add.calculate(10, 5);
```

1. 遍历列表并打印每个元素的值
```
items = [1, 2, 3, 4, 5]
result = list(map(lambda x: x, items))
print(result)
```

2. 使用条件表达式过滤列表中的元素
```
items = [1, 2, 3, 4, 5]
result = list(filter(lambda x: x % 2 == 0, items))
print(result)
```

3. 对列表中的元素求平方
```
items = [1, 2, 3, 4, 5]
result = list(map(lambda x: x**2, items))
print(result)
```

4. 将列表中的元素转换为字符串
```
items = [1, 2, 3, 4, 5]
result = list(map(lambda x: str(x), items))
print(result)
```

5. 对字典列表按照某个键进行排序
```
items = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 20}]
result = sorted(items, key=lambda x: x['age'])
print(result)
```

6. 按照字符串长度对列表中的字符串排序
```
items = ['apple', 'banana', 'cherry', 'date']
result = sorted(items, key=lambda x: len(x))
print(result)
```

7. 使用加法运算符对两个数字进行操作
```
a = 3
b = 5
result = (lambda x, y: x + y)(a, b)
print(result)
```

8. 检查列表中是否存在满足特定条件的元素
```
items = [1, 2, 3, 4, 5]
result = any(x % 2 == 0 for x in items)
print(result)
```

9. 使用reduce计算列表中所有元素的乘积
```
from functools import reduce
items = [1, 2, 3, 4, 5]
result = reduce(lambda x, y: x * y, items)
print(result)
```

10. 将列表中的字符串转换为大写
```
items = ['apple', 'banana', 'cherry', 'date']
result = list(map(lambda x: x.upper(), items))
print(result)
```

11. 使用排序函数对列表中的元素进行自定义排序
```
items = [5, 3, 2, 4, 1]
result = sorted(items, key=lambda x: x % 3)
print(result)
```

12. 使用reduce计算列表中所有元素的和
```
from functools import reduce
items = [1, 2, 3, 4, 5]
result = reduce(lambda x, y: x + y, items)
print(result)
```

13. 使用filter过滤列表中的空字符串
```
items = ['apple', '', 'banana', '', 'cherry']
result = list(filter(lambda x: x != '', items))
print(result)
```

14. 将字典列表按照多个键进行排序
```
items = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 20}]
result = sorted(items, key=lambda x: (x['age'], x['name']))
print(result)
```

15. 将列表中的元素转换为整型
```
items = ['1', '2', '3', '4', '5']
result = list(map(lambda x: int(x), items))
print(result)
```

16. 检查列表中是否所有元素都满足特定条件
```
items = [2, 4, 6, 8, 10]
result = all(x % 2 == 0 for x in items)
print(result)
```

17. 判断列表中是否存在某个元素
```
items = ['apple', 'banana', 'cherry', 'date']
result = any(x == 'banana' for x in items)
print(result)
```

18. 使用排序函数对字典列表的键进行排序
```
items = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 20}]
result = sorted(items, key=lambda x: x.keys())
print(result)
```

19. 使用reduce计算列表中所有元素的平均值
```
from functools import reduce
items = [1, 2, 3, 4, 5]
result = reduce(lambda x, y: (x + y) / 2, items)
print(result)
```

20. 检查列表中是否所有字符串都以特定后缀结尾
```
items = ['apple', 'banana', 'cherry', 'date']
result = all(x.endswith('e') for x in items)
print(result)
```