Skip to main content

Posts

Showing posts from September, 2020

How to get the length of a Collection in the JSF expression language?

 Problem How to get the length of a Collection in the JSF expression language? Solution There are two ways to get the length of a Collection i.e. List, Set, etc in the JSF expression language a) Define a method in the Bean In your bean declare a method to return the length of a collection @Named("MyBean ") @SessionScoped public class MyBean {     private List list;     .     .     .     public int getCollectionLength() {       return  list.size();     } } b) Using Facelets,the length function #{ fn:length(MyBean.list) }

How to convert Enum to List of String in Java 8?

Problem  How to convert Enum to List of String in Java 8?  Solution  Using the Java 8 Streams, its map and collect functions we can convert Enum to List of Strings  Code   import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class EnumToStringList { public enum Alpha { A,B,C,D; }  public static void main(String[] args) { List<String> strings = Stream.of(Alpha.values()).map(                                               Alpha::name).collect( Collectors.toList()); } }