Coding

Published on October 26th, 2016 | by Guest

0

Java developers using forEach and lambda expression in Java 8

In this post, experts will explain the features of Java 8 update in detail. If you have any requirement of java development, hire java developers who are having knowledge of these features and can use them during the process.

Java 8 has got lambda expressions support, which is considered to be the best feature of Java 8. With the help of lambda expressions, developers are able to facilitate programming and perform development of application with an ease.

In java 8 the forEach loop of iteration is little bit change with the addition of lambda expression as compare to java 7.

As we know that lambda expression is new stuff in Java 8 and in this post, we will describe how to use in forEach loop of List and Map.

Following example is describing iterating loop using normal way and using lambda expression of collection Map.

Normal way:

public static void main(String args[]) 
{
        Map<Integer, Integer> map = new HashMap<Integer, Integer>(0);
        // this loop is to add key and value to Map
        for (int i = 0; i < 15; i++) {
            map.put(i, i + i);
        }
        
        //Noramal Way to iterate loop using forEach
        
        for (Map.Entry<Integer, Integer> mapIterate : map.entrySet() ) 
        {
            
            System.out.println(mapIterate.getKey()+" "+mapIterate.getValue());
        }
        
}

Using Lambda expression:

public static void main(String args[]) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>(0);
        // this loop is to add key and value to Map
        for (int i = 0; i < 15; i++) {
            map.put(i, i + i);
        }

        map.forEach((k,v)->System.out.println("Key:"+ k + "Value:" + v));
        
}

Now we will see how lambda is use in collection List:
Normal Way:

public static void main(String args[]){

        List<Integer> list = new ArrayList<Integer>(0);
        
        for(int i=0; i< 15; i++){
            
            list.add(i);
        }
        
        for(Integer value : list){
            System.out.println(value);
        }
}

Normal Way:

public static void main(String args[]){
        
        List<Integer> list = new ArrayList<Integer>(0);
        
        for(int i=0; i< 15; i++){
            
            list.add(i);
        }
        
        list.forEach(value->System.out.println(value));
}

Filtering particular value is also one of the important parts in Collection. So now we will see how we filter using Lambda expression in Collection.

public static void main(String args[]){
        
        List<Integer> list = new ArrayList<Integer>(0);
        
        for(int i=0; i< 15; i++){
            
            list.add(i);
        }
        
        list.forEach(value->System.out.println(value));
    
        //Filtering value
        
        List<Integer> newList = list.stream().filter(u -> u.intValue() > 10).collect(Collectors.toList());
        
        newList.forEach(newValue->System.out.println(newValue));
       
}

In Map Collection:

public static void main(String args[]) {


        Map<Integer, Integer> map = new HashMap<Integer, Integer>(0);
        // this loop is to add key and value to Map
        for (int i = 0; i < 15; i++) {
            map.put(i, i + i);
        }
        
        //Noramal Way to iterate loop using forEach
        
        map.forEach((k,v)->System.out.println("Key:" + k + "Value:" + v));
        
        
        map.forEach((k,v)->{
            System.out.println("Key:" + k + "Value: " + v);
            if("5".equals(k)){
                System.out.println("Filter Value"+ k);
            }
    });
    }

With lambda expression, java developers can refer to final variable or effectively final variable. Lambda expression pushes a compilation error, if developer assigns a value for second time to the variable. You can check Lambda function with aws lambda unittest.

Kindly share your valuable thoughts in comments with us and tell other readers how you find the post. Do share and subscribe to the blog if you like this post.

Be it the lambda expressions support or anything else, you should hire java developers who know every detail about the Java 8 and its features. Share this blog with your friends and contacts who are looking for information on Java 8 lambda expression support.

Author bio:

James Warner is working as Java developer or hiring java developers in freelance works and projects. You can share your thoughts regarding this post with her and suggest some topics.

Tags: , , , ,


About the Author

Contribution of guest authors towards Techno FAQ blog



Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top ↑