I have two lists where the order of one list is different from the order of other list, in the list I am adding a map where key order is different in both lists. I want the order of both the list same. How can I perform this?
Map<Long, String> attfieldMap = null;
attfieldMap = view.getNameMap(form);
List<Field> auditMap = new ArrayList<Field>();
if (itemId != 0) {
Item dom = getRecord();
Map<FormField, Object> tempValues = dom.getValues();
List<Field> oldValues = new ArrayList<Field>();
for (Map.Entry<FormField, Object> values : tempValues
.entrySet()) {
Field oldFld = new Field();
Form form = (Form) values.getKey();
if (attfieldMap.get(form.getField().getId()) != null) {
oldFld.setKey(attfieldMap.get(form.getField()
.getId()));
oldFld.setValue(values.getValue());
oldValues.add(oldFld);
}
}
}
for (Map.Entry<Long, String> attfieldEntry : attfieldMap.entrySet()) {
// Mandates to declare within loop
Field attributeFld = new Field;
attributeFld.setKey(attfieldEntry.getValue());
attributeFld.setValue(String.valueOf(attfieldEntry.getKey()));
auditMap.add(attributeFld);
}
The list auditMap
has map with key att13
, att12
, att14
and oldValues
has map with key att12
, att13
, att14
. I need this list to be in same order as auditMap
list. How to order this?
As per the answeres the below code edited :Here the data gets copied,iwant just the sort,even though the list size is different.list1 should have its own data,list2 should have same data,but the order should be maintained
List<Field> auditMap = new ArrayList<Field>();
attfieldMap = View
.getFieldID(form);
for (Map.Entry<Long, String> attfieldEntry : attfieldMap.entrySet()) {
Field attributeFld = new Field();
attributeFld.setKey(attfieldEntry.getValue());
attributeFld.setValue(String.valueOf(attfieldEntry.getKey()));
auditMap.add(attributeFld);
attributeFld = null;
}
if (itemId != 0) {
Item dom = getRecord(domainItem);
Map<FormField, Object> tempValues = dom.getValues();
List<Field> oldValues = new ArrayList<Field>();
for (Map.Entry<FormField, Object> values : tempValues
.entrySet()) {
Field oldFld = new Field();
Form form = (Form) values.getKey();
if (attfieldMap.get(form.getField().getId()) != null) {
oldFld.setKey(attfieldMap.get(form.getField()
.getId()));
oldFld.setValue(values.getValue());
oldValues.add(oldFld);
}
}
final Map<Field, Integer> indices = new HashMap<Field, Integer>();
for (int i = 0; i < oldValues.size(); i++) {
indices.put(oldValues.get(i), i);
}
Collections.sort(auditMap, new Comparator<Field>() {
public int compare(Field o1, Field o2) {
int index1 = indices.containsKey(o1) ? indices.get(o1)
: -1;
int index2 = indices.containsKey(o2) ? indices.get(o2)
: -1;
return index1 - index2;
}
});
for (int i = 0; i < oldValues.size(); i++) {
LOGGER.info("the new data is--" + oldValues.get(i).getKey());
}
for (int i = 0; i < auditMap.size(); i++) {
LOGGER.info("the new data is--" + auditMap.get(i).getKey());
}
This is my actual code,the data is not getting ordered.The keys in auditMap are 1,2,3,4 and in old values are 1,3,2 the result of old values should be 1,2,3 but its not happening