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 6696681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:22:12+00:00 2026-05-26T06:22:12+00:00

What is the most elegant way to copy keys and values from one hashtable

  • 0

What is the most elegant way to copy keys and values from one hashtable to another between start and end keys in inverse order? For example original hashtable is:

[<1,"object1">; <2, "object2">; <4,"object3">; <5,"object4">;<7,"object5">;<8,"object6">]

after calling function getPartListOfNews(2,4) it should return hashtable like this:

[<7,"object5">;<5,"object4">;<4,"object3">]

I had made code to do it and it comes below, but I don’t think is this a better way to do what i had described before. Is there ara any better solutions? How can I simplify this code?

public Hashtable<Integer, News> getPartListOfNews(int start, int end){
        Hashtable <Integer, News> tempNewsList = new Hashtable <Integer, News>();
        int total_to_get = end-start;
        int list_size = newsList.size();
        Object[] key_array = new Object[list_size];
        if(list_size < total_to_get){
            return newsList;
        }
        else{
            Enumeration e = newsList.keys();
            int index=0;
            while(e.hasMoreElements()){
                key_array[index] = e.nextElement();
                index  ;
            }
            for (int i=end; i>start; i--){
                tempNewsList.put((Integer)key_array[i], newsList.get(key_array[i]));
            }
            return tempNewsList;
        }
    }

Update:

public Hashtable<Integer, News> newsList = new Hashtable<Integer, News>();

Thanks.

  • 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-26T06:22:12+00:00Added an answer on May 26, 2026 at 6:22 am

    First, you need to use a LinkedHashMap in your newsList attribute, to preserve insertion order. Also, it’s better if you declare attributes and return values of methods using the Map interface instead of the concrete class used, in this way you can easily change the implementation, like this:

    private Map<Integer, News> newsList = new LinkedHashMap<Integer, News>();
    

    With the above in mind, here’s my shot at solving your problem:

    public Map<Integer, News> getPartListOfNews(int start, int end) {
    
        // first, get the range of keys from the original map
    
        List<Integer> keys = new ArrayList<Integer>();
        for (Integer key : newsList.keySet()) // iterates in insertion order
            keys.add(key);
        List<Integer> subkeys = keys.subList(start, end);
    
        // now add them in the required order
    
        Map<Integer, News> tempNewsList = new LinkedHashMap<Integer, News>();
        ListIterator<Integer> iter = subkeys.listIterator();
        while (iter.hasPrevious()) {
            Integer key = iter.previous();
            tempNewsList.put(key, newsList.get(key));
        }
    
        return tempNewsList;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: most elegant way to return a string from List<int> I'm not sure
What would be the most elegant way to receive data from a streaming JSON
What is the most elegant way of getting the following: Starting from today's date,
What is the most elegant way to return a string from a List ok,
What is the most elegant way to put each line of text (from the
What's the most elegant way to return a std::list object from a shared lib
What is the most elegant way to solve this: open a file for reading,
What is the most elegant way of bubble-sorting in F#? UPDATE As pointed out
What would be the most elegant way to implement a Win32 equivalent of WaitForMultipleObjects
I'm looking for most elegant way to ajaxify my forms (with jQuery). How do

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.