Benutzerspezifische Werkzeuge
Sie sind hier: Startseite / Programmierung / Java / Sortieren einer ArrayList-HashMap

Sortieren einer ArrayList-HashMap

Hier eine Lösung für eine ArrayList-HashMap Sortierung

Folgende Liste wollen wir sortieren:

ArrayList<HashMap<String, String>> arrayListHashMap = new ArrayList<HashMap<String, String>>();
{
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("name", "Klaus");
hashMap.put("age", "21");
hashMap.put("car", "Audi");
arrayListHashMap.add(hashMap);
}
{
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("name", "Peter");
hashMap.put("age", "44");
hashMap.put("car", "Opel");
arrayListHashMap.add(hashMap);
}
{
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("name", "Axel");
hashMap.put("age", "33");
hashMap.put("car", "VW");
arrayListHashMap.add(hashMap);
}
System.out.println(arrayListHashMap);

Ausgabe: [{car=Audi, age=21, name=Klaus}, {car=Opel, age=44, name=Peter}, {car=VW, age=33, name=Axel}]

Für die Sortierung nutzten wir folgende Funktion:

public static class MapComparator implements Comparator<Map<String, String>> {
    private final String key;
    public MapComparator(String key) {
        this.key = key;
    }
    public int compare(Map<String, String> first, Map<String, String> second) {
        String firstValue = first.get(key);
        String secondValue = second.get(key);
        return firstValue.compareTo(secondValue);
    }
}

Und so wird die Funktion dann genutzt:

Collections.sort(arrayListHashMap, new MapComparator("name"));
System.out.println(arrayListHashMap);

Ausgabe: [{car=VW, age=33, name=Axel}, {car=Audi, age=21, name=Klaus}, {car=Opel, age=44, name=Peter}]

 

Wer jetzt die Ausgabe noch in umgekehrter Reihenfolge haben möchte, taucht einfach die Variablen der Rückgabe:

return secondValue.compareTo(firstValue);

 

 

 

 

Moncler Jackets Canada 1 sagt
27.02.2018 17:08

I not that much of a internet reader to be honest but your blogs really nice, keep it up! I'll go ahead and bookmark your site to come back down the road. Cheers
Moncler Jackets Canada 1 https://www.instagrameeeeeee323.com

Parajumpers Jacket sagt
07.03.2018 03:04

F*ckin?amazing things here. I very glad to see your post. Thanks a lot and i'm looking forward to contact you. Will you please drop me a mail?
Parajumpers Jacket http://www.parajumpersjacket.us.com

Fitflop Clearance sagt
09.03.2018 05:37

Ishould check with you here. Which is not something I often do! I get pleasure from studying a publish that will make people think. Additionally, thanks for allowing me to comment!
Fitflop Clearance http://www.fitflopclearancesale.net

Most Popular Moncler Jacket sagt
10.03.2018 23:11

Thank you for the sensible critique. Me & my neighbor were just preparing to do some research on this. We got a grab a book from our local library but I think I learned more from this post. I am very glad to see such wonderful information being shared freely out there.
Most Popular Moncler Jacket https://www.pinterest.com/mostpopularmonclerjacket

Moncler Coat sagt
22.03.2018 07:02

Itreally a cool and useful piece of information. I am glad that you shared this useful info with us. Please keep us up to date like this. Thank you for sharing.
Moncler Coat https://www.pinterest.com/monclercoat0638

Adidas Nmd sagt
25.03.2018 02:26

WONDERFUL Post.thanks for share..extra wait .. ?
Adidas Nmd https://adidasnmd554.wordpress.com

Oakley Eyeglasses Online sagt
26.03.2018 06:36

With havin so much content and articles do you ever run into any problems of plagorism or copyright infringement? My website has a lot of exclusive content I've either created myself or outsourced but it seems a lot of it is popping it up all over the internet without my permission. Do you know any techniques to help protect against content from being stolen? I'd genuinely appreciate it.
Oakley Eyeglasses Online https://oakleyeyeglassesonline892.wordpress.com

Parajumpers Outwear sagt
27.03.2018 10:14

When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove people from that service? Thanks a lot!
Parajumpers Outwear http://www.pjsparajumpersonlineshop.com

Fitflop Shoes Women sagt
02.04.2018 05:10

Valuable information. Lucky me I found your website by accident, and I'm shocked why this twist of fate did not took place in advance! I bookmarked it.
Fitflop Shoes Women http://www.fitflopshoeswomen.com

Kate Spade Bags sagt
19.04.2018 11:02

Thanks for some other magnificent article. The place else may just anyone get that kind of information in such a perfect method of writing? I have a presentation next week, and I'm at the look for such info.
Kate Spade Bags http://www.kate-spadehandbags.com

Kommentieren

Sie können einen Kommentar abgeben, indem Sie das untenstehende Formular ausfüllen. Nur Text.