how to sort Map<String, Map>, based on one of the inside map key value in java [duplicate]












-3
















This question already has an answer here:




  • How to sort Map values by key in Java?

    14 answers




I have one hashMap in java and want to sort based on one of the key value .
my hashmap likes like below.



HashMap= < string, map <string,string> >

key="toUppercase" -> value={ key="column_positions" -> value="4,5" ,
key="rule_order" -> value="3" }
key="replace" -> value= { key= "column_positions"-> value= "1,2" ,
key= "rule_order"-> value= "1" }
key= "concat" -> value={key= "column_positions"-> value= "6,7"
key ="rule_order"-> value= "2" }


I want to sort the hashmap based on rule_order. could you please help me.



My expected Output for outer hasmap is:



HashMap= < string, map <string,string> >

key="replace" -> value= { key= "column_positions"-> value= "1,2" ,
key= "rule_order"-> value= "1" }
key= "concat" -> value={key= "column_positions"-> value= "6,7"
key ="rule_order"-> value= "2" }
key="toUppercase" -> value={ key="column_positions" -> value="4,5" ,
key="rule_order" -> value="3" }


Thanks.










share|improve this question















marked as duplicate by daniu, Seelenvirtuose, Aomine, Federico Peralta Schaffner, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 10:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    A HashMap doesn't have an order. You need to use a list or a SortedMap.

    – marstran
    Jan 2 at 15:23











  • Look at a tree map. docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html You can provide a comparator or use the natural ordering of keys.

    – Nick Clark
    Jan 2 at 15:43
















-3
















This question already has an answer here:




  • How to sort Map values by key in Java?

    14 answers




I have one hashMap in java and want to sort based on one of the key value .
my hashmap likes like below.



HashMap= < string, map <string,string> >

key="toUppercase" -> value={ key="column_positions" -> value="4,5" ,
key="rule_order" -> value="3" }
key="replace" -> value= { key= "column_positions"-> value= "1,2" ,
key= "rule_order"-> value= "1" }
key= "concat" -> value={key= "column_positions"-> value= "6,7"
key ="rule_order"-> value= "2" }


I want to sort the hashmap based on rule_order. could you please help me.



My expected Output for outer hasmap is:



HashMap= < string, map <string,string> >

key="replace" -> value= { key= "column_positions"-> value= "1,2" ,
key= "rule_order"-> value= "1" }
key= "concat" -> value={key= "column_positions"-> value= "6,7"
key ="rule_order"-> value= "2" }
key="toUppercase" -> value={ key="column_positions" -> value="4,5" ,
key="rule_order" -> value="3" }


Thanks.










share|improve this question















marked as duplicate by daniu, Seelenvirtuose, Aomine, Federico Peralta Schaffner, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 10:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 2





    A HashMap doesn't have an order. You need to use a list or a SortedMap.

    – marstran
    Jan 2 at 15:23











  • Look at a tree map. docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html You can provide a comparator or use the natural ordering of keys.

    – Nick Clark
    Jan 2 at 15:43














-3












-3








-3









This question already has an answer here:




  • How to sort Map values by key in Java?

    14 answers




I have one hashMap in java and want to sort based on one of the key value .
my hashmap likes like below.



HashMap= < string, map <string,string> >

key="toUppercase" -> value={ key="column_positions" -> value="4,5" ,
key="rule_order" -> value="3" }
key="replace" -> value= { key= "column_positions"-> value= "1,2" ,
key= "rule_order"-> value= "1" }
key= "concat" -> value={key= "column_positions"-> value= "6,7"
key ="rule_order"-> value= "2" }


I want to sort the hashmap based on rule_order. could you please help me.



My expected Output for outer hasmap is:



HashMap= < string, map <string,string> >

key="replace" -> value= { key= "column_positions"-> value= "1,2" ,
key= "rule_order"-> value= "1" }
key= "concat" -> value={key= "column_positions"-> value= "6,7"
key ="rule_order"-> value= "2" }
key="toUppercase" -> value={ key="column_positions" -> value="4,5" ,
key="rule_order" -> value="3" }


Thanks.










share|improve this question

















This question already has an answer here:




  • How to sort Map values by key in Java?

    14 answers




I have one hashMap in java and want to sort based on one of the key value .
my hashmap likes like below.



HashMap= < string, map <string,string> >

key="toUppercase" -> value={ key="column_positions" -> value="4,5" ,
key="rule_order" -> value="3" }
key="replace" -> value= { key= "column_positions"-> value= "1,2" ,
key= "rule_order"-> value= "1" }
key= "concat" -> value={key= "column_positions"-> value= "6,7"
key ="rule_order"-> value= "2" }


I want to sort the hashmap based on rule_order. could you please help me.



My expected Output for outer hasmap is:



HashMap= < string, map <string,string> >

