Maintaining the Order of Inserting and Retrieving Java Objects Using Hashtable

We know that using Hashtable we can put and get the java objects relatively fast. But Hashtable does not maintain the order in which you add and retrieve the objects. Using a combination of hashtables we can accomplish this. With the following tip you can maintain the order.

  java.util.Hashtable hash = new java.util.Hashtable();for(int i = 0; i < 3 ; i++) {	Hashtable subHash = new Hashtable();String name = "name"+i;String value= "Foo"+i;	subHash.put(name, value);hash.put(new Integer(i), subHash);}

When you retrieve it, and if you want to retrieve the second item, which might be value associated with "name1" then you can have code:

 for(int i=0; i < hash.size(); i++){	Hashtable subHash = (Hashtable)hash.get(new Integer(i));	if(subHash != null){		String retValue =null;retValue = (String)subHash.get("name1")If(retValue != null)	return retValue;		}}

Also when we loop through the objects we can retrieve all the objects in the order in which they were inserted.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.