Functional interface can not have more than one abstract method but it can have more than one default methods.
public interface DefaultInterface1 {
public void printMessage1();
default void printMessage2(){
System.out.println("default method test1");
}
default void printMessage3(){
System.out.println("default method test2");
}
}
you can either use annotations
@FunctionalInterface
public interface DefaultInterface1 {
public void printMessage1();
default void printMessage2(){
System.out.println("default method test1");
}
default void printMessage3(){
System.out.println("default method test2");
}
}
public class DefaultInterface1Test implements DefaultInterface1 {
public static void main(String[] args) {
DefaultInterface1Test di1=new DefaultInterface1Test();
di1.printMessage1();
// default method test1
di1.printMessage2();
// default method test2
di1.printMessage3();
}
@Override
public void printMessage1() {
System.out.println("not default method");
}
}
Tuesday, April 29, 2014
Java 8 features : lambda expressions
Anonymous classes enable you to make your code more concise.
They enable you to declare and instantiate a class at the same time.
They are like local classes except that they do not have a name
public interface FunctionalInterface1 {
// Functional Interface by defining Single Abstract Method in interface.
public void printMessage();
}
public class FunctionalInterface1Test {
public static void main(String[] args) {
// anonymous inner class example
FunctionalInterface1 ft1=new FunctionalInterface1(){
public void printMessage(){
System.out.println("anonymous inner class example");
}
};
ft1.printMessage();
// lambda expression example
FunctionalInterface1 ft2=()->{
System.out.println("lambda test1");
System.out.println("lambda test2");
};
ft2.printMessage();
}
}
Functional interface can not have more than one abstract method but it can have more than one default methods.
You can use annotations for functional interfaces
@FunctionalInterface
public interface FunctionalInterface2 {
// Functional Interface by defining Single Abstract Method in interface.
public void printMessage();
// public void printMessage1();
}
if you uncomment printMessage1 it gives error below
Invalid '@FunctionalInterface' annotation; FunctionalInterface2 is not a functional interface
public class FunctionalInterface2Test {
public static void main(String[] args) {
FunctionalInterface2 fi1=new FunctionalInterface2() {
@Override
public void printMessage() {
System.out.println("anonymous inner class test1");
}
};
fi1.printMessage();
FunctionalInterface2 fi2=()->{
System.out.println("lambda expression test1");
};
fi2.printMessage();
}
}
Using functional interfaces with anonymous inner classes are a common pattern in Java
In addition to the EventListener classes, interfaces like Runnable and Comparator are used in a similar manner
Therefore, functional interfaces are leveraged for use with lambda expressions.
As Runnable is having Single Abstract Method, we can consider this as a Functional Interface and we can use Lambda expression
public class RunnableTest {
public static void main(String[] args) {
System.out.println("=== RunnableTest ===");
// Anonymous Runnable
Runnable r1 = new Runnable(){
@Override
public void run() {
System.out.println("anonymous works");
}
};
// Lambda Runnable
Runnable r2=()-> System.out.println("lambda works");
// Run em!
r1.run();
r2.run();
}
}
They enable you to declare and instantiate a class at the same time.
They are like local classes except that they do not have a name
public interface FunctionalInterface1 {
// Functional Interface by defining Single Abstract Method in interface.
public void printMessage();
}
public class FunctionalInterface1Test {
public static void main(String[] args) {
// anonymous inner class example
FunctionalInterface1 ft1=new FunctionalInterface1(){
public void printMessage(){
System.out.println("anonymous inner class example");
}
};
ft1.printMessage();
// lambda expression example
FunctionalInterface1 ft2=()->{
System.out.println("lambda test1");
System.out.println("lambda test2");
};
ft2.printMessage();
}
}
Functional interface can not have more than one abstract method but it can have more than one default methods.
You can use annotations for functional interfaces
@FunctionalInterface
public interface FunctionalInterface2 {
// Functional Interface by defining Single Abstract Method in interface.
public void printMessage();
// public void printMessage1();
}
if you uncomment printMessage1 it gives error below
Invalid '@FunctionalInterface' annotation; FunctionalInterface2 is not a functional interface
public class FunctionalInterface2Test {
public static void main(String[] args) {
FunctionalInterface2 fi1=new FunctionalInterface2() {
@Override
public void printMessage() {
System.out.println("anonymous inner class test1");
}
};
fi1.printMessage();
FunctionalInterface2 fi2=()->{
System.out.println("lambda expression test1");
};
fi2.printMessage();
}
}
Using functional interfaces with anonymous inner classes are a common pattern in Java
In addition to the EventListener classes, interfaces like Runnable and Comparator are used in a similar manner
Therefore, functional interfaces are leveraged for use with lambda expressions.
As Runnable is having Single Abstract Method, we can consider this as a Functional Interface and we can use Lambda expression
public class RunnableTest {
public static void main(String[] args) {
System.out.println("=== RunnableTest ===");
// Anonymous Runnable
Runnable r1 = new Runnable(){
@Override
public void run() {
System.out.println("anonymous works");
}
};
// Lambda Runnable
Runnable r2=()-> System.out.println("lambda works");
// Run em!
r1.run();
r2.run();
}
}
Wednesday, April 23, 2014
super() example
It’s legal to invoke "new" from within a constructor and to call
super() on a class with no explicit superclass.
public class Test1 {
static int count = 0;
Test1() {
while(count < 10)
new Test1(++count);
}
Test1(int x)
{
super();
}
public static void main(String[] args) {
new Test1();
new Test1(count);
System.out.println(count++);
}
}
outcome is 10 here.
super() on a class with no explicit superclass.
public class Test1 {
static int count = 0;
Test1() {
while(count < 10)
new Test1(++count);
}
Test1(int x)
{
super();
}
public static void main(String[] args) {
new Test1();
new Test1(count);
System.out.println(count++);
}
}
outcome is 10 here.
Sunday, April 20, 2014
how to mirror repository on artifactory
If you automatically generate settings.xml file on artifactory server.Mirror part looks like below
<mirrors>
<mirror>
<mirrorOf>*</mirrorOf>
<name>repo</name>
<url>http://localhost:5555/artifactory/repo</url>
<id>repo</id>
</mirror>
</mirrors>
Artifactory repositories use "**/*" syntax. You should replace mirror settings like below
<mirrors>
<mirror>
<mirirrorOf>**/*</mirrorOf>
<name>repo</name>
<url>http://localhost:5555/artifactory/repo</url>
<id>repo</id>
</mirror>
</mirrors>
<mirrors>
<mirror>
<mirrorOf>*</mirrorOf>
<name>repo</name>
<url>http://localhost:5555/artifactory/repo</url>
<id>repo</id>
</mirror>
</mirrors>
Artifactory repositories use "**/*" syntax. You should replace mirror settings like below
<mirrors>
<mirror>
<mirirrorOf>**/*</mirrorOf>
<name>repo</name>
<url>http://localhost:5555/artifactory/repo</url>
<id>repo</id>
</mirror>
</mirrors>
Subscribe to:
Posts (Atom)