::方法使用
条件:lambada表达式的主体仅包含一个表达式,且lambada表达式只调用一个已经存在的方法;被引用的方法的参数列表与lambada表达式的输入输出一致
以下是Java 8中方法引用的一些语法:
- 静态方法引用(static method)语法:
classname::methodname
例如:Person::getAge
- 对象的实例方法引用语法:
instancename::methodname
例如:System.out::println
- 对象的超类方法引用语法:
super::methodname
- 类构造器引用语法:
classname::new
例如:ArrayList::new
- 数组构造器引用语法:
typename[]::new
例如:String[]:new
DataStreamSource kafkaDS = env.addSource(MyKafkaUtil.getKafkaConsumer(sourceTopic, groupId));
kafkaDS.map(line->{return JSON.parseObject(line);});
//Lambda 简写
kafkaDS.map(JSON::parseObject);
@Test
void printTest1() {
List list = Arrays.asList("a", "b", "c", "d");
System.out.println("for => ");
for (String item : list) {
AcceptMethod.printValue(item);
}
System.out.println("forEach => ");
list.forEach(item -> AcceptMethod.printValue(item));
System.out.println("forEach => ::");
//list.forEach(new AcceptMethod()::printValue); //非静态方法
list.forEach(AcceptMethod::printValue);
System.out.println("forEach list => ::");
List loginUserList = new ArrayList();
LoginUser loginUser = new LoginUser();
loginUser.setOs("iOS");
loginUserList.add(loginUser);
loginUser = new LoginUser();
loginUser.setOs("Android");
loginUserList.add(loginUser);
loginUserList.forEach(AcceptMethod::printList);
}
public static class AcceptMethod {
public static void printValue(String str) {
System.out.println("print value : " + str);
}
public static void printList(LoginUser map) {
System.out.println("print value : " + map.getOs());
}
}
Stream 示例
public class Java8Tester {
public static void main(String args[]){
System.out.println("使用 Java 7: ");
// 计算空字符串
List strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
System.out.println("列表: " +strings);
long count = getCountEmptyStringUsingJava7(strings);
System.out.println("空字符数量为: " + count);
count = getCountLength3UsingJava7(strings);
System.out.println("字符串长度为 3 的数量为: " + count);
// 删除空字符串
List filtered = deleteEmptyStringsUsingJava7(strings);
System.out.println("筛选后的列表: " + filtered);
// 删除空字符串,并使用逗号把它们合并起来
String mergedString = getMergedStringUsingJava7(strings,", ");
System.out.println("合并字符串: " + mergedString);
List numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
// 获取列表元素平方数
List squaresList = getSquares(numbers);
System.out.println("平方数列表: " + squaresList);
List integers = Arrays.asList(1,2,13,4,15,6,17,8,19);
System.out.println("列表: " +integers);
System.out.println("列表中最大的数 : " + getMax(integers));
System.out.println("列表中最小的数 : " + getMin(integers));
System.out.println("所有数之和 : " + getSum(integers));
System.out.println("平均数 : " + getAverage(integers));
System.out.println("随机数: ");
// 输出10个随机数
Random random = new Random();
for(int i=0; i string.isEmpty()).count();
System.out.println("空字符串数量为: " + count);
count = strings.stream().filter(string -> string.length() == 3).count();
System.out.println("字符串长度为 3 的数量为: " + count);
filtered = strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.toList());
System.out.println("筛选后的列表: " + filtered);
mergedString = strings.stream().filter(string ->!string.isEmpty()).collect(Collectors.joining(", "));
System.out.println("合并字符串: " + mergedString);
squaresList = numbers.stream().map( i ->i*i).distinct().collect(Collectors.toList());
System.out.println("Squares List: " + squaresList);
System.out.println("列表: " +integers);
IntSummaryStatistics stats = integers.stream().mapToInt((x) ->x).summaryStatistics();
System.out.println("列表中最大的数 : " + stats.getMax());
System.out.println("列表中最小的数 : " + stats.getMin());
System.out.println("所有数之和 : " + stats.getSum());
System.out.println("平均数 : " + stats.getAverage());
System.out.println("随机数: ");
random.ints().limit(10).sorted().forEach(System.out::println);
// 并行处理
count = strings.parallelStream().filter(string -> string.isEmpty()).count();
System.out.println("空字符串的数量为: " + count);
}
private static int getCountEmptyStringUsingJava7(List strings){
int count = 0;
for(String string: strings){
if(string.isEmpty()){
count++;
}
}
return count;
}
private static int getCountLength3UsingJava7(List strings){
int count = 0;
for(String string: strings){
if(string.length() == 3){
count++;
}
}
return count;
}
private static List deleteEmptyStringsUsingJava7(List strings){
List filteredList = new ArrayList();
for(String string: strings){
if(!string.isEmpty()){
filteredList.add(string);
}
}
return filteredList;
}
private static String getMergedStringUsingJava7(List strings, String separator){
StringBuilder stringBuilder = new StringBuilder();
for(String string: strings){
if(!string.isEmpty()){
stringBuilder.append(string);
stringBuilder.append(separator);
}
}
String mergedString = stringBuilder.toString();
return mergedString.substring(0, mergedString.length()-2);
}
private static List getSquares(List numbers){
List squaresList = new ArrayList();
for(Integer number: numbers){
Integer square = new Integer(number.intValue() * number.intValue());
if(!squaresList.contains(square)){
squaresList.add(square);
}
}
return squaresList;
}
private static int getMax(List numbers){
int max = numbers.get(0);
for(int i=1;i max){
max = number.intValue();
}
}
return max;
}
private static int getMin(List numbers){
int min = numbers.get(0);
for(int i=1;i numbers){
return getSum(numbers) / numbers.size();
}
}
实例
/**
* java list 中的字符是否包括在另一个 list 中
*/
@Test
void anyMatchTest() {
List permission = Arrays.asList("A", "B", "D");
List arr = Arrays.asList("C", "D", "E", "F");
boolean b = arr.stream().anyMatch(permission::contains);
System.out.println("" + b);
}
/**
* 数组转SetList
*/
void anyMatchTest() {
String[] permsArr = "a,b,c".split(",");
Set permsSet = Arrays.stream(permsArr).collect(Collectors.toSet());
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net