Frage

Hello i have 2 diffrent arraylist of which both contain data in hashmap format (key value pairs). Now i need to combine those 2 list's into a single one while maintaining the order from both list ( so the reference elements from both list at position[0] should stay one to another in the new list). I need this to show those information in a listview but so far i only managed to merge the two but the order wasn't right (the first list would take the first half of the new one while the references from the second one would come after). Hope someone can help me :)

War es hilfreich?

Lösung

Here you have a method (it can be refactor to reduce code)

import java.util.ArrayList;
import java.util.HashMap;


public class Test {


    public static void main (String[] args){

        final ArrayList<HashMap<String, Integer>> listOne = new ArrayList<HashMap<String, Integer>>();
        final ArrayList<HashMap<String, Integer>> listTwo = new ArrayList<HashMap<String, Integer>>();

        // Test data for listOne
        for (int i = 0; i< 10; i++) {
            final HashMap<String, Integer> map = new HashMap<String, Integer>();

            map.put(String.valueOf(i), i);

            listOne.add(map);
        }

        // Test data for listTwo
        for (int i = 0; i< 20; i++) {
            final HashMap<String, Integer> map = new HashMap<String, Integer>();

            map.put(String.valueOf(i), i);

            listTwo.add(map);
        }

        // Determine the lower size
        final ArrayList<HashMap<String, Integer>> finalList = new ArrayList<HashMap<String, Integer>>();
        final int minSize = (listOne.size() <= listTwo.size()) ? listOne.size() : listTwo.size();

        // Iterate the two list with the lower size
        for (int i = 0; i<minSize; i++) {
            finalList.add(listOne.get(i));
            finalList.add(listTwo.get(i));
        }

        // Add the remanent for the bigger list if needed
        if (listOne.size() > minSize) {

            for (int i = minSize; i<listOne.size(); i++) {
                finalList.add(listOne.get(i));
            }

        } else if (listTwo.size() > minSize) {

            for (int i = minSize; i<listTwo.size(); i++) {
                finalList.add(listTwo.get(i));
            }
        }

        System.out.println(finalList);

    }
}

Andere Tipps

Try this I think this may solve your issue

   list2.add(list1);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top