Java 8 Split String and Create Map inside MapIs Java “pass-by-reference” or “pass-by-value”?How do I efficiently iterate over each entry in a Java Map?Create ArrayList from arrayHow do I iterate over the words of a string?How do I read / convert an InputStream into a String in Java?How do I split a string on a delimiter in Bash?How to split a string in JavaHow do I convert a String to an int in Java?Creating a memory leak with JavaJava 8 List<V> into Map<K, V>
When a company launches a new product do they "come out" with a new product or do they "come up" with a new product?
Question on branch cuts and branch points
What defenses are there against being summoned by the Gate spell?
Is it legal for company to use my work email to pretend I still work there?
Why can't we play rap on piano?
How to format long polynomial?
Is it unprofessional to ask if a job posting on GlassDoor is real?
Linear Path Optimization with Two Dependent Variables
How to source a part of a file
Operational amplifier as a comparator at high frequency
How much RAM could one put in a typical 80386 setup?
Why doesn't a class having private constructor prevent inheriting from this class? How to control which classes can inherit from a certain base?
Client team has low performances and low technical skills: we always fix their work and now they stop collaborate with us. How to solve?
Theorems that impeded progress
Java Casting: Java 11 throws LambdaConversionException while 1.8 does not
if condition in the past
Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?
Revoked SSL certificate
Can a Cauchy sequence converge for one metric while not converging for another?
Why doesn't H₄O²⁺ exist?
Alternative to sending password over mail?
Can a vampire attack twice with their claws using multiattack?
Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)
How do I deal with an unproductive colleague in a small company?
Java 8 Split String and Create Map inside Map
Is Java “pass-by-reference” or “pass-by-value”?How do I efficiently iterate over each entry in a Java Map?Create ArrayList from arrayHow do I iterate over the words of a string?How do I read / convert an InputStream into a String in Java?How do I split a string on a delimiter in Bash?How to split a string in JavaHow do I convert a String to an int in Java?Creating a memory leak with JavaJava 8 List<V> into Map<K, V>
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a String like 101-1-5,101-2-4,102-1-5,102-2-5,102-3-5,103-1-4.
I want to add this in to a Map<String, Map<String, String>>.
Like: 101=1=5, 2=4, 102=1=5, 2=5, 3=5, 103=1=4
How to do this using Java 8 stream?
I tried using normal Java. And it works fine.
public class Util
public static void main(String[] args)
String samp = "101-1-5,101-2-4,102-1-5,102-2-5,102-3-5,103-1-4";
Map<String, Map<String, String>> m1 = new HashMap<>();
Map<String, String> m2 = null;
String[] items = samp.split(",");
for(int i=0; i<items.length; i++)
System.out.println(m1);
java split java-8 java-stream
add a comment |
I have a String like 101-1-5,101-2-4,102-1-5,102-2-5,102-3-5,103-1-4.
I want to add this in to a Map<String, Map<String, String>>.
Like: 101=1=5, 2=4, 102=1=5, 2=5, 3=5, 103=1=4
How to do this using Java 8 stream?
I tried using normal Java. And it works fine.
public class Util
public static void main(String[] args)
String samp = "101-1-5,101-2-4,102-1-5,102-2-5,102-3-5,103-1-4";
Map<String, Map<String, String>> m1 = new HashMap<>();
Map<String, String> m2 = null;
String[] items = samp.split(",");
for(int i=0; i<items.length; i++)
System.out.println(m1);
java split java-8 java-stream
add a comment |
I have a String like 101-1-5,101-2-4,102-1-5,102-2-5,102-3-5,103-1-4.
I want to add this in to a Map<String, Map<String, String>>.
Like: 101=1=5, 2=4, 102=1=5, 2=5, 3=5, 103=1=4
How to do this using Java 8 stream?
I tried using normal Java. And it works fine.
public class Util
public static void main(String[] args)
String samp = "101-1-5,101-2-4,102-1-5,102-2-5,102-3-5,103-1-4";
Map<String, Map<String, String>> m1 = new HashMap<>();
Map<String, String> m2 = null;
String[] items = samp.split(",");
for(int i=0; i<items.length; i++)
System.out.println(m1);
java split java-8 java-stream
I have a String like 101-1-5,101-2-4,102-1-5,102-2-5,102-3-5,103-1-4.
I want to add this in to a Map<String, Map<String, String>>.
Like: 101=1=5, 2=4, 102=1=5, 2=5, 3=5, 103=1=4
How to do this using Java 8 stream?
I tried using normal Java. And it works fine.
public class Util
public static void main(String[] args)
String samp = "101-1-5,101-2-4,102-1-5,102-2-5,102-3-5,103-1-4";
Map<String, Map<String, String>> m1 = new HashMap<>();
Map<String, String> m2 = null;
String[] items = samp.split(",");
for(int i=0; i<items.length; i++)
System.out.println(m1);
java split java-8 java-stream
java split java-8 java-stream
edited Mar 21 at 23:46
Samuel Philipp
3,7891729
3,7891729
asked Mar 21 at 20:34
ShakthiShakthi
204317
204317
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use the following snippet for this:
Map<String, Map<String, String>> result = Arrays.stream(samp.split(","))
.map(i -> i.split("-"))
.collect(Collectors.groupingBy(a -> a[0], Collectors.toMap(a -> a[1], a -> a[2])));
First it creates a Stream of your items, which are mapped to a stream of arrays, containing the subitems. At the end you collect all by using group by on the first subitem and create an inner map with the second value as key and the last one as value.
The result is:
101=1=5, 2=4, 102=1=5, 2=5, 3=5, 103=1=4
add a comment |
Map<String, Map<String, String>> map = Arrays.stream(samp.split(","))
.map(s -> s.split("-"))
.collect(Collectors.toMap(
o -> o[0],
o -> Map.of(o[1], o[2]),
(m1, m2) -> Stream.concat(m1.entrySet().stream(), m2.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))));
Map.of is from Java 9, you can use AbstractMap.SimpleEntry if you are on Java 8.
Samuel's answer is better though, much concise.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288838%2fjava-8-split-string-and-create-map-inside-map%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the following snippet for this:
Map<String, Map<String, String>> result = Arrays.stream(samp.split(","))
.map(i -> i.split("-"))
.collect(Collectors.groupingBy(a -> a[0], Collectors.toMap(a -> a[1], a -> a[2])));
First it creates a Stream of your items, which are mapped to a stream of arrays, containing the subitems. At the end you collect all by using group by on the first subitem and create an inner map with the second value as key and the last one as value.
The result is:
101=1=5, 2=4, 102=1=5, 2=5, 3=5, 103=1=4
add a comment |
You can use the following snippet for this:
Map<String, Map<String, String>> result = Arrays.stream(samp.split(","))
.map(i -> i.split("-"))
.collect(Collectors.groupingBy(a -> a[0], Collectors.toMap(a -> a[1], a -> a[2])));
First it creates a Stream of your items, which are mapped to a stream of arrays, containing the subitems. At the end you collect all by using group by on the first subitem and create an inner map with the second value as key and the last one as value.
The result is:
101=1=5, 2=4, 102=1=5, 2=5, 3=5, 103=1=4
add a comment |
You can use the following snippet for this:
Map<String, Map<String, String>> result = Arrays.stream(samp.split(","))
.map(i -> i.split("-"))
.collect(Collectors.groupingBy(a -> a[0], Collectors.toMap(a -> a[1], a -> a[2])));
First it creates a Stream of your items, which are mapped to a stream of arrays, containing the subitems. At the end you collect all by using group by on the first subitem and create an inner map with the second value as key and the last one as value.
The result is:
101=1=5, 2=4, 102=1=5, 2=5, 3=5, 103=1=4
You can use the following snippet for this:
Map<String, Map<String, String>> result = Arrays.stream(samp.split(","))
.map(i -> i.split("-"))
.collect(Collectors.groupingBy(a -> a[0], Collectors.toMap(a -> a[1], a -> a[2])));
First it creates a Stream of your items, which are mapped to a stream of arrays, containing the subitems. At the end you collect all by using group by on the first subitem and create an inner map with the second value as key and the last one as value.
The result is:
101=1=5, 2=4, 102=1=5, 2=5, 3=5, 103=1=4
edited Mar 23 at 11:06
marc_s
584k13011241270
584k13011241270
answered Mar 21 at 22:53
Samuel PhilippSamuel Philipp
3,7891729
3,7891729
add a comment |
add a comment |
Map<String, Map<String, String>> map = Arrays.stream(samp.split(","))
.map(s -> s.split("-"))
.collect(Collectors.toMap(
o -> o[0],
o -> Map.of(o[1], o[2]),
(m1, m2) -> Stream.concat(m1.entrySet().stream(), m2.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))));
Map.of is from Java 9, you can use AbstractMap.SimpleEntry if you are on Java 8.
Samuel's answer is better though, much concise.
add a comment |
Map<String, Map<String, String>> map = Arrays.stream(samp.split(","))
.map(s -> s.split("-"))
.collect(Collectors.toMap(
o -> o[0],
o -> Map.of(o[1], o[2]),
(m1, m2) -> Stream.concat(m1.entrySet().stream(), m2.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))));
Map.of is from Java 9, you can use AbstractMap.SimpleEntry if you are on Java 8.
Samuel's answer is better though, much concise.
add a comment |
Map<String, Map<String, String>> map = Arrays.stream(samp.split(","))
.map(s -> s.split("-"))
.collect(Collectors.toMap(
o -> o[0],
o -> Map.of(o[1], o[2]),
(m1, m2) -> Stream.concat(m1.entrySet().stream(), m2.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))));
Map.of is from Java 9, you can use AbstractMap.SimpleEntry if you are on Java 8.
Samuel's answer is better though, much concise.
Map<String, Map<String, String>> map = Arrays.stream(samp.split(","))
.map(s -> s.split("-"))
.collect(Collectors.toMap(
o -> o[0],
o -> Map.of(o[1], o[2]),
(m1, m2) -> Stream.concat(m1.entrySet().stream(), m2.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))));
Map.of is from Java 9, you can use AbstractMap.SimpleEntry if you are on Java 8.
Samuel's answer is better though, much concise.
edited Mar 21 at 23:03
answered Mar 21 at 22:50
KartikKartik
4,52231537
4,52231537
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288838%2fjava-8-split-string-and-create-map-inside-map%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown