🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
size method and ListIterator.Table of Contents
- Introduction
- Using
sizeMethod - Using
ListIterator - Conclusion
Introduction
Lists in Java are part of the java.util package and can be manipulated using various methods provided by the List interface. It's important to handle cases where the list might be empty to avoid runtime errors.
Using size Method
The size method returns the number of elements in the list. You can use this method to get the index of the last element and then retrieve the element using the get method.
Example
import java.util.ArrayList;
import java.util.List;
public class LastElementExample {
public static void main(String[] args) {
List<String> list1 = new ArrayList<>();
list1.add("A");
list1.add("B");
list1.add("C");
List<String> list2 = new ArrayList<>();
System.out.println(getLastElement(list1)); // C
System.out.println(getLastElement(list2)); // null
}
public static <T> T getLastElement(List<T> list) {
if (list != null && !list.isEmpty()) {
return list.get(list.size() - 1);
}
return null;
}
}
Explanation
list.size() - 1: Calculates the index of the last element.list.get(list.size() - 1): Retrieves the element at the last index.- The method checks if the list is not
nulland not empty before attempting to get the last element to avoidIndexOutOfBoundsException.
Using ListIterator
The ListIterator can be used to iterate over the list. You can use it to traverse to the last element.
Example
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class LastElementExample {
public static void main(String[] args) {
List<String> list1 = new ArrayList<>();
list1.add("A");
list1.add("B");
list1.add("C");
List<String> list2 = new ArrayList<>();
System.out.println(getLastElementUsingIterator(list1)); // C
System.out.println(getLastElementUsingIterator(list2)); // null
}
public static <T> T getLastElementUsingIterator(List<T> list) {
if (list != null && !list.isEmpty()) {
ListIterator<T> iterator = list.listIterator(list.size());
return iterator.previous();
}
return null;
}
}
Explanation
list.listIterator(list.size()): Creates aListIteratorstarting at the end of the list.iterator.previous(): Moves the iterator backward and retrieves the last element.- The method checks if the list is not
nulland not empty before attempting to get the last element to avoidNoSuchElementException.
Conclusion
Getting the last element of a list in Java can be accomplished using various methods, including the size method and ListIterator. Each method has its own advantages and specific use cases:
- The
sizemethod is straightforward and commonly used. - Using
ListIteratoris another effective way, especially when dealing with custom traversals or more complex operations.
By understanding these methods, you can choose the most appropriate one for your specific use case when working with lists in Java.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment