how to sort Map<String, Map>, based on one of the inside map key value in java [duplicate]
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.
java sorting hashmap
marked as duplicate by daniu, Seelenvirtuose, Aomine, Federico Peralta Schaffner, Mark Rotteveel
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.
add a comment |
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.
java sorting hashmap
marked as duplicate by daniu, Seelenvirtuose, Aomine, Federico Peralta Schaffner, Mark Rotteveel
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
AHashMap
doesn't have an order. You need to use a list or aSortedMap
.
– 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
add a comment |
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.
java sorting hashmap
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
java sorting hashmap
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
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
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
AHashMap
doesn't have an order. You need to use a list or aSortedMap
.
– 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
add a comment |
2
AHashMap
doesn't have an order. You need to use a list or aSortedMap
.
– 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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 2 at 15:48
Vladyslav DiachenkoVladyslav Diachenko
435
435
add a comment |
add a comment |
2
A
HashMap
doesn't have an order. You need to use a list or aSortedMap
.– 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