public class Employee {
private int employeeId;
private String firstName;
private String lastName;
private double salary;
public Employee(int employeeId, String firstName, String lastName, double salary) {
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
Say you have an Object of class Employee and you want to convert it into a type Optional, then follow the below code.
import java.util.Optional;
public class ObjectToOptionalExample {
public static void main(String[] args) {
Employee employee = new Employee(1,"Sam","Page",200000);
Optional<Employee> optionalEmployee = Optional.ofNullable(employee);
//Checks
if (optionalEmployee.isPresent()) {
Employee resultEmployee = optionalEmployee.get();
System.out.println("Hello " + employee.getLastName());
} else {
System.out.println("Employee object is null!");
}
}
}
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- CRUD operations in Spring Boot + JDBC
- Java Check Leap Year - Programs with Code Examples
- [fix] Java JDBC ConnectException: Connection refused
- How to add hours and minutes to Java Instant
- Java Program: Random Number Generator
- Java: The value of the local variable string is not used
- How to get list of all Java versions installed on macOS
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Java + Spring JDBC Template + Gradle Example
- Convert String to LocalDate in Java
- Remove Trailing zeros BigDecimal Java
- Java 8 Predicate Functional Interface isEqual() Method Example
- How to Hardcode Date in Java with Examples
- Java 8: Predicate negate() default Function Example
- Java: Collect Stream as ArrayList or LinkedList
- The Motivation Behind Generics in Java Programming
- How to Add/Subtract Days to the Current Date in Java
- Error: Can not find the tag library descriptor for
- Setting up JUnit 5 dependency with Maven Example
- Run Java Code Every Second
- How to create a tar.gz file using Java
- [Fix] java: integer number too large compilation error
- Java 8: Find the Max value in a List
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- Convert Java Array to ArrayList Code Example
More Posts:
- Fix mySQL Error Cant connect to local MySQL server through socket /var/run/mysqld/mysqld.sock ERROR 2002 HY000 - MySQL
- Is Roblox available on the Mac App Store? - MacOS
- Fix: Error code: 0x204 Microsoft Remote Desktop - Unable to connect the remote PC. - Windows
- Add Emojis and GIFs using Windows 10 or 11 PC Keyboard - Windows-11
- Find MAC address of your laptop device - HowTos
- How to delete SharePoint Online List Item using REST API - SharePoint
- How to set CSS background-Image Property - CSS
- How to hide or disable iOS 14 App Library on iPhone? - Apple