Naveen Suman
Naveen Suman

Reputation: 147

Creation of a map from another map in Java

I have to convert a Map- Map1 into Map2.

Map1:

A.B.Key    = "Key"
A.B.Value  = "Value"
A.B.Key1   = "Key1"
A.B.Value1 = "Value1"
A.B.Key2   = "Key2"
A.B.Value2 = "Value2"

Map2:

Key  = Value
Key1 = Value1
Key2 = Value2

Map1 is created from this XML file:

XML File:

<A>
  <B>
    <Key> Key </Key>
    <Value> Value </Value>
  </B>
  <B>
    <Key> Key1 </Key>
    <Value> Value1 </Value>
  </B>
  <B>
    <Key> Key2 </Key>
    <Value> Value2 </Value2>
  </B>
</A>

I am getting the problem because Map1 contents are not in the order, in which Its mentioned in the XML file. Map1 is something like this:

Actual Map1:

A.B.Key2   = "Key2"
A.B.Key    = "Key"
A.B.Value  = "Value"
A.B.Value1 = "Value1"
A.B.Value3 = "Value2"
A.B.Key1   = "Key1"

Where am I going wrong?

Upvotes: 1

Views: 107

Answers (1)

Ulrich von Poblotzki
Ulrich von Poblotzki

Reputation: 456

In the Java Collection API there are "Linked..." implementations of the basic interfaces, which are ordered according the element insertion. You could use the LinkedHashMap for your propose.

Upvotes: 2

Related Questions