流API数组Lambda实例
在我之前的文章中,我们介绍并学习了java8中的lambda表达式 ,这篇文章是关于使用流API的常用数组lambda表达式的例子。
如何在java8中删除字符串数组中的空格?
删除/修剪每个字符串对象,并返回没有空白的字符串数组。
在java8中一步步使用lambda Stream map函数。
- 一个声明的字符串对象数组,其中一些字符串元素含有空白。
- 首先使用
Arrays.stream(array)方法将数组转换为流。 - 一旦有了流,你可以使用流类
map()方法,使用lambda表达式函数来修剪流中的每个字符串。 - 一旦流修改完成,你需要使用
toArray()方法将流转换为数组。
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
String[] stringArray = { "one ", "two ", " three ", " four " };
System.out.println("Original Array: " + Arrays.toString(stringArray));
String[] result = Arrays.stream(stringArray).map(value -> value.trim()).toArray(size -> new String[size]);
System.out.println("Trim Array: " + Arrays.toString(result));
}
}
Original Array: [one, two , three, four ]
Trim Array: [one, two, three, four]
如何在java8中找到一个字符串的字数
给定一个字符串是一组由空格分隔的单词。
这段代码返回每个字的数量,即在一个字符串中重复了多少次。
以下是具体步骤
- 使用正则表达式创建一个字串数组
- 接下来是使用Arrays.stream(array)方法创建一个流数组。
- 使用Collectors.Groupby与Function.ident()对相同的字符串进行分组-- 它就像SQL关键字的分组。
- 用collect方法将分组元素提供给reduction operator,返回带有单词和计数的Map。
import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String stringText = "This is a testing java stream example of a lamda expression example";
String[] word = stringText.trim().split("\\s+");
Map mapWordCount = Arrays.stream(word)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println("Map String with count:"+ mapWordCount);
}
}
输出:
Map String with count:{a=2, expression=1, java=1, stream=1, of=1, testing=1, This=1, lamda=1, is=1, example=2}
如何在java8中把原始的int数组转换为对象数组?
- 首先,使用
Arrays.stream()方法将原始数组转换为流。 - 接着,使用
boxer() method,该方法接收流数组并转换为流的整数。 - 最后,使用
toArray()将流转换为数组,返回整数对象数组。
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] primitiveIntArray = {11, 21, 13, 41, 15,61};
Integer[] integerObjectArray=Arrays.stream(primitiveIntArray).boxed().toArray(Integer[]::new);
System.out.println("Integer Object Array: " + Arrays.toString(integerObjectArray));
}
}
输出:
Integer Object Array: [11, 21, 13, 41, 15, 61]
如何在java8中把对象列表转换为原始数组?
这是一个在java8中将列表转换为Int[]的例子_
- 首先,创建一个雇员对象的列表,每个雇员对象包含id和name。
- 接下来,使用list.stream()方法将列表转换为流,将这个对象流传给mapToInt函数以转换为流的原始值。
- 最后,使用toArray()方法将流转换为数组。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class LamdaExpressionThisExample {
public static void main(String[] args) {
List emps = new ArrayList();
emps.add(new Emp(1, "one"));
emps.add(new Emp(4, "four"));
emps.add(new Emp(6, "six"));
emps.add(new Emp(9, "nine"));
emps.add(new Emp(12, "tweleve"));
int[] idsArray = emps.stream().mapToInt(Emp::getId).toArray();
System.out.println(Arrays.toString(idsArray));
}
}
class Emp {
Integer id;
String name;
Emp(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
输出:
Map String with count:{a=2, expression=1, java=1, stream=1, of=1, testing=1, This=1, lamda=1, is=1, example=2}
如何在Java 8中使用流和lambda对数字数组进行排序?
下面是在java8中对数组数字进行排序的一系列步骤
- 首先使用IntStream.of()方法转换intstream。
- 使用比较器的sorted、boxed和mapToInt还原操作进行排序
- 对于降序 - 使用比较器.reverseOrder()
- 对于升序--默认比较器。
下面的例子使用lambda和流类对数字进行升序和降序排序
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
int intArray[] = {56,11,1,67,34,3,90,2};
// Sort Descending Order
int[] sortedDescOutput = IntStream.of(intArray)
.boxed()
.sorted(Comparator.reverseOrder())
.mapToInt(i -> i)
.toArray();
// Sort Asecnding Order
int[] sortedAscOutput = IntStream.of(intArray)
.boxed()
.sorted()
.mapToInt(i -> i)
.toArray();
System.out.println(Arrays.toString(sortedDescOutput));
System.out.println(Arrays.toString(sortedAscOutput));
}
}
输出:
[90, 67, 56, 34, 11, 3, 2, 1]
[1, 2, 3, 11, 34, 56, 67, 90]
结论
学习了在java8中使用lambda表达式的数组流的多个例子。