Friday 15 May 2015

sorting - Java data structure sorted by key -


I am trying to store the integer in the form of a key in the form of a string, for example What is stored:

  key: "name1", int: 123 key: "name2", int: 124   

I want to be able to insert so that they were ordered alphabetically for easy printing purposes on the screen.

is being used so far:

  dictionary & lt; String, integer & gt; X = new hashtable & lt; String, integer & gt; (); X.put ("NAME1", 123); X.put ("NAME2", 124); Calculation & LT; String & gt; E String key; For (e = x.keys (); e.hasMoreElements ();) {key = e.nextElement (); System.out.println (key + ":" + x.get (key)); }   

This output:

  name 2: 124 name1: 123   

Why not in alphabetical order Are you Is there a substitute for a dictionary that I should know?

Unless otherwise stated, map / hashpps / hashtables / dictionaries / etc. No defined sort order should work for your purposes; It implements the interface. He said, I do not necessarily think that you should choose a data structure because it makes printing easy.


Side note: dictionary and hashtable are legacy classes that are primarily present for rear compatibility. The new code should generally give priority to general-purpose key-value data structures.

No comments:

Post a Comment