Sort a list based on another list

I currently have an ordered list of String ids ( List<String>) and an unordered list of custom class ( List<CustomClass>). I would like to order a list of custom class objects based on an ordered IDS list.

I got the impression that the best way to do this is to use TreeMap. So I implemented this:

Map<String, CustomClass> mapB = new HashMap<String, CustomClass>();
    for (String id : mIds) {
        for (CustomClass customClass : mCustomClass) {                
            mapB.put(thingId, mCustomClass);
        }
    }

Map<String, CustomClass> treeMap = new TreeMap<String, CustomClass>();
treeMap.putAll(mapB);

Although, it keeps all identifiers in order, but when I print TreeMap, it seems that it takes only the last mapB value and saves it. That is an example of logs:

Map: 1: Paris, Map: 2: Paris, Map: 3: Paris

But I added:

mapB.put("1", London);
mapB.put("2", Berlin);
mapB.put("3", Paris);

So yes, I'm a little confused about what is happening, can anyone give some kind of guidance? Thank!

+4
1

fors. , : 1 - 1 - 1 - 2 - 2 - 2 - 3 - 3 - 3 -

TreeMap , , - .

, mIds mCustomClass ( ), mapB.put(mIds [i], mCustomClass [i]).

, ( ), , 2 (id ) , .

+3

All Articles