








package com.imooc;
@FunctionalInterface
public interface IUserCredential {
String verifyUser(String username);
default String getCredential(String username) {
if ("admin".equals(username)) {
return "admin + 系统管理员用户";
} else if("manager".equals(username)){
return "manager + 用户管理员用户";
} else {
return "commons + 普通会员用户";
}
}
}
package com.imooc;
@FunctionalInterface
public interface IMessageFormat {
String format(String message, String format);
String toString();
static boolean verifyMessage(String msg) {
if (msg != null) {
return true;
}
return false;
}
}
package com.imooc.impl;
import com.imooc.IUserCredential;
public class UserCredentialImpl implements IUserCredential {
@Override
public String verifyUser(String username) {
if ("admin".equals(username)) {
return "系统管理员";
} else if("manager".equals(username)) {
return "用户管理员";
}
return "普通会员";
}
}
package com.imooc.impl.impl;
import com.imooc.IMessageFormat;
public class MessageFormatImpl implements IMessageFormat {
@Override
public String format(String message, String format) {
System.out.println("消息转换...");
return message;
}
}
package imooclambda;
import com.imooc.IMessageFormat;
import com.imooc.IUserCredential;
import com.imooc.impl.UserCredentialImpl;
import com.imooc.impl.impl.MessageFormatImpl;
import java.util.Random;
import java.util.UUID;
import java.util.function.*;
public class App
{
String welcome = "慕课网欢迎您.";
public static void main( String[] args ) {
IUserCredential ic = new UserCredentialImpl();
System.out.println(ic.verifyUser("admin"));
System.out.println(ic.getCredential("admin"));
String msg = "hello world";
if (IMessageFormat.verifyMessage(msg)) {
IMessageFormat format = new MessageFormatImpl();
format.format(msg, "json");
}
IUserCredential ic2 = new IUserCredential() {
@Override
public String verifyUser(String username) {
return "admin".equals(username)?"管理员":"会员";
}
};
System.out.println(ic2.verifyUser("manager"));
System.out.println(ic2.verifyUser("admin"));
IUserCredential ic3 = (String username) -> {
return "admin".equals(username)?"lbd管理员": "lbd会员";
};
System.out.println(ic3.verifyUser("manager"));
System.out.println(ic3.verifyUser("admin"));
}
}




Predicate<String> pre = (String username) -> {
return "admin".equals(username);
};
System.out.println(pre.test("manager"));
System.out.println(pre.test("admin"));
Consumer<String> con = (String message) -> {
System.out.println("要发送的消息:" + message);
System.out.println("消息发送完成");
};
con.accept("hello 慕课网的学员们..");
con.accept("imooc lambda expression.");
Function<String, Integer> fun = (String gender) -> {
return "male".equals(gender)?1:0;
};
System.out.println(fun.apply("male"));
System.out.println(fun.apply("female"));
Supplier<String> sup = () -> {
return UUID.randomUUID().toString();
};
System.out.println(sup.get());
System.out.println(sup.get());
System.out.println(sup.get());
UnaryOperator<String> uo = (String img)-> {
img += "[100x200]";
return img;
};
System.out.println(uo.apply("原图--"));
BinaryOperator<Integer> bo = (Integer i1, Integer i2) -> {
return i1 > i2? i1: i2;
};
System.out.println(bo.apply(12, 13));

package imooclambda;
import com.imooc.IMessageFormat;
import com.imooc.IUserCredential;
import com.imooc.impl.UserCredentialImpl;
import com.imooc.impl.impl.MessageFormatImpl;
import java.util.Random;
import java.util.UUID;
import java.util.function.*;
public class App
{
String welcome = "慕课网欢迎您.";
public static void main( String[] args ) {
ILambda1 i1 = () -> {
System.out.println("hello imooc!");
System.out.println("welcome to imooc!");
};
i1.test();
ILambda1 i2 = () -> System.out.println("hello imooc");
i2.test();
ILambda2 i21 = (String n, int a) -> {
System.out.println(n + "say: my year's old is " + a);
};
i21.test("jerry", 18);
ILambda2 i22 = (n, a) -> {
System.out.println(n + " 说:我今年" + a + "岁了.");
};
i22.test("tom", 22);
ILambda3 i3 = (x, y) -> {
int z = x + y;
return z;
};
System.out.println(i3.test(11, 22));
ILambda3 i31 = (x, y) -> x + y;
System.out.println(i31.test(100, 200));
}
interface ILambda1{
void test();
}
interface ILambda2{
void test(String name, int age);
}
interface ILambda3 {
int test(int x, int y);
}
}

package imooclambda;
import java.util.ArrayList;
import java.util.List;
public class App2 {
String s1 = "全局变量";
public void testInnerClass() {
String s2 = "局部变量";
new Thread(new Runnable() {
String s3 = "内部变量";
@Override
public void run() {
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(this.s3);
}
}).start();
}
public void testLambda() {
String s2 = "局部变量lambda";
new Thread(() -> {
String s3 = "内部变量lambda";
System.out.println(this.s1);
System.out.println(s2);
System.out.println(s3);
s3 = "labmda 内部变量直接修改";
System.out.println(s3);
}).start();
}
public static void main(String[] args) {
App2 app = new App2();
app.testLambda();
}
}

package imooclambda;
import java.util.ArrayList;
import java.util.List;
public class App3 {
public static void test(MyInterface<String, List> inter) {
List<String> list = inter.strategy("hello", new ArrayList());
System.out.println(list);
}
public static void main(String[] args) {
test(new MyInterface<String, List>() {
@Override
public List strategy(String s, List list) {
list.add(s);
return list;
}
});
test((x, y) -> {
y.add(x);
return y;
});
}
}
@FunctionalInterface
interface MyInterface<T, R> {
R strategy (T t, R r);
}

package imooclambda;
public class App4 {
interface Param1 {
void outInfo(String info);
}
interface Param2 {
void outInfo(String info);
}
public void lambdaMethod(Param1 param) {
param.outInfo("hello param1 imooc!");
}
public void lambdaMethod(Param2 param) {
param.outInfo("hello param2 imooc");
}
public static void main(String[] args) {
App4 app = new App4();
app.lambdaMethod(new Param1() {
@Override
public void outInfo(String info) {
System.out.println(info);
}
});
app.lambdaMethod(new Param2() {
@Override
public void outInfo(String info) {
System.out.println("------");
System.out.println(info);
}
});
}
}



package com.imooc.test;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Person> personList = new ArrayList<>();
personList.add(new Person("tom", "男", 16));
personList.add(new Person("jerry", "女", 15));
personList.add(new Person("shuke", "男", 30));
personList.add(new Person("beita", "女", 26));
personList.add(new Person("damu", "男", 32));
Collections.sort(personList, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.getAge() - o2.getAge();
}
});
System.out.println(personList);
Collections.sort(personList, (p1, p2) -> p1.getAge() - p2.getAge());
Collections.sort(personList, Person::compareByAge);
PersonUtil pu = new PersonUtil();
Collections.sort(personList, pu::compareByName);
System.out.println("tom".hashCode());
System.out.println("jerry".hashCode());
System.out.println(personList);
IPerson ip = Person::new;
Person person = ip.initPerson("jerry", "男", 22);
System.out.println(person);
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Person {
private String name;
private String gender;
private int age;
public static int compareByAge(Person p1, Person p2) {
return p1.getAge() - p2.getAge();
}
}
class PersonUtil {
public int compareByName(Person p1, Person p2) {
return p1.getName().hashCode() - p2.getName().hashCode();
}
}
interface IPerson {
Person initPerson(String name, String gender, int age);
}

package com.imooc.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
public class Test2 {
public static void main(String[] args) {
List<String> accounts = new ArrayList<String>();
accounts.add("tom");
accounts.add("jerry");
accounts.add("beita");
accounts.add("shuke");
accounts.add("damu");
for (String account : accounts) {
if (account.length() >= 5) {
System.out.println("有效账号:" + account);
}
}
Iterator<String> it = accounts.iterator();
while(it.hasNext()) {
String account = it.next();
if (account.length() >= 5) {
System.out.println("it有效账号:" + account);
}
}
List validAccounts = accounts.stream().filter(s->s.length()>=5).collect(Collectors.toList());
System.out.println(validAccounts);
}
}

package com.imooc.test;
import java.io.BufferedReader;
public class Test3 {}

package com.imooc.test;
import java.util.*;
import java.util.stream.Stream;
public class Test1 {
public static void main(String[] args) {
Stream stream = Stream.of("admin", "tom", "damu");
String [] strArrays = new String[] {"xueqi", "biyao"};
Stream stream2 = Arrays.stream(strArrays);
List<String> list = new ArrayList<>();
list.add("少林");
list.add("武当");
list.add("青城");
list.add("崆峒");
list.add("峨眉");
Stream stream3 = list.stream();
Set<String> set = new HashSet<>();
set.add("少林罗汉拳");
set.add("武当长拳");
set.add("青城剑法");
Stream stream4 = set.stream();
Map<String, Integer> map = new HashMap<>();
map.put("tom", 1000);
map.put("jerry", 1200);
map.put("shuke", 1000);
Stream stream5 = map.entrySet().stream();
List<String> accountList = new ArrayList<>();
accountList.add("xongjiang");
accountList.add("lujunyi");
accountList.add("wuyong");
accountList.add("linchong");
accountList.add("luzhishen");
accountList.add("likui");
accountList.add("wusong");
List<Integer> intList = new ArrayList<>();
intList.add(20);
intList.add(19);
intList.add(7);
intList.add(8);
intList.add(86);
intList.add(11);
intList.add(3);
intList.add(20);
Optional optional = intList.stream().max((x, y)-> x-y);
System.out.println(optional.get());
Optional optional2 = intList.stream().reduce((sum, x)-> sum + x);
System.out.println(optional2.get());
}
}


