How to Mapping code logic using Stream API
Stream API in Java provides a powerful way to work with collections and other data sources.
Mapping is one of the key operations available in the Stream API, used to transform elements of a stream. Here's a step-by-step guide on how to use the map
function with the Stream API:
Import Required Classes :
Import Required Classes :
First, make sure to import the necessary classes:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
You can create a stream from various sources, like collections, arrays, or generator functions. For example, creating a stream from a list:
List names = Arrays.asList("a14mk", "manoj", "Jack");
Use the map function to transform each element of the stream. For instance, to convert each name to uppercase:
Stream upperCaseNames = nameStream.map(String::toUpperCase);
Finally, you can collect the transformed stream back into a list or another collection:
List upperCaseNameList = upperCaseNames.collect(Collectors.toList());
Here’s a complete example demonstrating how to use the map function with the Stream API:
Mapping with Complex Objects:
If you are working with a list of objects, you can use the map function to transform specific fields or create new objects. For example:
In this example, the map function is used to extract the names from a list of Person objects.
Many-To-Many Hibernate Mapping
Many to Many Mapping : A many-to-many relationship refers to a relationship between tables in a database when a parent row in one table contains several child rows in the second table, and vice versa. Many-to-many relationships are often tricky to represent.
Many-to-Many mapping is usually implemented in database using a Join Table, for example we can have Cart, Item table and Cart_Items table for many-to-many mapping. Every cart can have multiple items and every item can be part of multiple carts, so we have a many to many mapping here.
1. Create Database Name is manytomany and Three Table are item, cart_items and cart.
Open NetBeans See The Menu Bar go to the file Menu Click on the New Project(Ctrl+Shift+N) and Select Left site categories Java/java web and Project categories to Right Site Project java/web Application .You have to give The Program Name "manytomany". if you wont to save Project Location you can Browse and File save on the direction place and Gone a default Place and Now uncheck/check the check box and click on the next button, select server settings and select the frameworks like Hibernate and go to the finish.
2. hibernate.cfg.xml
3. hibernate.reveng.xml
4. Item.java
5. CartItems.java
6. CartItemsId.java
7. Cart.java
8. Item.hbm.xml
9. CartItems.hbm.xml
10. Cart.hbm.xml
11. ManyToMany.java
OUTPUT:
Many-to-Many mapping is usually implemented in database using a Join Table, for example we can have Cart, Item table and Cart_Items table for many-to-many mapping. Every cart can have multiple items and every item can be part of multiple carts, so we have a many to many mapping here.
1. Create Database Name is manytomany and Three Table are item, cart_items and cart.
Open NetBeans See The Menu Bar go to the file Menu Click on the New Project(Ctrl+Shift+N) and Select Left site categories Java/java web and Project categories to Right Site Project java/web Application .You have to give The Program Name "manytomany". if you wont to save Project Location you can Browse and File save on the direction place and Gone a default Place and Now uncheck/check the check box and click on the next button, select server settings and select the frameworks like Hibernate and go to the finish.
2. hibernate.cfg.xml
3. hibernate.reveng.xml
4. Item.java
5. CartItems.java
6. CartItemsId.java
7. Cart.java
8. Item.hbm.xml
9. CartItems.hbm.xml
10. Cart.hbm.xml
11. ManyToMany.java
OUTPUT:
One-To-Many Hibernate Mapping
One to Many Mapping: A single attribute of a table relating with multiple attributes of another table is referred as One to Many mapping.
A department may have multiple employees.
1. Create Database Name is mapping and Two Table are employee1 and department.
Open NetBeans See The Menu Bar go to the file Menu Click on the New Project(Ctrl+Shift+N) and Select Left site categories java web and Project categories to Right Site Project web Application .You have to give The Program Name "OnetoManyMapping". if you wont to save Project Location you can Browse and File save on the direction place and Gone a default Place and Now uncheck/check the check box and click on the next button, select server settings and select the frameworks like Hibernate and go to the finish.
2. hibernate.cfg.xml
3. hibernate.reveng.xml
4. Employee1.java
5. Department.java
6. Employee1.hbm.xml
7. Department.hbm.xml
8. OnetoMany.java
OUTPUT:
1. Create Database Name is mapping and Two Table are employee1 and department.
Open NetBeans See The Menu Bar go to the file Menu Click on the New Project(Ctrl+Shift+N) and Select Left site categories java web and Project categories to Right Site Project web Application .You have to give The Program Name "OnetoManyMapping". if you wont to save Project Location you can Browse and File save on the direction place and Gone a default Place and Now uncheck/check the check box and click on the next button, select server settings and select the frameworks like Hibernate and go to the finish.
2. hibernate.cfg.xml
3. hibernate.reveng.xml
4. Employee1.java
5. Department.java
6. Employee1.hbm.xml
7. Department.hbm.xml
8. OnetoMany.java
OUTPUT:
One-To-One Hibernate Mapping
A one-to-one relationships occurs when one entity is related to exactly one occurrence in another entity. Thus, there will be one primary key which will be mapped with both the entities.
1. Create Database Name is mapping and Two Table are employee and employeedetail.
Open NetBeans See The Menu Bar go to the file Menu Click on the New Project(Ctrl+Shift+N) and Select Left site categories java web and Project categories to Right Site Project web Application .You have to give The Program Name "Mapping". if you wont to save Project Location you can Browse and File save on the direction place and Gone a default Place and Now uncheck/check the check box and click on the next button, select server settings and select the frameworks like Hibernate and go to the finish.
2. hibernate.cfg.xml
3. hibernate.reveng.xml
4. Employee.java
5. Employeedetail.java
6. Employee.hbm.xml
7. Employeedetail.hbm.xml
8. OnetoOnemapping.java
9. output-
1. Create Database Name is mapping and Two Table are employee and employeedetail.
Open NetBeans See The Menu Bar go to the file Menu Click on the New Project(Ctrl+Shift+N) and Select Left site categories java web and Project categories to Right Site Project web Application .You have to give The Program Name "Mapping". if you wont to save Project Location you can Browse and File save on the direction place and Gone a default Place and Now uncheck/check the check box and click on the next button, select server settings and select the frameworks like Hibernate and go to the finish.
2. hibernate.cfg.xml
3. hibernate.reveng.xml
4. Employee.java
5. Employeedetail.java
6. Employee.hbm.xml
7. Employeedetail.hbm.xml
8. OnetoOnemapping.java
Subscribe to:
Posts
(
Atom
)