Java 9 Objects Class additional methods null pointer check index and length

Опубликовано: 26 Май 2024
на канале: CodeWis Technologies by Nuhman Paramban
24
0

This Java code demonstrates the usage of new methods introduced in Java 9 in the Objects class. The class java9Additionals contains a method objectsClassModificationsInJava9 that showcases the functionality of these methods. Here's a detailed explanation:

Package and Imports
java
Copy code
package com.nuhman.coding.test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
The package declaration indicates that this class is part of the com.nuhman.coding.test package.
The java.util package is imported to use Arrays, Collections, List, and Objects classes.
Class Definition
java
Copy code
public class java9Additionals {
The class java9Additionals is declared as public, meaning it can be accessed from outside its package.
Method: objectsClassModificationsInJava9
java
Copy code
public static void objectsClassModificationsInJava9 (List<String> stringList) {
This method is declared as public static, meaning it can be called without creating an instance of the class. It takes a single parameter, stringList, which is a List of String objects.
Using requireNonNullElse
java
Copy code
// The new methods in Java 9
// 1 . requireNonNullElse method
List<String> strList = Objects.requireNonNullElse(stringList, Collections.EMPTY_LIST);
System.out.println(strList);
Objects.requireNonNullElse(stringList, Collections.EMPTY_LIST) returns stringList if it is non-null; otherwise, it returns Collections.EMPTY_LIST (an empty list). This method provides a simpler alternative to manually checking for null.
Using requireNonNullElseGet
java
Copy code
// 2 . requireNonNullElseGet method
strList = Objects.requireNonNullElseGet(stringList, List::of);
System.out.println(strList);
Objects.requireNonNullElseGet(stringList, List::of) returns stringList if it is non-null; otherwise, it returns a new empty immutable list created by List::of.
Using checkIndex
java
Copy code
// 3 . checkIndex method
int checkIndex = Objects.checkIndex(strList.size(), 1);
System.out.println(checkIndex);
// replaces if(strList.size()==1) throws exception
Objects.checkIndex(strList.size(), 1) checks if the index 1 is within the bounds of strList. It returns the index if valid; otherwise, it throws IndexOutOfBoundsException.
Using checkFromToIndex
java
Copy code
// 4 . checkFromToIndex method
int startIndex = 0;
int endIndex = 1;
checkIndex = Objects.checkFromToIndex(startIndex, endIndex, 1);
System.out.println(checkIndex);
Objects.checkFromToIndex(startIndex, endIndex, 1) checks if the range from startIndex to endIndex is within the bounds of a list of size 1. It returns the startIndex if valid; otherwise, it throws IndexOutOfBoundsException.
Using checkFromIndexSize
java
Copy code
// 5 . checkFromIndexSize method
startIndex = 2;
endIndex = 4;
checkIndex = Objects.checkFromIndexSize(startIndex, endIndex, 6);
System.out.println(checkIndex);
Objects.checkFromIndexSize(startIndex, endIndex, 6) checks if the sublist starting at startIndex with endIndex length is within the bounds of a list of size 6. It returns the startIndex if valid; otherwise, it throws IndexOutOfBoundsException.
Main Method
java
Copy code
public static void main(String[] args) {
objectsClassModificationsInJava9(null);
objectsClassModificationsInJava9(Arrays.asList("str", "str2"));
}
The main method tests objectsClassModificationsInJava9 with two scenarios:
Passing null to the method.
Passing a list with two strings: "str" and "str2".
In summary, this code demonstrates the new Objects methods introduced in Java 9, which provide more concise and readable ways to handle common tasks like null-checking and index validation.


Смотрите видео Java 9 Objects Class additional methods null pointer check index and length онлайн, длительностью часов минут секунд в хорошем качестве, которое загружено на канал CodeWis Technologies by Nuhman Paramban 26 Май 2024. Делитесь ссылкой на видео в социальных сетях, чтобы ваши подписчики и друзья так же посмотрели это видео. Данный видеоклип посмотрели 24 раз и оно понравилось 0 посетителям.