How to get each array of String with the largest number and with an unique key?How to generate a random alpha-numeric string?How do I efficiently iterate over each entry in a Java Map?How does the Java 'for each' loop work?How do I read / convert an InputStream into a String in Java?How to get an enum value from a string value in Java?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How to split a string in JavaConvert ArrayList<String> to String[] arrayHow do I convert a String to an int in Java?
Is there an idiom that means that you are in a very strong negotiation position in a negotiation?
Why did Drogon spare this character?
Merge pdfs sequentially
Why'd a rational buyer offer to buy with no conditions precedent?
How can I get a refund from a seller who only accepts Zelle?
Are there any German nonsense poems (Jabberwocky)?
To exponential digit growth and beyond!
Where is Jon going?
Papers on ArXiv as main references
Count all vowels in string
The disk image is 497GB smaller than the target device
Why do the i8080 I/O instructions take a byte-sized operand to determine the port?
Cisco 3750X Power Cable
Team has team lunch everyday, am I forced to go?
Why is unzipped directory exactly 4.0K (much smaller than zipped file)?
How would a developer who mostly fixed bugs for years at a company call out their contributions in their CV?
Why isn't Tyrion mentioned in 'A song of Ice and Fire'?
Fill area of x^2+y^2>1 and x^2+y^2>4 using patterns and tikzpicture
Visual Block Mode edit with sequential number
Is this homebrew "Cactus Grenade" cantrip balanced?
Are there historical examples of audiences drawn to a work that was "so bad it's good"?
Why is this integration method not valid?
Flatten not working
How does the Earth's center produce heat?
How to get each array of String with the largest number and with an unique key?
How to generate a random alpha-numeric string?How do I efficiently iterate over each entry in a Java Map?How does the Java 'for each' loop work?How do I read / convert an InputStream into a String in Java?How to get an enum value from a string value in Java?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How to split a string in JavaConvert ArrayList<String> to String[] arrayHow do I convert a String to an int in Java?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have an ArrayList<String[]>
which contains the following data:
[Branford, Paddock Lane, 136, 1]
[Branford, Paddock Lane, 42, 3]
[Branford, Hartford Road, 117, 0]
[Branford, Hartford Road, 45, 3]
[McAlister, Sigourney Street, 103, 3]
[McAlister, Sigourney Street, 61, 1]
[McAlister, Stanford Street, 1, 3]
[McAlister, Stanford Street, 34, 4]
[Cypress Gardens, Route 115, 112, 12]
[Cypress Gardens, Park Road, 10, 4]
[Cypress Gardens, Park Road, 49, 4]
[Cypress Gardens, Old Farm Road, 28, 5]
[Cypress Gardens, Old Farm Road, 79, 3]
[Germfask, Exeter Court, 28, 7]
[Germfask, South Boulevard, 119, 6]
[Germfask, South Boulevard, 135, 3]
[Minford, Liberty Lane, 41, 3]
[Minford, Liberty Lane, 52, 4]
[Minford, State Street West, 103, 0]
The first element from each array of strings is a city's name and the last element is a counter. I need to extract data which will contain also array of strings but only with one city name and the largest counter. For example, for Branford
the result will be [Branford, Paddock Lane, 42, 3]
. How can I extract this data only with forEach
loops?
Thank you.
I have tried by using a forEach
through the ArrayList
and comparing each counter from current element with the next one where the city name is the same.
The output is not as expected, as it contains no data.
final int[] max = Integer.parseInt(arrayListSrc.get(0)[3]);
ArrayList<String[]> arrayList = new ArrayList<>(1);
final int[] index = 0;
arrayListSrc.forEach(strings ->
if (strings[0].equals(arrayListSrc.get(index[0])[0]))
if (Integer.parseInt(strings[3]) < Integer.parseInt(arrayListSrc.get(index[0])[3]))
if (Integer.parseInt(arrayListSrc.get(index[0])[3]) > max[0])
max[0] = Integer.parseInt(arrayListSrc.get(index[0])[3]);
arrayList.add(strings);
index[0]++;
);
java arraylist
|
show 3 more comments
I have an ArrayList<String[]>
which contains the following data:
[Branford, Paddock Lane, 136, 1]
[Branford, Paddock Lane, 42, 3]
[Branford, Hartford Road, 117, 0]
[Branford, Hartford Road, 45, 3]
[McAlister, Sigourney Street, 103, 3]
[McAlister, Sigourney Street, 61, 1]
[McAlister, Stanford Street, 1, 3]
[McAlister, Stanford Street, 34, 4]
[Cypress Gardens, Route 115, 112, 12]
[Cypress Gardens, Park Road, 10, 4]
[Cypress Gardens, Park Road, 49, 4]
[Cypress Gardens, Old Farm Road, 28, 5]
[Cypress Gardens, Old Farm Road, 79, 3]
[Germfask, Exeter Court, 28, 7]
[Germfask, South Boulevard, 119, 6]
[Germfask, South Boulevard, 135, 3]
[Minford, Liberty Lane, 41, 3]
[Minford, Liberty Lane, 52, 4]
[Minford, State Street West, 103, 0]
The first element from each array of strings is a city's name and the last element is a counter. I need to extract data which will contain also array of strings but only with one city name and the largest counter. For example, for Branford
the result will be [Branford, Paddock Lane, 42, 3]
. How can I extract this data only with forEach
loops?
Thank you.
I have tried by using a forEach
through the ArrayList
and comparing each counter from current element with the next one where the city name is the same.
The output is not as expected, as it contains no data.
final int[] max = Integer.parseInt(arrayListSrc.get(0)[3]);
ArrayList<String[]> arrayList = new ArrayList<>(1);
final int[] index = 0;
arrayListSrc.forEach(strings ->
if (strings[0].equals(arrayListSrc.get(index[0])[0]))
if (Integer.parseInt(strings[3]) < Integer.parseInt(arrayListSrc.get(index[0])[3]))
if (Integer.parseInt(arrayListSrc.get(index[0])[3]) > max[0])
max[0] = Integer.parseInt(arrayListSrc.get(index[0])[3]);
arrayList.add(strings);
index[0]++;
);
java arraylist
2
Why notBranford, Paddock Lane, 42, 3
?
– forpas
Mar 23 at 22:05
1
Please edit into the question the minimal code that demonstrates what you have attempted so far.
– KevinO
Mar 23 at 22:08
@forpas oops, my bad.
– Vitaliy Kalmyk
Mar 23 at 22:15
@KevinO i have added the code
– Vitaliy Kalmyk
Mar 23 at 22:20
5
Your code is extremely hard to read, probably also for you, for several reasons. You're using meaningless names (arrayListSrc strings). You're using magic numbers everywhere. You're using strings to represent integers. You shouldn't be dealing with a List<String[]>. You should be dealing with a List<Address>, where Address is a class with a city, a street, a number and a counter. Naming things and using the right types makes things much much easier.
– JB Nizet
Mar 23 at 22:24
|
show 3 more comments
I have an ArrayList<String[]>
which contains the following data:
[Branford, Paddock Lane, 136, 1]
[Branford, Paddock Lane, 42, 3]
[Branford, Hartford Road, 117, 0]
[Branford, Hartford Road, 45, 3]
[McAlister, Sigourney Street, 103, 3]
[McAlister, Sigourney Street, 61, 1]
[McAlister, Stanford Street, 1, 3]
[McAlister, Stanford Street, 34, 4]
[Cypress Gardens, Route 115, 112, 12]
[Cypress Gardens, Park Road, 10, 4]
[Cypress Gardens, Park Road, 49, 4]
[Cypress Gardens, Old Farm Road, 28, 5]
[Cypress Gardens, Old Farm Road, 79, 3]
[Germfask, Exeter Court, 28, 7]
[Germfask, South Boulevard, 119, 6]
[Germfask, South Boulevard, 135, 3]
[Minford, Liberty Lane, 41, 3]
[Minford, Liberty Lane, 52, 4]
[Minford, State Street West, 103, 0]
The first element from each array of strings is a city's name and the last element is a counter. I need to extract data which will contain also array of strings but only with one city name and the largest counter. For example, for Branford
the result will be [Branford, Paddock Lane, 42, 3]
. How can I extract this data only with forEach
loops?
Thank you.
I have tried by using a forEach
through the ArrayList
and comparing each counter from current element with the next one where the city name is the same.
The output is not as expected, as it contains no data.
final int[] max = Integer.parseInt(arrayListSrc.get(0)[3]);
ArrayList<String[]> arrayList = new ArrayList<>(1);
final int[] index = 0;
arrayListSrc.forEach(strings ->
if (strings[0].equals(arrayListSrc.get(index[0])[0]))
if (Integer.parseInt(strings[3]) < Integer.parseInt(arrayListSrc.get(index[0])[3]))
if (Integer.parseInt(arrayListSrc.get(index[0])[3]) > max[0])
max[0] = Integer.parseInt(arrayListSrc.get(index[0])[3]);
arrayList.add(strings);
index[0]++;
);
java arraylist
I have an ArrayList<String[]>
which contains the following data:
[Branford, Paddock Lane, 136, 1]
[Branford, Paddock Lane, 42, 3]
[Branford, Hartford Road, 117, 0]
[Branford, Hartford Road, 45, 3]
[McAlister, Sigourney Street, 103, 3]
[McAlister, Sigourney Street, 61, 1]
[McAlister, Stanford Street, 1, 3]
[McAlister, Stanford Street, 34, 4]
[Cypress Gardens, Route 115, 112, 12]
[Cypress Gardens, Park Road, 10, 4]
[Cypress Gardens, Park Road, 49, 4]
[Cypress Gardens, Old Farm Road, 28, 5]
[Cypress Gardens, Old Farm Road, 79, 3]
[Germfask, Exeter Court, 28, 7]
[Germfask, South Boulevard, 119, 6]
[Germfask, South Boulevard, 135, 3]
[Minford, Liberty Lane, 41, 3]
[Minford, Liberty Lane, 52, 4]
[Minford, State Street West, 103, 0]
The first element from each array of strings is a city's name and the last element is a counter. I need to extract data which will contain also array of strings but only with one city name and the largest counter. For example, for Branford
the result will be [Branford, Paddock Lane, 42, 3]
. How can I extract this data only with forEach
loops?
Thank you.
I have tried by using a forEach
through the ArrayList
and comparing each counter from current element with the next one where the city name is the same.
The output is not as expected, as it contains no data.
final int[] max = Integer.parseInt(arrayListSrc.get(0)[3]);
ArrayList<String[]> arrayList = new ArrayList<>(1);
final int[] index = 0;
arrayListSrc.forEach(strings ->
if (strings[0].equals(arrayListSrc.get(index[0])[0]))
if (Integer.parseInt(strings[3]) < Integer.parseInt(arrayListSrc.get(index[0])[3]))
if (Integer.parseInt(arrayListSrc.get(index[0])[3]) > max[0])
max[0] = Integer.parseInt(arrayListSrc.get(index[0])[3]);
arrayList.add(strings);
index[0]++;
);
java arraylist
java arraylist
edited Mar 23 at 22:16
Vitaliy Kalmyk
asked Mar 23 at 21:55
Vitaliy KalmykVitaliy Kalmyk
155
155
2
Why notBranford, Paddock Lane, 42, 3
?
– forpas
Mar 23 at 22:05
1
Please edit into the question the minimal code that demonstrates what you have attempted so far.
– KevinO
Mar 23 at 22:08
@forpas oops, my bad.
– Vitaliy Kalmyk
Mar 23 at 22:15
@KevinO i have added the code
– Vitaliy Kalmyk
Mar 23 at 22:20
5
Your code is extremely hard to read, probably also for you, for several reasons. You're using meaningless names (arrayListSrc strings). You're using magic numbers everywhere. You're using strings to represent integers. You shouldn't be dealing with a List<String[]>. You should be dealing with a List<Address>, where Address is a class with a city, a street, a number and a counter. Naming things and using the right types makes things much much easier.
– JB Nizet
Mar 23 at 22:24
|
show 3 more comments
2
Why notBranford, Paddock Lane, 42, 3
?
– forpas
Mar 23 at 22:05
1
Please edit into the question the minimal code that demonstrates what you have attempted so far.
– KevinO
Mar 23 at 22:08
@forpas oops, my bad.
– Vitaliy Kalmyk
Mar 23 at 22:15
@KevinO i have added the code
– Vitaliy Kalmyk
Mar 23 at 22:20
5
Your code is extremely hard to read, probably also for you, for several reasons. You're using meaningless names (arrayListSrc strings). You're using magic numbers everywhere. You're using strings to represent integers. You shouldn't be dealing with a List<String[]>. You should be dealing with a List<Address>, where Address is a class with a city, a street, a number and a counter. Naming things and using the right types makes things much much easier.
– JB Nizet
Mar 23 at 22:24
2
2
Why not
Branford, Paddock Lane, 42, 3
?– forpas
Mar 23 at 22:05
Why not
Branford, Paddock Lane, 42, 3
?– forpas
Mar 23 at 22:05
1
1
Please edit into the question the minimal code that demonstrates what you have attempted so far.
– KevinO
Mar 23 at 22:08
Please edit into the question the minimal code that demonstrates what you have attempted so far.
– KevinO
Mar 23 at 22:08
@forpas oops, my bad.
– Vitaliy Kalmyk
Mar 23 at 22:15
@forpas oops, my bad.
– Vitaliy Kalmyk
Mar 23 at 22:15
@KevinO i have added the code
– Vitaliy Kalmyk
Mar 23 at 22:20
@KevinO i have added the code
– Vitaliy Kalmyk
Mar 23 at 22:20
5
5
Your code is extremely hard to read, probably also for you, for several reasons. You're using meaningless names (arrayListSrc strings). You're using magic numbers everywhere. You're using strings to represent integers. You shouldn't be dealing with a List<String[]>. You should be dealing with a List<Address>, where Address is a class with a city, a street, a number and a counter. Naming things and using the right types makes things much much easier.
– JB Nizet
Mar 23 at 22:24
Your code is extremely hard to read, probably also for you, for several reasons. You're using meaningless names (arrayListSrc strings). You're using magic numbers everywhere. You're using strings to represent integers. You shouldn't be dealing with a List<String[]>. You should be dealing with a List<Address>, where Address is a class with a city, a street, a number and a counter. Naming things and using the right types makes things much much easier.
– JB Nizet
Mar 23 at 22:24
|
show 3 more comments
2 Answers
2
active
oldest
votes
I had something like this:
Map<String, String[]> result = new TreeMap<>();
for(String[] s : source)
if(!result.containsKey(s[0]))
result.put(s[0], s);
else if (Integer.valueOf(result.get(s[0])[3]) < Integer.valueOf(s[3]))
result.put(s[0], s);
for(String k : result.keySet())
System.out.println(k + ": " + Arrays.toString(result.get(k)));
This should print:
Branford: [Branford, Paddock Lane, 42, 3]
Cypress Gardens: [Cypress Gardens, Route 115, 112, 12]
Germfask: [Germfask, Exeter Court, 28, 7]
McAlister: [McAlister, Stanford Street, 34, 4]
Minford: [Minford, Liberty Lane, 52, 4]
Is that the result you are after? You do have the challenge that you have entries with the same number of hits.
Yes, that is the result I was after. Thank a lot for your explanation
– Vitaliy Kalmyk
Mar 24 at 15:39
@VitaliyKalmyk Please use the accept answer and upvote buttons to convey this.
– pjanssen
Mar 25 at 14:44
add a comment |
First of all, try to map your String[]
to a suitable data structure next time, since it will be a lot easier to handle. Just create a suitable class, thats what object oriented programming is for.
But, to answer the question, you can map the plain List<String[]>
in the following way:
Map<String, String[]> results = source.stream().collect(
Collectors.toMap(s -> s[0], Function.identity(),
BinaryOperator.maxBy(
Comparator.comparing(s -> Integer.valueOf(s[3])))));
The result is a Map where the key represents the city name and the value is the original String[]
maxed by the counter. This solution does not require the use of forEach()
, but rather stream operations to achieve the expected result.
You can, however, use forEach()
to print or further operate on the results:
results.forEach((s, strings) ->
System.out.println(s + " " + Arrays.toString(strings));
);
1
Well, you have right approach, but why not putting it to a List? @Glains
– MS90
Mar 24 at 0:03
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%2f55318761%2fhow-to-get-each-array-of-string-with-the-largest-number-and-with-an-unique-key%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
I had something like this:
Map<String, String[]> result = new TreeMap<>();
for(String[] s : source)
if(!result.containsKey(s[0]))
result.put(s[0], s);
else if (Integer.valueOf(result.get(s[0])[3]) < Integer.valueOf(s[3]))
result.put(s[0], s);
for(String k : result.keySet())
System.out.println(k + ": " + Arrays.toString(result.get(k)));
This should print:
Branford: [Branford, Paddock Lane, 42, 3]
Cypress Gardens: [Cypress Gardens, Route 115, 112, 12]
Germfask: [Germfask, Exeter Court, 28, 7]
McAlister: [McAlister, Stanford Street, 34, 4]
Minford: [Minford, Liberty Lane, 52, 4]
Is that the result you are after? You do have the challenge that you have entries with the same number of hits.
Yes, that is the result I was after. Thank a lot for your explanation
– Vitaliy Kalmyk
Mar 24 at 15:39
@VitaliyKalmyk Please use the accept answer and upvote buttons to convey this.
– pjanssen
Mar 25 at 14:44
add a comment |
I had something like this:
Map<String, String[]> result = new TreeMap<>();
for(String[] s : source)
if(!result.containsKey(s[0]))
result.put(s[0], s);
else if (Integer.valueOf(result.get(s[0])[3]) < Integer.valueOf(s[3]))
result.put(s[0], s);
for(String k : result.keySet())
System.out.println(k + ": " + Arrays.toString(result.get(k)));
This should print:
Branford: [Branford, Paddock Lane, 42, 3]
Cypress Gardens: [Cypress Gardens, Route 115, 112, 12]
Germfask: [Germfask, Exeter Court, 28, 7]
McAlister: [McAlister, Stanford Street, 34, 4]
Minford: [Minford, Liberty Lane, 52, 4]
Is that the result you are after? You do have the challenge that you have entries with the same number of hits.
Yes, that is the result I was after. Thank a lot for your explanation
– Vitaliy Kalmyk
Mar 24 at 15:39
@VitaliyKalmyk Please use the accept answer and upvote buttons to convey this.
– pjanssen
Mar 25 at 14:44
add a comment |
I had something like this:
Map<String, String[]> result = new TreeMap<>();
for(String[] s : source)
if(!result.containsKey(s[0]))
result.put(s[0], s);
else if (Integer.valueOf(result.get(s[0])[3]) < Integer.valueOf(s[3]))
result.put(s[0], s);
for(String k : result.keySet())
System.out.println(k + ": " + Arrays.toString(result.get(k)));
This should print:
Branford: [Branford, Paddock Lane, 42, 3]
Cypress Gardens: [Cypress Gardens, Route 115, 112, 12]
Germfask: [Germfask, Exeter Court, 28, 7]
McAlister: [McAlister, Stanford Street, 34, 4]
Minford: [Minford, Liberty Lane, 52, 4]
Is that the result you are after? You do have the challenge that you have entries with the same number of hits.
I had something like this:
Map<String, String[]> result = new TreeMap<>();
for(String[] s : source)
if(!result.containsKey(s[0]))
result.put(s[0], s);
else if (Integer.valueOf(result.get(s[0])[3]) < Integer.valueOf(s[3]))
result.put(s[0], s);
for(String k : result.keySet())
System.out.println(k + ": " + Arrays.toString(result.get(k)));
This should print:
Branford: [Branford, Paddock Lane, 42, 3]
Cypress Gardens: [Cypress Gardens, Route 115, 112, 12]
Germfask: [Germfask, Exeter Court, 28, 7]
McAlister: [McAlister, Stanford Street, 34, 4]
Minford: [Minford, Liberty Lane, 52, 4]
Is that the result you are after? You do have the challenge that you have entries with the same number of hits.
answered Mar 23 at 22:53
pjanssenpjanssen
656928
656928
Yes, that is the result I was after. Thank a lot for your explanation
– Vitaliy Kalmyk
Mar 24 at 15:39
@VitaliyKalmyk Please use the accept answer and upvote buttons to convey this.
– pjanssen
Mar 25 at 14:44
add a comment |
Yes, that is the result I was after. Thank a lot for your explanation
– Vitaliy Kalmyk
Mar 24 at 15:39
@VitaliyKalmyk Please use the accept answer and upvote buttons to convey this.
– pjanssen
Mar 25 at 14:44
Yes, that is the result I was after. Thank a lot for your explanation
– Vitaliy Kalmyk
Mar 24 at 15:39
Yes, that is the result I was after. Thank a lot for your explanation
– Vitaliy Kalmyk
Mar 24 at 15:39
@VitaliyKalmyk Please use the accept answer and upvote buttons to convey this.
– pjanssen
Mar 25 at 14:44
@VitaliyKalmyk Please use the accept answer and upvote buttons to convey this.
– pjanssen
Mar 25 at 14:44
add a comment |
First of all, try to map your String[]
to a suitable data structure next time, since it will be a lot easier to handle. Just create a suitable class, thats what object oriented programming is for.
But, to answer the question, you can map the plain List<String[]>
in the following way:
Map<String, String[]> results = source.stream().collect(
Collectors.toMap(s -> s[0], Function.identity(),
BinaryOperator.maxBy(
Comparator.comparing(s -> Integer.valueOf(s[3])))));
The result is a Map where the key represents the city name and the value is the original String[]
maxed by the counter. This solution does not require the use of forEach()
, but rather stream operations to achieve the expected result.
You can, however, use forEach()
to print or further operate on the results:
results.forEach((s, strings) ->
System.out.println(s + " " + Arrays.toString(strings));
);
1
Well, you have right approach, but why not putting it to a List? @Glains
– MS90
Mar 24 at 0:03
add a comment |
First of all, try to map your String[]
to a suitable data structure next time, since it will be a lot easier to handle. Just create a suitable class, thats what object oriented programming is for.
But, to answer the question, you can map the plain List<String[]>
in the following way:
Map<String, String[]> results = source.stream().collect(
Collectors.toMap(s -> s[0], Function.identity(),
BinaryOperator.maxBy(
Comparator.comparing(s -> Integer.valueOf(s[3])))));
The result is a Map where the key represents the city name and the value is the original String[]
maxed by the counter. This solution does not require the use of forEach()
, but rather stream operations to achieve the expected result.
You can, however, use forEach()
to print or further operate on the results:
results.forEach((s, strings) ->
System.out.println(s + " " + Arrays.toString(strings));
);
1
Well, you have right approach, but why not putting it to a List? @Glains
– MS90
Mar 24 at 0:03
add a comment |
First of all, try to map your String[]
to a suitable data structure next time, since it will be a lot easier to handle. Just create a suitable class, thats what object oriented programming is for.
But, to answer the question, you can map the plain List<String[]>
in the following way:
Map<String, String[]> results = source.stream().collect(
Collectors.toMap(s -> s[0], Function.identity(),
BinaryOperator.maxBy(
Comparator.comparing(s -> Integer.valueOf(s[3])))));
The result is a Map where the key represents the city name and the value is the original String[]
maxed by the counter. This solution does not require the use of forEach()
, but rather stream operations to achieve the expected result.
You can, however, use forEach()
to print or further operate on the results:
results.forEach((s, strings) ->
System.out.println(s + " " + Arrays.toString(strings));
);
First of all, try to map your String[]
to a suitable data structure next time, since it will be a lot easier to handle. Just create a suitable class, thats what object oriented programming is for.
But, to answer the question, you can map the plain List<String[]>
in the following way:
Map<String, String[]> results = source.stream().collect(
Collectors.toMap(s -> s[0], Function.identity(),
BinaryOperator.maxBy(
Comparator.comparing(s -> Integer.valueOf(s[3])))));
The result is a Map where the key represents the city name and the value is the original String[]
maxed by the counter. This solution does not require the use of forEach()
, but rather stream operations to achieve the expected result.
You can, however, use forEach()
to print or further operate on the results:
results.forEach((s, strings) ->
System.out.println(s + " " + Arrays.toString(strings));
);
edited Mar 23 at 23:55
answered Mar 23 at 23:22
GlainsGlains
1,278920
1,278920
1
Well, you have right approach, but why not putting it to a List? @Glains
– MS90
Mar 24 at 0:03
add a comment |
1
Well, you have right approach, but why not putting it to a List? @Glains
– MS90
Mar 24 at 0:03
1
1
Well, you have right approach, but why not putting it to a List? @Glains
– MS90
Mar 24 at 0:03
Well, you have right approach, but why not putting it to a List? @Glains
– MS90
Mar 24 at 0:03
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%2f55318761%2fhow-to-get-each-array-of-string-with-the-largest-number-and-with-an-unique-key%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
2
Why not
Branford, Paddock Lane, 42, 3
?– forpas
Mar 23 at 22:05
1
Please edit into the question the minimal code that demonstrates what you have attempted so far.
– KevinO
Mar 23 at 22:08
@forpas oops, my bad.
– Vitaliy Kalmyk
Mar 23 at 22:15
@KevinO i have added the code
– Vitaliy Kalmyk
Mar 23 at 22:20
5
Your code is extremely hard to read, probably also for you, for several reasons. You're using meaningless names (arrayListSrc strings). You're using magic numbers everywhere. You're using strings to represent integers. You shouldn't be dealing with a List<String[]>. You should be dealing with a List<Address>, where Address is a class with a city, a street, a number and a counter. Naming things and using the right types makes things much much easier.
– JB Nizet
Mar 23 at 22:24