key="replace" -> value= { key= "column_positions"-> value= "1,2" ,
key= "rule_order"-> value= "1" }
key= "concat" -> value={key= "column_positions"-> value= "6,7"
key ="rule_order"-> value= "2" }
key="toUppercase" -> value={ key="column_positions" -> value="4,5" ,
key="rule_order" -> value="3" }


Thanks.





This question already has an answer here:




  • How to sort Map values by key in Java?

    14 answers








java sorting hashmap






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 4 at 8:03







uttam kumar dash

















asked Jan 2 at 15:22









uttam kumar dashuttam kumar dash

11




11




marked as duplicate by daniu, Seelenvirtuose, Aomine, Federico Peralta Schaffner, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 10:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by daniu, Seelenvirtuose, Aomine, Federico Peralta Schaffner, Mark Rotteveel java
Users with the  java badge can single-handedly close java questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 3 at 10:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2





    A HashMap doesn't have an order. You need to use a list or a SortedMap.

    – marstran
    Jan 2 at 15:23











  • Look at a tree map. docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html You can provide a comparator or use the natural ordering of keys.

    – Nick Clark
    Jan 2 at 15:43














  • 2





    A HashMap doesn't have an order. You need to use a list or a SortedMap.

    – marstran
    Jan 2 at 15:23











  • Look at a tree map. docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html You can provide a comparator or use the natural ordering of keys.

    – Nick Clark
    Jan 2 at 15:43








2




2





A HashMap doesn't have an order. You need to use a list or a SortedMap.

– marstran
Jan 2 at 15:23





A HashMap doesn't have an order. You need to use a list or a SortedMap.

– marstran
Jan 2 at 15:23













Look at a tree map. docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html You can provide a comparator or use the natural ordering of keys.

– Nick Clark
Jan 2 at 15:43





Look at a tree map. docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html You can provide a comparator or use the natural ordering of keys.

– Nick Clark
Jan 2 at 15:43












1 Answer
1






active

oldest

votes


















0














you can't sort by the key of the inner map. Moreover, you are not able to sort HashMap.
You can use TreeMap but with ruleOrder as key. but in this case, you won't be able to store duplicates. (with same rule order).
You can sort map in runtime :



Map<String, Map<String, String>> map = new HashMap<>();
map.entrySet()
.stream()
.sorted(Comparator.comparing(en -> en.getValue().get("rule_order")))
.forEach(this::doWork);


Please replce this::doWork with neaded code.
Please pay attention that this sample is not null-safe.






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    you can't sort by the key of the inner map. Moreover, you are not able to sort HashMap.
    You can use TreeMap but with ruleOrder as key. but in this case, you won't be able to store duplicates. (with same rule order).
    You can sort map in runtime :



    Map<String, Map<String, String>> map = new HashMap<>();
    map.entrySet()
    .stream()
    .sorted(Comparator.comparing(en -> en.getValue().get("rule_order")))
    .forEach(this::doWork);


    Please replce this::doWork with neaded code.
    Please pay attention that this sample is not null-safe.






    share|improve this answer




























      0














      you can't sort by the key of the inner map. Moreover, you are not able to sort HashMap.
      You can use TreeMap but with ruleOrder as key. but in this case, you won't be able to store duplicates. (with same rule order).
      You can sort map in runtime :



      Map<String, Map<String, String>> map = new HashMap<>();
      map.entrySet()
      .stream()
      .sorted(Comparator.comparing(en -> en.getValue().get("rule_order")))
      .forEach(this::doWork);


      Please replce this::doWork with neaded code.
      Please pay attention that this sample is not null-safe.






      share|improve this answer


























        0












        0








        0







        you can't sort by the key of the inner map. Moreover, you are not able to sort HashMap.
        You can use TreeMap but with ruleOrder as key. but in this case, you won't be able to store duplicates. (with same rule order).
        You can sort map in runtime :



        Map<String, Map<String, String>> map = new HashMap<>();
        map.entrySet()
        .stream()
        .sorted(Comparator.comparing(en -> en.getValue().get("rule_order")))
        .forEach(this::doWork);


        Please replce this::doWork with neaded code.
        Please pay attention that this sample is not null-safe.






        share|improve this answer













        you can't sort by the key of the inner map. Moreover, you are not able to sort HashMap.
        You can use TreeMap but with ruleOrder as key. but in this case, you won't be able to store duplicates. (with same rule order).
        You can sort map in runtime :



        Map<String, Map<String, String>> map = new HashMap<>();
        map.entrySet()
        .stream()
        .sorted(Comparator.comparing(en -> en.getValue().get("rule_order")))
        .forEach(this::doWork);


        Please replce this::doWork with neaded code.
        Please pay attention that this sample is not null-safe.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 15:48









        Vladyslav DiachenkoVladyslav Diachenko

        435




        435

















            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]