0

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

4

2 回答 2

3

If all you want is that, at the end of that code snippet, both auditMap and oldValues should be in same order, just run them through Collections.sort() method with appropriate comparator for the Field objects.

If not, then create auditMapArray, a Field Array of size as that of oldValues (you certainly need to declare this outside IF block), after the IF block. Everytime you want to insert an element into auditMap, find its index in oldValues (assuming you have proper equals method implementation to check if two Field objects are equal) and insert at the same location in auditMapArray too, and finally get the auditMap list using Arrays.asList(auditMapArray).

Modified version of your code snippet that ensures that all the values in oldValues that are present in auditMap are in the same order. Any additional elements into auditMap are appended at the end.

Map<Long, String> attfieldMap = null;
attfieldMap = view.getNameMap(form);

List<Field> oldValues = new ArrayList<Field>();
if (itemId != 0) {

    Item dom = getRecord();

    Map<FormField, Object> tempValues = dom.getValues();

    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);
        }
    }
}

List<Field> otherFields = new ArrayList<Field>();
Field [] auditMapArray = new Field[oldValue.size()];
int index;
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()));

    index = oldValues.indexOf(attributeFld);
    if (index > 0) {
        auditMapArray[index] = attributeFld;
    }
    else
    {
        System.err.println(attributeFld + " not found in oldValues");
        otherFields.add(attributeFld);
    }
}

List<Field> auditMap = Arrays.asList(auditMapArray);
auditMap.addAll(otherFields);
于 2012-09-01T13:23:27.770 回答
3

You could sort the second list using a custom comparator, using the items' indices in the first list as key. Something like this:

Collections.sort(list2, new Comparator() {
    public int compare(Object o1, Object o2) {
        return list1.indexOf(o1) - list1.indexOf(o2);
     }});

Update: Since indexOf could take some time if the list is unsorted, and since this method would be called very often, it may be better to first store the indices of the objects in a Map, i.e. create a HashMap {item: list1.indexOf(item)} and use this map in the compare method.

Putting it together, and using the names from your example:

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;
     }});
于 2012-09-01T13:43:58.790 回答