Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6352635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:15:54+00:00 2026-05-24T22:15:54+00:00

I have use java stack data structure to maintain data. I have limit my

  • 0

I have use java stack data structure to maintain data. I have limit my stack to be size 50. What I want to do get latest 5 data from the stack from one call. I thought method sublist(0,5) would do it. But unfortunately method returns last 5 data since that method inherited from java.util.list is there any way to do this. Or is there any other data structure which fulfill my requirment. pls help me. Thanks in advance.

P.s I want to remain lates 5 data in the stack after i retrive.

 Stack stack = new Stack();
        for(int i=0; i<10;i++){
            stack.push(""+i);
        }
        for(int k= 0 ;k<11;k++){
            System.out.println(stack.subList(0,5));
        }

out put of this will be {0,1,2,3,4}. But I want to get {9,8,7,6,5}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:15:54+00:00Added an answer on May 24, 2026 at 10:15 pm

    If sublist(0,5) returns the wrong end of your stack, try changing your indices. Instead of starting from 0, start from stack.size()-6.

    As others pointed out you can pop() 5 times as well. In functional languages the operation you are looking for simply referred to as take 5. It is easy to implement it yourself, but you’d have to derive your own stack class in Java.

    Another important point: When you pop() 5 times, those elements will be removed from the stack. This may or may not be what you intend. However, keep in mind, that sublist will return you a list that may modify the stack itself, so you can easily run into integrity problems there.

    Hence, I suggest you write your own stack class and implement a take(n) method that performs a sublist call and copies the results into a fresh list, such that the ultimately returned list cannot affect your original stack.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl module that I would like to use from Java. Is
I'm converting an application to use Java 1.5 and have found the following method:
I've just started using Java's enums in my own projects (I have to use
I have 2 separate java services that use a complex type that is a
I have a Java string of XML content. I use Velocity to generate some
What java GUI layout manager does everyone use? Lately, I have been using MigLayout
I have use IlMerge to merge all the dlls of my projects in one
Have a use case wherein need to maintain a connection open to a database
I have the following problem in my Data Structures and Problem Solving using Java
In a Java application stack with Spring & Hibernate (JPA) in the Data Access

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.