-1

How to sort map by key string?

My map structure is like this

Map<String, List<Mobile>> mobiles = new HashMap<String, List<Mobile>>();

mobiles.put("Samsung", ListOfMobileModels);
mobiles.put("Nokia", ListOfMobileModels);
mobiles.put("Apple", ListOfMobileModels);
mobiles.put("Blackberrey", ListOfMobileModels);

/** Sort Like below**/

mobiles.put("Apple", ListOfMobileModels);
mobiles.put("Blackberrey", ListOfMobileModels);
mobiles.put("Samsung", ListOfMobileModels);
mobiles.put("Nokia", ListOfMobileModels);

I have mobile rank list in another Map like Apple-1,Blackberry-2,Samsung-3,Nokia-4

How to sort this map using stream or any Comparator?

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
Angular Dev
  • 97
  • 2
  • 13
  • HashMaps don't have any order, sorting doesn't make sense without order. – Minn Aug 10 '19 at 13:43
  • 1
    You could use another type of map. To see the different types of Map, have a look at https://stackoverflow.com/questions/2889777/difference-between-hashmap-linkedhashmap-and-treemap – alea Aug 10 '19 at 13:55
  • Just use `TreeMap` – Ryuzaki L Aug 10 '19 at 15:37
  • And you should follow the Java Naming Conventions: variable names are always written in camelCase, that means they start with lowercase. So `ListOfMobileModels` should be `listOfMobileModels`. – MC Emperor Aug 10 '19 at 16:35
  • @Deadpool you are totally right, TreeMap is the key here – developer_hatch Aug 11 '19 at 17:26
  • i am getting this map from Webservice response i have to process the response and order the map based on my rank list. I am not create any map so i am not able to put the key values in TreeMap – Angular Dev Aug 11 '19 at 18:18

1 Answers1

0

Take a look at this example:

SortedMap<String, String> fileExtensions  = new TreeMap<>();

        // Adding new key-value pairs to a TreeMap
        fileExtensions.put("python", ".py");
        fileExtensions.put("c++", ".cpp");
        fileExtensions.put("kotlin", ".kt");
        fileExtensions.put("golang", ".go");
        fileExtensions.put("java", ".java");

        // Printing the TreeMap (Output will be sorted based on keys)
        System.out.println(fileExtensions);
    }

It seems like it is what you are looking for. Source

Based on that, you can create an Enum and a class Comparator:

import java.util.*;

public class Main2 {
    public static void main(String[] args) {
        SortedMap<RankComparator,List<Mobile>> mobiles = new TreeMap<>();

        List<Mobile> mobiles1 = new ArrayList<>();

        mobiles.put(new RankComparator(Rank.Samsung), mobiles1);
        mobiles.put(new RankComparator(Rank.Nokia), mobiles1);
        mobiles.put(new RankComparator(Rank.Apple), mobiles1);
        mobiles.put(new RankComparator(Rank.Blackberry), mobiles1);

        ((TreeMap<RankComparator, List<Mobile>>) mobiles).entrySet().forEach(e -> System.out.println(e.getKey().rank));
    }
}

class Mobile{
    Rank rank;

    public Mobile(Rank rank) {
        this.rank = rank;
    }
}

enum Rank{
    Apple(1),
    Blackberry(2),
    Samsung(3),
    Nokia(4);

    int rank;
    Rank(int i) {
        this.rank = i;
    }
}

class RankComparator implements Comparable<RankComparator>{

    Rank rank;

    public RankComparator(Rank rank) {
        this.rank = rank;
    }

    @Override
    public int compareTo(RankComparator otherRank) {
        return this.rank.rank - otherRank.rank.rank;
    }
}

output ->

Apple
Blackberry
Samsung
Nokia
developer_hatch
  • 15,898
  • 3
  • 42
  • 75