流API数组Lamda实例
如何在java8中删除字符串数组中的空格?
一个由字符串对象组成的数组,其中一些字符串元素含有空白。
删除/修剪每个字符串对象并返回没有空白的字符串数组。
在java8中使用lambda Stream map函数
首先使用Arrays.stream(array)方法将数组转换为流。
一旦有了流,你可以使用流类map()方法,使用lambda表达式函数来修剪流中的每个字符串。
一旦流修改完成,你需要使用toArray()方法将流转换为数组。
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.identity()对相同的字符串进行分组--*它就像SQL关键字group by
使用collect方法将分组元素提供给reduction operator 返回带有单词和其计数的Map
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数组转换为Object数组?
首先使用Arrays.stream()方法将原始数组转换为流
接下来使用boxer()方法,该方法接收流数组并转换为流的整数
使用toArray()将流转换为数组,返回整数对象数组
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;
}
}
如何在Java 8中使用流和lambda对一个数字数组进行排序?
首先使用IntStream.of()方法转换intstream。
使用排序、盒式和mapToInt减少操作进行排序,使用比较器
对于降序--使用比较器.reverseOrder()
对于升序--默认比较器
下面的例子使用lambda和流类对数字进行升序和降序排序
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]