How to check if an array contains a partial value once?
up vote
-1
down vote
favorite
I have an array,
arr = [name1 class age, name2 class age, name3 class age, name1 class age, name2 class age]
I want to check each item and store it in another array
String r = Arrays.stream(arr)
.filter(s -> s.contains("name1"))
.toArray(String::new);
How do I save just the first name1 using the above code?
output: [name1 class age, name1 class age]
expected output: [name1 class age]
java
add a comment |
up vote
-1
down vote
favorite
I have an array,
arr = [name1 class age, name2 class age, name3 class age, name1 class age, name2 class age]
I want to check each item and store it in another array
String r = Arrays.stream(arr)
.filter(s -> s.contains("name1"))
.toArray(String::new);
How do I save just the first name1 using the above code?
output: [name1 class age, name1 class age]
expected output: [name1 class age]
java
1
what's not working here? Post a Minimal, Complete, and Verifiable example
– Reimeus
Nov 6 at 20:55
@Reimeus Please check the updated post
– c.r
Nov 6 at 20:57
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have an array,
arr = [name1 class age, name2 class age, name3 class age, name1 class age, name2 class age]
I want to check each item and store it in another array
String r = Arrays.stream(arr)
.filter(s -> s.contains("name1"))
.toArray(String::new);
How do I save just the first name1 using the above code?
output: [name1 class age, name1 class age]
expected output: [name1 class age]
java
I have an array,
arr = [name1 class age, name2 class age, name3 class age, name1 class age, name2 class age]
I want to check each item and store it in another array
String r = Arrays.stream(arr)
.filter(s -> s.contains("name1"))
.toArray(String::new);
How do I save just the first name1 using the above code?
output: [name1 class age, name1 class age]
expected output: [name1 class age]
java
java
edited Nov 10 at 22:46
Zabuza
11k52538
11k52538
asked Nov 6 at 20:47
c.r
188
188
1
what's not working here? Post a Minimal, Complete, and Verifiable example
– Reimeus
Nov 6 at 20:55
@Reimeus Please check the updated post
– c.r
Nov 6 at 20:57
add a comment |
1
what's not working here? Post a Minimal, Complete, and Verifiable example
– Reimeus
Nov 6 at 20:55
@Reimeus Please check the updated post
– c.r
Nov 6 at 20:57
1
1
what's not working here? Post a Minimal, Complete, and Verifiable example
– Reimeus
Nov 6 at 20:55
what's not working here? Post a Minimal, Complete, and Verifiable example
– Reimeus
Nov 6 at 20:55
@Reimeus Please check the updated post
– c.r
Nov 6 at 20:57
@Reimeus Please check the updated post
– c.r
Nov 6 at 20:57
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
In case you need to retrieve only the first found element:
String r = new String Arrays.stream(arr)
.filter(s -> s.contains("name1"))
.findFirst().orElse("") ;
add a comment |
up vote
1
down vote
You could map the first match
String array =
Arrays.stream(arr).filter
(s -> s.contains("name1")).findFirst().map(s -> new String s ).orElse(new String );
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
In case you need to retrieve only the first found element:
String r = new String Arrays.stream(arr)
.filter(s -> s.contains("name1"))
.findFirst().orElse("") ;
add a comment |
up vote
1
down vote
accepted
In case you need to retrieve only the first found element:
String r = new String Arrays.stream(arr)
.filter(s -> s.contains("name1"))
.findFirst().orElse("") ;
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
In case you need to retrieve only the first found element:
String r = new String Arrays.stream(arr)
.filter(s -> s.contains("name1"))
.findFirst().orElse("") ;
In case you need to retrieve only the first found element:
String r = new String Arrays.stream(arr)
.filter(s -> s.contains("name1"))
.findFirst().orElse("") ;
edited Nov 10 at 22:21
marc_s
566k12610921245
566k12610921245
answered Nov 6 at 21:24
oleg.cherednik
4,3132916
4,3132916
add a comment |
add a comment |
up vote
1
down vote
You could map the first match
String array =
Arrays.stream(arr).filter
(s -> s.contains("name1")).findFirst().map(s -> new String s ).orElse(new String );
add a comment |
up vote
1
down vote
You could map the first match
String array =
Arrays.stream(arr).filter
(s -> s.contains("name1")).findFirst().map(s -> new String s ).orElse(new String );
add a comment |
up vote
1
down vote
up vote
1
down vote
You could map the first match
String array =
Arrays.stream(arr).filter
(s -> s.contains("name1")).findFirst().map(s -> new String s ).orElse(new String );
You could map the first match
String array =
Arrays.stream(arr).filter
(s -> s.contains("name1")).findFirst().map(s -> new String s ).orElse(new String );
edited Nov 6 at 21:50
answered Nov 6 at 21:00
Reimeus
140k10155221
140k10155221
add a comment |
add a comment |
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%2f53179779%2fhow-to-check-if-an-array-contains-a-partial-value-once%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
1
what's not working here? Post a Minimal, Complete, and Verifiable example
– Reimeus
Nov 6 at 20:55
@Reimeus Please check the updated post
– c.r
Nov 6 at 20:57