Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
LinkedList descendingIterator() Method in Java
Next article icon

LinkedList descendingIterator() Method in Java

Last Updated : 17 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the descendingIterator() method of LinkedList is used to return an iterator that allows to traverse the list in reverse order.

Example 1: This example demonstrates how to use descendingIterator() method with Strings in a LinkedList.

Java
// Java program to use  // descendingIterator()  import java.util.Iterator; import java.util.LinkedList;  public class Geeks {     public static void main(String[] args) {                // Creating a LinkedList         LinkedList<String> l = new LinkedList<>();          // Adding elements to the LinkedList         l.add("Java");         l.add("Python");         l.add("C++");          // Display the original LinkedList         System.out.println("Original list: " + l);          // Using descendingIterator to          // iterate the list in reverse order         Iterator<String> it = l.descendingIterator();          // Iterating through the list in reverse order         System.out.print("Reverse Order list: ");         while (it.hasNext()) {             System.out.print(it.next() + " ");         }     } } 

Output
Original list: [Java, Python, C++] Reverse Order list: C++ Python Java 

Syntax of LinkedList descendingIterator() Method

Iterator<E> descendingIterator();

Return type: It returns an Iterator that iterates over the elements in the reverse order.

Example 2: This example demonstrates how to use descendingIterator() method with Integers in a LinkedList.

Java
// Use of descendingIterator() with Integers import java.util.Iterator; import java.util.LinkedList;  class Geeks {     public static void main(String[] args) {                // Creating an empty LinkedList         LinkedList<Integer> l = new LinkedList<>();         l.add(100);         l.add(200);         l.add(300);         l.add(400);         l.add(500);          // Displaying the original LinkedList         System.out.println("Original list: "                            + l);          // Getting the descending iterator         Iterator<Integer> it             = l.descendingIterator();          // Iterating in reverse order using the         // descendingIterator() method         System.out.print("Iterating in reverse order: ");         while (it.hasNext()) {             System.out.print(it.next() + " ");         }     } } 

Output
Original list: [100, 200, 300, 400, 500] Iterating in reverse order: 500 400 300 200 100 


Example 3: This example demonstrates how to use descendingIterator() method with Objects in a LinkedList.

Java
// Use of descendingIterator() with Object  import java.util.Iterator; import java.util.LinkedList;  class Human {     int a;     String n;      // Created a Constructor     public Human(int a, String n)     {         this.a = a;         this.n = n;     }      @Override public String toString()     {         return "age=" + a + ", name=" + n;     } }  public class Geeks {     public static void main(String[] args) {          // Created a Linkedlist to store Human object         LinkedList<Human> l = new LinkedList<>();          // Adding human object to the LinkedList         l.add(new Human(10, "Bob"));         l.add(new Human(28, "Alice"));         l.add(new Human(23, "Robin"));         l.add(new Human(18, "Scott"));         l.add(new Human(34, "David"));          // Getting the descending Iterator         Iterator<Human> it = l.descendingIterator();         System.out.println("Iterating in reverse order:");          // Iterating in reverse order using the         // descendingIterator() method         while (it.hasNext()) {                        System.out.println(it.next());         }     } } 

Output
Iterating in reverse order: age=34, name=David age=18, name=Scott age=23, name=Robin age=28, name=Alice age=10, name=Bob 

Next Article
LinkedList descendingIterator() Method in Java

R

rohitprasad3
Improve
Article Tags :
  • Java
  • Java-Collections
  • Java - util package
  • Java-Functions
  • java-LinkedList
Practice Tags :
  • Java
  • Java-Collections

Similar Reads

    LinkedList in Java
    Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure, which is a linear data structure where the elements are not stored in contiguous locations, and every element is a separate object with a data part and an
    12 min read
    LinkedList add() Method in Java
    In Java, the add() method of the LinkedList class is used to add an element to the list. By default, it adds the element to the end of the list, if the index is not specified.Example: Here, we use the add() method to add a single element to the list.Java// Java program to add elements in LinkedList
    2 min read
    LinkedList set() Method in Java
    The Java.util.LinkedList.set() method is used to replace any particular element in the linked list created using the LinkedList class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() method. Syntax: Link
    3 min read
    LinkedList remove() Method in Java
    In Java, the remove() method of the LinkedList class removes an element from the list, either by specifying its index or by providing its value.Example 1: Here, we use the remove() method to remove element from the LinkedList of Strings. By default the remove() will remove the beginning element(head
    3 min read
    LinkedList get() Method in Java
    In Java, the get() method of LinkedList is used to fetch or retrieve an element at a specific index from a LinkedList.Example 1: Here, we use the get() method to retrieve an element at a specified index.Java// Java Program to illustrate get() method import java.util.LinkedList; public class Geeks {
    2 min read
    LinkedList addAll() Method in Java
    In Java, the addAll() method of the LinkedList class is used to add all the elements of one collection to another. This method takes a Collection as an argument and adds all its elements to the end of the list. Example: Here, we use the addAll() method to add all the elements of one collection to an
    3 min read
    LinkedList addFirst() Method in Java
    In Java, the addFirst() method of LinkedList, adds elements at the beginning of the list. All the existing elements moved one position to the right and the new element is placed at the beginning.Syntax of LinkedList addFirst() Method public void addFirst( E e)Parameters: E is the data type of elemen
    1 min read
    LinkedList addLast() Method in Java
    In Java, the addLast() method of the LinkedList class is used to add an element at the end of the list.Syntax of LinkedList addLast() Method void addLast( E e)Parameter: e is the element you want to add at the end of the list.Return type: This method does not return any value.Example: Here, we use t
    1 min read
    LinkedList clear() Method in Java
    In Java, the clear() is used to remove all the elements from a LinkedList. This method only clears all the element from the list and not deletes the list. After calling this method, the list will be empty.Syntax of LinkedList clear() Methodvoid clear()Parameters: This method does not accept any para
    1 min read
    LinkedList clone() Method in Java
    In Java, the clone() method of LinkedList, creates a shallow copy which means the structure is duplicated but the objects inside the list are shared between the original and the copied list. So, the cloned list will have the same elements as the original list.Syntax of Java LinkedList clone() Method
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences