public void anonymousExample() {
String nonFinalVariable = "Non Final Example";
String variable = "Outer Method Variable";
new Thread(new Runnable() {
String variable = "Runnable Class Member";
public void run() {
String variable = "Run Method Variable";
//Below line gives compilation error.
//System.out.println("->" + nonFinalVariable);
System.out.println("->" + variable);
System.out.println("->" + this.variable);
}
}).start();
}
->Run Method Variable ->Runnable Class Member
interface Runnable { void run(); }
// Functional
interface Foo { boolean equals(Object obj); }
// Not functional; equals is already an implicit member
interface Bar extends Foo {int compare(String o1, String o2); }
// Functional; Bar has one abstract non-Object method
interface Comparator {
boolean equals(Object obj);
int compare(T o1, T o2);
}
// Functional; Comparator has one abstract non-Object method
interface Foo {int m(); Object clone(); }
// Not functional; method Object.clone is not public
interface X { int m(Iterable arg); }
interface Y { int m(Iterable arg); }
interface Z extends X, Y {}
// Functional: two methods, but they have the same signature
(String s)-> s.lengh; () -> 43; (int x, int y) -> x + y;
public class FirstLambdaExpression {
public String variable = "Class Level Variable";
public static void main(String[] arg) {
new FirstLambdaExpression().lambdaExpression();
}
public void lambdaExpression(){
String variable = "Method Local Variable";
String nonFinalVariable = "This is non final variable";
new Thread (() -> {
//Below line gives compilation error
//String variable = "Run Method Variable"
System.out.println("->" + variable);
System.out.println("->" + this.variable);
}).start();
}
}
->Method Local Variable ->Class Level Variable
//Lambda expression is enclosed within methods parameter block.
//Target interface type is the methods parameter type.
String user = doSomething(() -> list.getProperty(“propName”);
//Lambda expression is enclosed within a thread constructor
//target interface type is contructors paramter i.e. Runnable
new Thread (() -> {
System.out.println("Running in different thread");
}).start();
Comparator c = (s1, s2) -> s1.compareToIgnoreCase(s2);
ActionListenr listenr = event -> event.getWhen();
void invoke(Runnable r) {r.run()}
void Future invoke(Callable r) {return c.compute()}
//above are two methods, both takes parameter of type functional interface
Future s = invoke(() ->"Done"); //Which invoke will be called?
public class FirstSightWithLambdaExpressions {
public static void main(String[] args) {
List list = Arrays.asList(
(Callable)()->"callable 1",
(Callable) ()->"callable 2",
(Callable) ()->"callable 3");
ExecutorService e = Executors.newFixedThreadPool(2);
List futures = null;
try {
futures = e.invokeAll(list);
new FirstSightWithLambdaExpressions().dumpList(futures);
} catch (InterruptedException | ExecutionException e1) {
e1.printStackTrace();
}
e.shutdown();
}
public void dumpList(List list) throws InterruptedException,
ExecutionException {
for (Future future : list) {
System.out.println(future.get());
}
}
}
System::getProperty "abc"::length String::length super::toString ArrayList::new
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class MethodReference {
public static void main (String[] ar){
Employee[] employees = {new Employee("Nick"), new Employee("Robin"), new Employee("Josh"), new Employee("Andy"), new Employee("Mark")};
System.out.println("Before Sort:");
dumpEmployee(employees);
Arrays.sort(employees, Employee::myCompare);
System.out.println("After Sort:");
dumpEmployee(employees);
}
public static void dumpEmployee(Employee[] employees){
for(Employee emp : Arrays.asList(employees)){
System.out.print(emp.name+", ");
}
System.out.println();
}
}
class Employee {
String name;
Employee(String name) {
this.name = name;
}
public static int myCompare(Employee emp1, Employee emp2) {
return emp1.name.compareTo(emp2.name);
}
}
Before Sort: Nick, Robin, Josh, Andy, Mark, After Sort: Andy, Josh, Mark, Nick, Robin,
public static int myCompare(Employee emp1, Employee emp2) {
return emp1.name.compareTo(emp2.name);
}
//Another method with the same name as of the above.
public static int myCompare(Integer int1, Integer int2) {
return int1.compareTo(int2);
}
Employee[] employees = {new Employee("Nick"), new Employee("Robin"),
new Employee("Josh"), new Employee("Andy"), new Employee("Mark")};
Integer[] ints = {1 , 4, 8, 2, 3, 8, 6};
Arrays.sort(employees, Employee::myCompare); Arrays.sort(ints, Employee::myCompare);
Arrays.sort(ints, Integer::compareTo);
public class ConstructorReference {
public static void main(String[] ar){
MyInterface in = MyClass::new;
System.out.println("->"+in.getMeMyObject());
}
}
interface MyInterface{
MyClass getMeMyObject();
}
class MyClass{
MyClass(){}
}
->com.MyClass@34e5307e
public class ConstructorReference {
public static void main(String[] ar){
EmlpoyeeProvider provider = Employee::new;
Employee emp = provider.getMeEmployee("John", 30);
System.out.println("->Employee Name: "+emp.name);
System.out.println("->Employee Age: "+emp.age);
}
}
interface EmlpoyeeProvider{
Employee getMeEmployee(String s, Integer i);
}
class Employee{
String name;
Integer age;
Employee (String name, Integer age){
this.name = name;
this.age = age;
}
}
->Employee Name: John ->Employee Age: 30
public class DefaultMethods {
public static void main(String[] ar){
NormalInterface instance = new NormalInterfaceImpl();
instance.myNormalMethod();
instance.myDefaultMethod();
}
}
interface NormalInterface{
void myNormalMethod();
void myDefaultMethod () default{
System.out.println("-> myDefaultMethod");
}
}
class NormalInterfaceImpl implements NormalInterface{
@Override
public void myNormalMethod() {
System.out.println("-> myNormalMethod");
}
}
-> myDefaultMethod
interface ParentInterface{
void initiallyNormal();
void initiallyDefault () default{
System.out.println("-> myDefaultMethod");
}
}
interface ChildInterface extends ParentInterface{
void initiallyNormal() default{
System.out.println("now default - > initiallyNormal");
}
void initiallyDefault (); //Now a normal method
}
public class DefaultMethods {
public static void main(String[] ar){
Interfaxe impl = new NormalInterfaceImpl();
impl.defaultMethod();
}
}
class ParentClass{
public void defaultMethod() {
System.out.println("->ParentClass");
}
}
interface Interfaxe{
public void defaultMethod() default{
System.out.println("->Interfaxe");
}
}
class NormalInterfaceImpl extends ParentClass implements Interfaxe{}
->ParentClass
public class DefaultMethods {
public static void main(String[] ar){
FirstInterface impl = new NormalInterfaceImpl();
impl.defaultMethod();
}
}
interface FirstInterface{
public void defaultMethod() default{
System.out.println("->FirstInterface");
}
}
interface SecondInterface{
public void defaultMethod() default{
System.out.println("->SecondInterface");
}
}
class NormalInterfaceImpl implements FirstInterface, SecondInterface{
public void defaultMethod(){
SecondInterface.super.defaultMethod();
}
}
->SecondInterface
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有