![]() ![]() * In JSP, you can access the attribute like: Request.getRequestDispatcher("home.jsp").forward(request, response) ![]() At the server-side, set the attribute in the request then forward the request to the JSP page like the following:.This technique binds the attribute to the current request, the attribute is only visible to the current request and it keeps alive as far as the request is resolved or keeps being dispatched from servlet to servlet, this technique is very useful in web applications whenever you need to set dynamic attributes specific to the request cycle and need not be available anymore after the request.The common way of passing data from servlet to JSP is through defining attributes in the HTTP request and then forwarding it to the corresponding JSP, this is done on the server-side using one of the following techniques: In this tutorial, we explain the different ways of sending data from a Java servlet to JSP, along with several examples specific for passing data types as objects, arrays, lists, and maps. JSP is mostly used as the view component in any Java-based MVC application, its main usage is to present dynamic data processed and generated from server-side controllers like a servlet. List list = List.of( "a", "b", "c", "a") ĬollectionUtils. To do so, we create an empty HashSet and use the addAll() method of CollectionUtils class to copy all the elements from the List into the Set. We can also use Apache Commons Collections Library to convert a Java List to a Set. ![]() ![]() Set set = Sets.newHashSet(list) Code language: Java ( java ) List to Set Using Apache Commons Library The Sets class of the Guava Library provides the newHashSet() method that creates a new HashSet containing all the elements from the given Java Collection. Set set = Set.copyOf(list) Code language: Java ( java ) List to Set Using Guava Library To create an immutable Set from a List, we can use the copyOf() method of the Set interface. The HashSet created by the constructor is a mutable one. Set set = new HashSet(list) Code language: Java ( java ) The constructor creates a new HashSet instance containing all the unique elements of the given List.Įxample of converting a Java List to a mutable HashSet using Java. The most basic solution to convert a Java List to a Set is to use the HashSet constructor and provide the List. Thus, all the duplicate elements are r emoved when we convert a List to a Set. Please remember that a List is not a collection of unique elements. Now, let’s learn to convert a Java List to a Set. We understood how we could convert a Java Set to a List. List list = pyOf(set) Code language: Java ( java ) However, if we want to create an immutable List, we can use the copyOf() method of the List interface.Įxample of converting a Java Set to an immutable List using Java. List list = new ArrayList(set) Code language: Java ( java ) The ArrayList constructor creates a new ArrayList containing all the elements from the given Java collection.Įxample of converting a Java Set to a mutable List using Java. The most straightforward way of converting a Java Set to a List is to use the ArrayList constructor and supply the source Set. List to Set Using Apache Commons LibraryĪs the Sets are a unique collection of elements and lists are not, we can easily convert Sets to Lists.Set to List Using Apache Commons Library. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |