Asif Billa
Asif Billa

Reputation: 1071

How to insert a entry in last position of a TreeMap

I have a list of entries to select from a drodown, like A, B, C, D, E etc. So I have used a tree map to display the same i sorted order. But in the last entry I want a 'Add New Record' option value which will be used to create a new option. If User selects 'Add New Record' then a new text box will appear and new entry can be made.

Now the problem is I want the option 'Add New Record' as last option in the dropdown. But as the treemap is sorted the value in the dropdown is getting displayed as A,Add New Record, B, C, D, E etc. How can I Put the 'Add New Record' at the last option value in Java.

Please suggest.

Upvotes: 1

Views: 756

Answers (3)

Bohemian
Bohemian

Reputation: 425438

Don't use a TreeMap.

Use a LinkedHashMap, which iterates over its entries in insertion order; simply insert the entries in the order you want them.

Upvotes: 2

YoungHobbit
YoungHobbit

Reputation: 13402

If you are doing this in java, (as you have put the java tag). You can you use the ArrayList. It maintain the insertion order of the elements.

public class ListExample {
  public static void main(String[] args) {
    List<String> myList = new ArrayList<String>();
    myList.add("one");
    myList.add("two");
    myList.add("three");
    myList.add("four");
    myList.add("five");

    for (String string : myList) {
      System.out.println(string);
    }
  }
}

Output:

one
two
three
four
five

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122026

Most easy solution is do not add "Add a new record" into the TreeMap.

Add that value to your widget after the Treemap items added to the widget, what so ever technology you are using in UI.

Upvotes: 0

Related Questions