UTIL PACKAGE / CATATAN</>↗Util Package27 Apr 2025
In Java, you can use the Optional API to filter values based on a condition using the filter method. The filter method takes a predicate as an argument and applies it to the value contained in the Optional. If the predicate evaluates to true, the Optional is returned unchanged. If the predicate evaluates to false, […]
UTIL PACKAGE / CATATAN</>↗Util Package27 Apr 2025
Using Optional in Java is a great way to refactor nested null checks into more readable and maintainable code. Below, I’ll explain how you can use Optional to replace deeply nested null checks step by step with examples. Example of Nested Null Checks Consider this code with deeply nested null checks: String streetName = null; […]
JSCH / CATATAN</>↗JSch26 Apr 2025
When using JSch (Java Secure Channel) to establish SSH connections in your Java application, it’s essential to properly manage resources like Session and Channel objects to prevent resource leaks. Cleanup involves explicitly closing all channels and disconnecting the session once the work is complete. Here is how you can handle session and channel cleanup in […]
UTIL PACKAGE / CATATAN</>↗Util Package26 Apr 2025
In Java, the Optional class provides methods for handling values that may or may not be present. The methods ifPresent and ifPresentOrElse are particularly useful for executing code conditionally based on whether a value is present in the Optional. 1. Using ifPresent The ifPresent method executes a Consumer when the value is present (i.e., it’s […]
UTIL PACKAGE / CATATAN</>↗Util Package26 Apr 2025
In Java, you can throw exceptions when an Optional is empty using the orElseThrow method. This method accepts a Supplier that provides the exception to be thrown if the Optional is empty. Here’s the syntax and an example: package org.kodejava.util; import java.util.Optional; public class Main { public static void main(String[] args) { Optional<String> optionalValue = […]
JSCH / CATATAN</>↗JSch25 Apr 2025
To execute a simple command over SSH using JSch (Java Secure Channel), you can use the ChannelExec channel provided by the JSch library. Here’s how you can achieve that: Maven Dependency: If you’re using Maven, include the JSch library in your pom.xml file: <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> <!– Use the latest version –> </dependency> Code […]