package com.imooc.performance;
import java.util.*;
public class Test {
public static void main(String[] args) {
Random random = new Random();
List<Product> productList = new ArrayList<>();
for(int i = 0; i < 1000000; i++) {
productList.add(new Product("pro" + i, i, random.nextInt(Integer.MAX_VALUE)));
}
testProductStream(productList);
testProductParallelStream(productList);
testProductForloop(productList);
testProductStrongForloop(productList);
testProductIterator(productList);
}
public static void testStream(List<Integer> list) {
long start = System.currentTimeMillis();
Optional optional = list.stream().max(Integer::compare);
System.out.println(optional.get());
long end = System.currentTimeMillis();
System.out.println("testStream:" + (end - start) + "ms");
}
public static void testParallelStream(List<Integer> list) {
long start = System.currentTimeMillis();
Optional optional = list.parallelStream().max(Integer::compare);
System.out.println(optional.get());
long end = System.currentTimeMillis();
System.out.println("testParallelStream:" + (end - start) + "ms");
}
public static void testForloop(List<Integer> list) {
long start = System.currentTimeMillis();
int max = Integer.MIN_VALUE;
for(int i = 0; i < list.size(); i++) {
int current = list.get(i);
if (current > max) {
max = current;
}
}
System.out.println(max);
long end = System.currentTimeMillis();
System.out.println("testForloop:" + (end - start) + "ms");
}
public static void testStrongForloop(List<Integer> list) {
long start = System.currentTimeMillis();
int max = Integer.MIN_VALUE;
for (Integer integer : list) {
if(integer > max) {
max = integer;
}
}
System.out.println(max);
long end = System.currentTimeMillis();
System.out.println("testStrongForloop:" + (end - start) + "ms");
}
public static void testIterator(List<Integer> list) {
long start = System.currentTimeMillis();
Iterator<Integer> it = list.iterator();
int max = it.next();
while(it.hasNext()) {
int current = it.next();
if(current > max) {
max = current;
}
}
System.out.println(max);
long end = System.currentTimeMillis();
System.out.println("testIterator:" + (end - start) + "ms");
}
public static void testProductStream(List<Product> list) {
long start = System.currentTimeMillis();
Optional optional = list.stream().max((p1, p2)-> p1.hot - p2.hot);
System.out.println(optional.get());
long end = System.currentTimeMillis();
System.out.println("testProductStream:" + (end - start) + "ms");
}
public static void testProductParallelStream(List<Product> list) {
long start = System.currentTimeMillis();
Optional optional = list.stream().max((p1, p2)-> p1.hot - p2.hot);
System.out.println(optional.get());
long end = System.currentTimeMillis();
System.out.println("testProductParallelStream:" + (end - start) + "ms");
}
public static void testProductForloop(List<Product> list) {
long start = System.currentTimeMillis();
Product maxHot = list.get(0);
for(int i = 0; i < list.size(); i++) {
Product current = list.get(i);
if (current.hot > maxHot.hot) {
maxHot = current;
}
}
System.out.println(maxHot);
long end = System.currentTimeMillis();
System.out.println("testProductForloop:" + (end - start) + "ms");
}
public static void testProductStrongForloop(List<Product> list) {
long start = System.currentTimeMillis();
Product maxHot = list.get(0);
for (Product product : list) {
if(product.hot > maxHot.hot) {
maxHot = product;
}
}
System.out.println(maxHot);
long end = System.currentTimeMillis();
System.out.println("testProductStrongForloop:" + (end - start) + "ms");
}
public static void testProductIterator(List<Product> list) {
long start = System.currentTimeMillis();
Iterator<Product> it = list.iterator();
Product maxHot = it.next();
while(it.hasNext()) {
Product current = it.next();
if (current.hot > maxHot.hot) {
maxHot = current;
}
}
System.out.println(maxHot);
long end = System.currentTimeMillis();
System.out.println("testProductIterator:" + (end - start) + "ms");
}
}
class Product {
String name;
Integer stock;
Integer hot;
public Product(String name, Integer stock, Integer hot) {
this.name = name;
this.stock = stock;
this.hot = hot;
}
}

package com.imooc.performance;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Test2 {
public static void main(String[] args) {
List<Integer> lists = new ArrayList<Integer>();
for (int i = 0; i < 1000; i++){
lists.add(i);
}
List<Integer> list2 = new ArrayList<>();
lists.stream().forEach(x->list2.add(x));
System.out.println(lists.size());
System.out.println(list2.size());
List<Integer> list3 = new ArrayList<>();
lists.parallelStream().forEach(x-> list3.add(x));
System.out.println(list3.size());
List<Integer> list4 = lists.parallelStream().collect(Collectors.toList());
System.out.println(list4.size());
}
}