Java easily convert from char or string to ENUM

One of my popular ways to make my code more readable is to make use of an ENUM.

If you have some “if” or “switch” statement:

             char myChar = 'D';
             if (myChar=='H') {
              //SOME CODE
              } else if (myChar=='V') {
                //SOME OTHER CODE
                 }

it does not say much about the functionality thats why I use enums

public enum myType {
        HORIZONTAL("H"),
        VERTICAL("V");
        private char directionToken;
        
        public static myType fromChar(char searchchar) {
            for (myType st : myType.values()) {
                if (st.directionToken == searchchar) {
                    return st;
                }
            }
            return null;
        }
        
        private myType(String transToken) {
            this.directionToken = transToken.charAt(0);
        }

        public char directionToken() {
            return directionToken;
        }
    }    

.....

             myType currRecordType = myType.fromChar(linechar[43]);
             switch (currRecordType) {
                 case HORIZONTAL:{
                     ...
                     break;
                     }
                     
                 case  VERTICAL:{
                     ...
                     break;
                 }
             }

Java 8 Lambda – Simple explanation of the Consumer Interface

I have not found a simple explanation of how the consumer interface works and how to use it in a lambda expression. Hopefully I will help you guys to see how easy it is to use and you will also simplify your code.
The legacy way to create a List and loop through it :

public class ConventionalForLoop {
    
    public static void main(String[] args) {
        List<Integer> il = new ArrayList<>();
        il.add(10); il.add(15); il.add(20); il.add(25);

        for (Iterator<Integer> i = il.iterator(); i.hasNext();) {
            Integer item = i.next();
            System.out.println("For Loop Value :" + item);
        }
    }
}
Now with Java 8 we can use a Consumer Interface and for explanation I have declared it.

public class ShowConsumer {
    public static void main(String[] args) {
        class ConsumeInt implements Consumer<Integer>
            {
                @Override
                public void accept(Integer i) {
                    System.out.println("Value From Consumer :" + i.toString());
                }
            }
        List<Integer> arrl = Arrays.asList(10,15,20,25);
        ConsumeInt ci = new ConsumeInt();
        arrl.forEach(ci);
    }
With the lambda expression it is very simple :

public class ShowConsumer {
    public static void main(String[] args) {
        List<Integer> arrl = Arrays.asList(10,15,20,25);
        arrl.forEach(i -> System.out.println("Value From Consumer :" + i.toString()));
    }
}

Java 8 Lambda Expressions quick explanation

To get started with Lambda expressions in Java 8 I have found that there was no simple explanation, so here is it its basic form for everyone to understand.
First of all look at this implementation of the Animal Interface. you will notice there are lots of unnecessary boilerplate code.
They are all Highlighted in the code:

  
package showlambda;

interface Animal {
    void eat();
}

class Dog implements Animal {
    @Override
    public void eat() {
        System.out.println("Feed me Anything");
    }
}

class Cat implements Animal {
    @Override
    public void eat() {
        System.out.println("Feed Me Meat");
    }
}

public class ShowLambda {
    public static void main(String[] args) {
        Cat c = new Cat();
        c.eat();
        Dog d = new Dog();
        d.eat();
    }
    
}
We dont have to declare the classes we can create one Anonymously like so:

package showlambda;

interface Animal {
    void eat();
}

public class ShowLambda {
    public static void main(String[] args) {
        Animal anonymousdog;
        Animal anonymouscat;
        anonymousdog = new Animal() {
            @Override
            public void eat() {
                System.out.println("Feed me Anything anonymously :-)") ;
            }
            
        };
        anonymouscat = new Animal() {
            @Override
            public void eat() {
                System.out.println("Feed me Meat anonymously :-)") ;
            }
        };
        anonymousdog.eat();
        anonymouscat.eat();
    }
}

But we are still stuck with all the boilerplate code.
The only thing that we really require is the implementation code and this is where the lambda comes in,
Now we don’t have to mention the eat method since there is only one:

package showlambda;

@FunctionalInterface
interface Animal {
    void eat();
}

public class ShowLambda {
    public static void main(String[] args) {
        Animal anonymousdog;
        Animal anonymouscat;
        anonymousdog = () -> 
            {
                System.out.println("Feed me Anything Yeah Lambda :-)") ;
            };
        anonymouscat = () -> 
            {
                System.out.println("Feed me Meat Yeah Lambda :-)") ;
            };
        anonymousdog.eat();
        anonymouscat.eat();
        
    }
}

Since there is just one line of code to implement eat we can even simplify it more:

package showlambda;

@FunctionalInterface
interface Animal {
    void eat();
}

public class ShowLambda {
    public static void main(String[] args) {
        Animal anonymousdog;
        Animal anonymouscat;
        anonymousdog = () -> System.out.println("Feed me Anything Yeah Lambda :-)") ;
        anonymouscat = () -> System.out.println("Feed me Meat Yeah Lambda :-)");
        anonymousdog.eat();
        anonymouscat.eat();
        
    }
}
Now to show how to implement a parameter:

package showlambda;

interface Animal{
    void eat(String food);
}

public class ShowLambda {
    public static void main(String[] args) {
        Animal anonymousdog;
        Animal anonymouscat;
        anonymousdog = (food) -> System.out.println("Feed me Anything Yeah Lambda :-) Parameter :" + food) ;
        anonymouscat = (f) -> System.out.println("Feed me Meat Yeah Lambda :-) Parameter :" + f);
        anonymousdog.eat("Dog Food");
        anonymouscat.eat("Cat Food");
        
    }
}