Showing posts with label Lambda Expressions. Show all posts
Showing posts with label Lambda Expressions. Show all posts
Friday, June 2, 2017
Java Experience latest Interview Quetions
1. Write the program to reverse the string without modify the special character?
2. Write the program to compare the object using comparable interface?
3. Write the program for singleton?
4. Explain concurrent packages?
5. Difference between @inject and @service annotation
6. Advantages of microservice and cassendra?
7. Write the program for executor library?
8. Whats inner join?
9. Write the program for rest controller?
10. Can we have multiple session factory in Hibernate?
11. Explain IOC container and dependency injection?
12. How many test cases you will write to test a method?
Monday, October 17, 2016
Streams Introduction
Streams:
--------
java.util.stream package (Streams),which enables the concise and declarative expression of possibly-parallel bulk operations on various data sources.
All stream computations share a common structure: They have a stream source, zero or moreintermediate operations, and a single terminal operation.
Stream source: Streams can create following ways
|
Method
|
Description
|
|
Collection.stream()
|
Create a stream from the elements of a collection.
|
|
Stream.of(T...)
|
Create a stream from the arguments passed to the factory method.
|
|
Stream.of(T[])
|
Create a stream from the elements of an array.
|
|
Stream.empty()
|
Create an empty stream.
|
|
Stream.iterate(T first, BinaryOperator<T> f)
|
Create an infinite stream consisting of the sequence first,
f(first), f(f(first)), ... |
|
Stream.iterate(T first, Predicate<T> test,
BinaryOperator<T> f) |
(Java 9 only) Similar to Stream.iterate(T first,
BinaryOperator<T> f), except the stream terminates on the first elements for which the test predicate returns false. |
|
Stream.generate(Supplier<T> f)
|
Create an infinite stream from a generator function.
|
|
IntStream.range(lower, upper)
|
Create an IntStream consisting of the elements from lower to upper,
exclusive. |
|
IntStream.rangeClosed(lower, upper)
|
Create an IntStream consisting of the elements from lower to upper,
inclusive. |
|
BufferedReader.lines()
|
Create a stream consisting of the lines from a BufferedReader.
|
|
BitSet.stream()
|
Create an IntStream consisting of the indexes of the set bits in a
BitSet. |
|
CharSequence.chars()
|
Create an IntStream corresponding to the chars in a String.
|
Intermediate operations :-
|
Operation
|
Contents
|
|
filter(Predicate<T>)
|
The elements of the stream matching the predicate
|
|
map(Function<T, U>)
|
The result of applying the provided function to the elements of the
stream |
|
flatMap(Function<T, Stream<U>>
|
The elements of the streams resulting from applying the provided
stream-bearing function to the elements of the stream |
|
distinct()
|
The elements of the stream, with duplicates removed
|
|
sorted()
|
The elements of the stream, sorted in natural order
|
|
Sorted(Comparator<T>)
|
The elements of the stream, sorted by the provided comparator
|
|
limit(long)
|
The elements of the stream, truncated to the provided length
|
|
skip(long)
|
The elements of the stream, discarding the first N elements
|
|
takeWhile(Predicate<T>)
|
(Java 9 only) The elements of the stream, truncated at the first element
for which the provided predicate is not true |
|
dropWhile(Predicate<T>)
|
(Java 9 only) The elements of the stream, discarding the initial segment
of elements for which the provided predicate is true |
Terminal stream operations:-
|
Operation
|
Description
|
|
forEach(Consumer<T> action)
|
Apply the provided action to each element of the stream.
|
|
toArray()
|
Create an array from the elements of the stream.
|
|
reduce(...)
|
Aggregate the elements of the stream into a summary value.
|
|
collect(...)
|
Aggregate the elements of the stream into a summary result container.
|
|
min(Comparator<T>)
|
Return the minimal element of the stream according to the comparator.
|
|
max(Comparator<T>)
|
Return the maximal element of the stream according to the comparator.
|
|
count()
|
Return the size of the stream.
|
|
{any,all,none}Match(Predicate<T>)
|
Return whether any/all/none of the elements of the stream match the
provided predicate. |
|
findFirst()
|
Return the first element of the stream, if present.
|
|
findAny()
|
Return any element of the stream, if present.
|
Lambda Expressions
Lambda expressions provide a clear and concise way of implementing a single-method interface using an expression. It allows you to reduce the amount of code you have to create and maintain. It's often a concise replacement for anonymous classes.
Functional Interfaces
Lambdas can only operate on a functional interface, which is an interface with just one abstract method. Functional interfaces can have any number of default or static methods.
When declaring a functional interface the @FunctionalInterface annotation can be added. This has no special effect, but a compiler error will be generated if this annotation is applied to an interface which is not functional, thus acting as a reminder that the interface should not be changed.
Examples of functional interfaces:
interface Foo1 {
void bar();
}
interface Foo2 {
int bar(boolean baz);
}
interface Foo3 {
String bar(Object baz, int mink);
}
interface Foo4 {
default String bar() { // default so not counted
return "baz";
}
void quux();
}
@FunctionalInterface
interface Foo5 {
void bar();
}
@FunctionalInterface
interface BlankFoo1 extends Foo3 { // inherits its abstract method from Foo3
}
@FunctionalInterface
interface Foo6 {
void bar();
boolean equals(Object obj); // overrides one of Object's method so not counted
}
Conversely, this is not a functional interface, as it has more than one method:
interface BadFoo {
void bar();
void quux(); // <-- Second method prevents lambda: which one do we use?
}
This is also not a functional interface, as it does not have any methods:
interface BlankFoo2 {}
Java 8 also provides a number of generic templated functional interfaces in the package java.util.function. For example, the built-in interface Predicate<T> wraps a single method which inputs a value of type T and returns a boolean.
Lambda Expressions
The basic structure of a Lambda expression is:
fi will then hold a singleton instance of an anonymous class which implements FunctionalInterfaceand where the one method's definition is { System.out.println("Hello") }. In other words, the above is exactly equivalent to:
FunctionalInterface fi = new FunctionalInterface() {
@Override
public void theOneMethod() {
System.out.println("Hello");
}
}
You cannot specify the name of the method when using a lambda—but you shouldn't need to, because a functional interface must have only one abstract method, so Java overrides that one.
In cases where the type of the lambda is not certain, (e.g. overloaded methods) you can add a cast to the lambda to tell the compiler what its type should be, like so:
Object fooHolder = (Foo1) () -> System.out.println("Hello");
System.out.println(fooHolder instanceof Foo1); // returns true
If the functional interface's single method takes parameters, the local formal names of these should appear between the brackets of the lambda. There is no need to declare the type of the parameter or return as these are taken from the interface (although it is not an error to declare the parameter types if you want to). Thus, these two examples are equivalent:
Foo2 longFoo = new Foo2() {
@Override
public int bar(boolean baz) {
if (baz) return 1; else return 0;
}
};
Foo2 shortFoo = (x) -> { if (x) return 1; else return 0; };
The parentheses around the argument can be omitted if the function only has one argument:
Foo2 np = x -> { if (x) return 1; else return 0; }; // okay
Foo3 np2 = x, y -> x.toString() + y // not okay
Implicit Returns
If the code placed inside a lambda is a Java expression rather than a statement, it is treated as a method which returns the value of the expression. Thus, the following two are equivalent:
IntUnaryOperator addOneShort = (x) -> (x+1);
IntUnaryOperator addOneLong = (x) -> { return (x+1); };
Accessing Local Variables (value closures)
Since lambdas are syntactic shorthand for anonymous classes, they follow the same rules for accessing local variables in the enclosing scope; the variables must be treated as final and not modified inside the lambda.
IntUnaryOperator makeAdder(int amount) {
return (x) -> (x + amount); // Legal even though amount will go out of scope
// because amount is not modified
}
IntUnaryOperator makeAccumulator(int value) {
return (x) -> { value += x; return value; }; // Will not compile
}
If it is necessary to wrap a changing variable in this way, a regular object that keeps a copy of the variable should be used.
Accepting Lambdas
Because a lambda is an implementation of an interface, nothing special needs to be done to make a method accept a lambda: any function which takes a functional interface can also accept a lambda.
public void passMeALambda(Foo1 f) {
f.bar();
}
passMeALambda(() -> System.out.println("Lambda called"));
Subscribe to:
Posts (Atom)
