forming a specific list with Java 8 streams
up vote
8
down vote
favorite
I'am currently working in a java project which I have a list of strings and I want them to have a specific format using streams .
For example
Input : [nom, contains, b, and, prenom, contains, y, and, age, >=, 1, and, age, <=, 100]
Ouput:
[
{key:"nom",
operation:"contains",
value:"b"
},
{
key:"prenom",
operation:"contains",
value:"y"
},
{
key:"age",
operation:">=",
value: 1
},
{
key:"age",
operation:"<=",
value: 1000
}]
I wrote a very basic code without using streams:
List filter = [nom, contains, b, and, prenom, contains, y, and, age, >=, 1, and, age, <=, 100]
List<SearchCriteria> formedFilter = new ArrayList<>();
SearchCriteria sc = new SearchCriteria();
if(filter != null){
for(int i = 0 ;i< filter.size();i++){
if(i % 4 ==0){
sc.setKey((String) filter.get(i));
}else if(i % 4 == 1){
sc.setOperation((String) filter.get(i));
}else if(i % 4 ==2){
sc.setValue(filter.get(i));
formedFilter.add(sc);
}else{
sc = new SearchCriteria();
}
}
}
SearchCriteria Class
public class SearchCriteria {
private String key;
private String operation;
private Object value;
public SearchCriteria() {
}
public SearchCriteria(String key, String operation, Object value) {
this.key = key;
this.operation = operation;
this.value = value;
}
// getters and setters
}
java java-8 java-stream
add a comment |
up vote
8
down vote
favorite
I'am currently working in a java project which I have a list of strings and I want them to have a specific format using streams .
For example
Input : [nom, contains, b, and, prenom, contains, y, and, age, >=, 1, and, age, <=, 100]
Ouput:
[
{key:"nom",
operation:"contains",
value:"b"
},
{
key:"prenom",
operation:"contains",
value:"y"
},
{
key:"age",
operation:">=",
value: 1
},
{
key:"age",
operation:"<=",
value: 1000
}]
I wrote a very basic code without using streams:
List filter = [nom, contains, b, and, prenom, contains, y, and, age, >=, 1, and, age, <=, 100]
List<SearchCriteria> formedFilter = new ArrayList<>();
SearchCriteria sc = new SearchCriteria();
if(filter != null){
for(int i = 0 ;i< filter.size();i++){
if(i % 4 ==0){
sc.setKey((String) filter.get(i));
}else if(i % 4 == 1){
sc.setOperation((String) filter.get(i));
}else if(i % 4 ==2){
sc.setValue(filter.get(i));
formedFilter.add(sc);
}else{
sc = new SearchCriteria();
}
}
}
SearchCriteria Class
public class SearchCriteria {
private String key;
private String operation;
private Object value;
public SearchCriteria() {
}
public SearchCriteria(String key, String operation, Object value) {
this.key = key;
this.operation = operation;
this.value = value;
}
// getters and setters
}
java java-8 java-stream
Are the elements at index 3, 7 and 11 alwaysand
, or could they also be i.e.or
, or maybe another boolean operation?
– Federico Peralta Schaffner
Nov 22 at 12:38
2
@FedericoPeraltaSchaffner they could beand / or
but for the moment i don't need it
– Wassim Makni
Nov 22 at 13:24
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I'am currently working in a java project which I have a list of strings and I want them to have a specific format using streams .
For example
Input : [nom, contains, b, and, prenom, contains, y, and, age, >=, 1, and, age, <=, 100]
Ouput:
[
{key:"nom",
operation:"contains",
value:"b"
},
{
key:"prenom",
operation:"contains",
value:"y"
},
{
key:"age",
operation:">=",
value: 1
},
{
key:"age",
operation:"<=",
value: 1000
}]
I wrote a very basic code without using streams:
List filter = [nom, contains, b, and, prenom, contains, y, and, age, >=, 1, and, age, <=, 100]
List<SearchCriteria> formedFilter = new ArrayList<>();
SearchCriteria sc = new SearchCriteria();
if(filter != null){
for(int i = 0 ;i< filter.size();i++){
if(i % 4 ==0){
sc.setKey((String) filter.get(i));
}else if(i % 4 == 1){
sc.setOperation((String) filter.get(i));
}else if(i % 4 ==2){
sc.setValue(filter.get(i));
formedFilter.add(sc);
}else{
sc = new SearchCriteria();
}
}
}
SearchCriteria Class
public class SearchCriteria {
private String key;
private String operation;
private Object value;
public SearchCriteria() {
}
public SearchCriteria(String key, String operation, Object value) {
this.key = key;
this.operation = operation;
this.value = value;
}
// getters and setters
}
java java-8 java-stream
I'am currently working in a java project which I have a list of strings and I want them to have a specific format using streams .
For example
Input : [nom, contains, b, and, prenom, contains, y, and, age, >=, 1, and, age, <=, 100]
Ouput:
[
{key:"nom",
operation:"contains",
value:"b"
},
{
key:"prenom",
operation:"contains",
value:"y"
},
{
key:"age",
operation:">=",
value: 1
},
{
key:"age",
operation:"<=",
value: 1000
}]
I wrote a very basic code without using streams:
List filter = [nom, contains, b, and, prenom, contains, y, and, age, >=, 1, and, age, <=, 100]
List<SearchCriteria> formedFilter = new ArrayList<>();
SearchCriteria sc = new SearchCriteria();
if(filter != null){
for(int i = 0 ;i< filter.size();i++){
if(i % 4 ==0){
sc.setKey((String) filter.get(i));
}else if(i % 4 == 1){
sc.setOperation((String) filter.get(i));
}else if(i % 4 ==2){
sc.setValue(filter.get(i));
formedFilter.add(sc);
}else{
sc = new SearchCriteria();
}
}
}
SearchCriteria Class
public class SearchCriteria {
private String key;
private String operation;
private Object value;
public SearchCriteria() {
}
public SearchCriteria(String key, String operation, Object value) {
this.key = key;
this.operation = operation;
this.value = value;
}
// getters and setters
}
java java-8 java-stream
java java-8 java-stream
edited Nov 22 at 13:47
nullpointer
35.7k1071141
35.7k1071141
asked Nov 22 at 11:50
Wassim Makni
176113
176113
Are the elements at index 3, 7 and 11 alwaysand
, or could they also be i.e.or
, or maybe another boolean operation?
– Federico Peralta Schaffner
Nov 22 at 12:38
2
@FedericoPeraltaSchaffner they could beand / or
but for the moment i don't need it
– Wassim Makni
Nov 22 at 13:24
add a comment |
Are the elements at index 3, 7 and 11 alwaysand
, or could they also be i.e.or
, or maybe another boolean operation?
– Federico Peralta Schaffner
Nov 22 at 12:38
2
@FedericoPeraltaSchaffner they could beand / or
but for the moment i don't need it
– Wassim Makni
Nov 22 at 13:24
Are the elements at index 3, 7 and 11 always
and
, or could they also be i.e. or
, or maybe another boolean operation?– Federico Peralta Schaffner
Nov 22 at 12:38
Are the elements at index 3, 7 and 11 always
and
, or could they also be i.e. or
, or maybe another boolean operation?– Federico Peralta Schaffner
Nov 22 at 12:38
2
2
@FedericoPeraltaSchaffner they could be
and / or
but for the moment i don't need it– Wassim Makni
Nov 22 at 13:24
@FedericoPeraltaSchaffner they could be
and / or
but for the moment i don't need it– Wassim Makni
Nov 22 at 13:24
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
For a Java 8 version of Aomine's answer you can use:
List<SearchCriteria> formedFilter = IntStream.iterate(0, i -> i + 4)
.limit(filter.size() / 4 + 1) // + 1 to consider the last group as well
.mapToObj(i -> new SearchCriteria(filter.get(i), filter.get(i + 1), filter.get(i + 2)))
.collect(Collectors.toList());
Alternatively, similar to a suggestion by Holger, you can use the rangeClosed
API from IntStream as:
List<SearchCriteria> formedFilter2 = IntStream.rangeClosed(0, filter.size() / 4)
.map(i -> i * 4)
.mapToObj(i -> new SearchCriteria(filter.get(i), filter.get(i + 1), filter.get(i + 2)))
.collect(Collectors.toList());
2
By the way, since the placement oflimit
is irrelevant here, I’d chain it immediately toiterate
, as that’s semantically closer and makes it easier to understand the iteration logic.
– Holger
Nov 22 at 14:01
@Holger I just kept the short-circuiting closer to the terminal operation and the order wouldn't really matter here is what my understanding is.
– nullpointer
Nov 22 at 14:04
1
@nullpointer right, as I said, the placement oflimit
is irrelevant here, so it’s just a matter of style. I see your point in emphasizing the result list’s size, but still think, the iteration logic is more important (consider the appearance of the4
).
– Holger
Nov 22 at 14:08
2
@nullpointer I think that replacing.limit(filter.size() / 4 + 1)
with.limit(filter.size() / 4 + 1)
do the job correctly
– Wassim Makni
Nov 22 at 14:14
2
@WassimMakni true, also a cleaner way could be as suggested by Holger as well usingrange..
API
– nullpointer
Nov 22 at 14:15
|
show 1 more comment
up vote
2
down vote
Using JDK 9, you can do:
IntStream.iterate(0, i -> i < source.size(), i -> i + 4)
.mapToObj(x -> new SearchCriteria(source.get(x), source.get(x + 1), source.get(x + 2)))
.collect(toList());
1
@WassimMakni it’s not working for Java 8, but you can replaceiterate(0, i -> i < source.size(), i -> i + 4)
withrange(0, source.size()/4).map(i -> i*4)
, which might even turn out to be more efficient.
– Holger
Nov 22 at 13:50
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
For a Java 8 version of Aomine's answer you can use:
List<SearchCriteria> formedFilter = IntStream.iterate(0, i -> i + 4)
.limit(filter.size() / 4 + 1) // + 1 to consider the last group as well
.mapToObj(i -> new SearchCriteria(filter.get(i), filter.get(i + 1), filter.get(i + 2)))
.collect(Collectors.toList());
Alternatively, similar to a suggestion by Holger, you can use the rangeClosed
API from IntStream as:
List<SearchCriteria> formedFilter2 = IntStream.rangeClosed(0, filter.size() / 4)
.map(i -> i * 4)
.mapToObj(i -> new SearchCriteria(filter.get(i), filter.get(i + 1), filter.get(i + 2)))
.collect(Collectors.toList());
2
By the way, since the placement oflimit
is irrelevant here, I’d chain it immediately toiterate
, as that’s semantically closer and makes it easier to understand the iteration logic.
– Holger
Nov 22 at 14:01
@Holger I just kept the short-circuiting closer to the terminal operation and the order wouldn't really matter here is what my understanding is.
– nullpointer
Nov 22 at 14:04
1
@nullpointer right, as I said, the placement oflimit
is irrelevant here, so it’s just a matter of style. I see your point in emphasizing the result list’s size, but still think, the iteration logic is more important (consider the appearance of the4
).
– Holger
Nov 22 at 14:08
2
@nullpointer I think that replacing.limit(filter.size() / 4 + 1)
with.limit(filter.size() / 4 + 1)
do the job correctly
– Wassim Makni
Nov 22 at 14:14
2
@WassimMakni true, also a cleaner way could be as suggested by Holger as well usingrange..
API
– nullpointer
Nov 22 at 14:15
|
show 1 more comment
up vote
5
down vote
accepted
For a Java 8 version of Aomine's answer you can use:
List<SearchCriteria> formedFilter = IntStream.iterate(0, i -> i + 4)
.limit(filter.size() / 4 + 1) // + 1 to consider the last group as well
.mapToObj(i -> new SearchCriteria(filter.get(i), filter.get(i + 1), filter.get(i + 2)))
.collect(Collectors.toList());
Alternatively, similar to a suggestion by Holger, you can use the rangeClosed
API from IntStream as:
List<SearchCriteria> formedFilter2 = IntStream.rangeClosed(0, filter.size() / 4)
.map(i -> i * 4)
.mapToObj(i -> new SearchCriteria(filter.get(i), filter.get(i + 1), filter.get(i + 2)))
.collect(Collectors.toList());
2
By the way, since the placement oflimit
is irrelevant here, I’d chain it immediately toiterate
, as that’s semantically closer and makes it easier to understand the iteration logic.
– Holger
Nov 22 at 14:01
@Holger I just kept the short-circuiting closer to the terminal operation and the order wouldn't really matter here is what my understanding is.
– nullpointer
Nov 22 at 14:04
1
@nullpointer right, as I said, the placement oflimit
is irrelevant here, so it’s just a matter of style. I see your point in emphasizing the result list’s size, but still think, the iteration logic is more important (consider the appearance of the4
).
– Holger
Nov 22 at 14:08
2
@nullpointer I think that replacing.limit(filter.size() / 4 + 1)
with.limit(filter.size() / 4 + 1)
do the job correctly
– Wassim Makni
Nov 22 at 14:14
2
@WassimMakni true, also a cleaner way could be as suggested by Holger as well usingrange..
API
– nullpointer
Nov 22 at 14:15
|
show 1 more comment
up vote
5
down vote
accepted
up vote
5
down vote
accepted
For a Java 8 version of Aomine's answer you can use:
List<SearchCriteria> formedFilter = IntStream.iterate(0, i -> i + 4)
.limit(filter.size() / 4 + 1) // + 1 to consider the last group as well
.mapToObj(i -> new SearchCriteria(filter.get(i), filter.get(i + 1), filter.get(i + 2)))
.collect(Collectors.toList());
Alternatively, similar to a suggestion by Holger, you can use the rangeClosed
API from IntStream as:
List<SearchCriteria> formedFilter2 = IntStream.rangeClosed(0, filter.size() / 4)
.map(i -> i * 4)
.mapToObj(i -> new SearchCriteria(filter.get(i), filter.get(i + 1), filter.get(i + 2)))
.collect(Collectors.toList());
For a Java 8 version of Aomine's answer you can use:
List<SearchCriteria> formedFilter = IntStream.iterate(0, i -> i + 4)
.limit(filter.size() / 4 + 1) // + 1 to consider the last group as well
.mapToObj(i -> new SearchCriteria(filter.get(i), filter.get(i + 1), filter.get(i + 2)))
.collect(Collectors.toList());
Alternatively, similar to a suggestion by Holger, you can use the rangeClosed
API from IntStream as:
List<SearchCriteria> formedFilter2 = IntStream.rangeClosed(0, filter.size() / 4)
.map(i -> i * 4)
.mapToObj(i -> new SearchCriteria(filter.get(i), filter.get(i + 1), filter.get(i + 2)))
.collect(Collectors.toList());
edited Nov 22 at 14:14
answered Nov 22 at 13:44
nullpointer
35.7k1071141
35.7k1071141
2
By the way, since the placement oflimit
is irrelevant here, I’d chain it immediately toiterate
, as that’s semantically closer and makes it easier to understand the iteration logic.
– Holger
Nov 22 at 14:01
@Holger I just kept the short-circuiting closer to the terminal operation and the order wouldn't really matter here is what my understanding is.
– nullpointer
Nov 22 at 14:04
1
@nullpointer right, as I said, the placement oflimit
is irrelevant here, so it’s just a matter of style. I see your point in emphasizing the result list’s size, but still think, the iteration logic is more important (consider the appearance of the4
).
– Holger
Nov 22 at 14:08
2
@nullpointer I think that replacing.limit(filter.size() / 4 + 1)
with.limit(filter.size() / 4 + 1)
do the job correctly
– Wassim Makni
Nov 22 at 14:14
2
@WassimMakni true, also a cleaner way could be as suggested by Holger as well usingrange..
API
– nullpointer
Nov 22 at 14:15
|
show 1 more comment
2
By the way, since the placement oflimit
is irrelevant here, I’d chain it immediately toiterate
, as that’s semantically closer and makes it easier to understand the iteration logic.
– Holger
Nov 22 at 14:01
@Holger I just kept the short-circuiting closer to the terminal operation and the order wouldn't really matter here is what my understanding is.
– nullpointer
Nov 22 at 14:04
1
@nullpointer right, as I said, the placement oflimit
is irrelevant here, so it’s just a matter of style. I see your point in emphasizing the result list’s size, but still think, the iteration logic is more important (consider the appearance of the4
).
– Holger
Nov 22 at 14:08
2
@nullpointer I think that replacing.limit(filter.size() / 4 + 1)
with.limit(filter.size() / 4 + 1)
do the job correctly
– Wassim Makni
Nov 22 at 14:14
2
@WassimMakni true, also a cleaner way could be as suggested by Holger as well usingrange..
API
– nullpointer
Nov 22 at 14:15
2
2
By the way, since the placement of
limit
is irrelevant here, I’d chain it immediately to iterate
, as that’s semantically closer and makes it easier to understand the iteration logic.– Holger
Nov 22 at 14:01
By the way, since the placement of
limit
is irrelevant here, I’d chain it immediately to iterate
, as that’s semantically closer and makes it easier to understand the iteration logic.– Holger
Nov 22 at 14:01
@Holger I just kept the short-circuiting closer to the terminal operation and the order wouldn't really matter here is what my understanding is.
– nullpointer
Nov 22 at 14:04
@Holger I just kept the short-circuiting closer to the terminal operation and the order wouldn't really matter here is what my understanding is.
– nullpointer
Nov 22 at 14:04
1
1
@nullpointer right, as I said, the placement of
limit
is irrelevant here, so it’s just a matter of style. I see your point in emphasizing the result list’s size, but still think, the iteration logic is more important (consider the appearance of the 4
).– Holger
Nov 22 at 14:08
@nullpointer right, as I said, the placement of
limit
is irrelevant here, so it’s just a matter of style. I see your point in emphasizing the result list’s size, but still think, the iteration logic is more important (consider the appearance of the 4
).– Holger
Nov 22 at 14:08
2
2
@nullpointer I think that replacing
.limit(filter.size() / 4 + 1)
with .limit(filter.size() / 4 + 1)
do the job correctly– Wassim Makni
Nov 22 at 14:14
@nullpointer I think that replacing
.limit(filter.size() / 4 + 1)
with .limit(filter.size() / 4 + 1)
do the job correctly– Wassim Makni
Nov 22 at 14:14
2
2
@WassimMakni true, also a cleaner way could be as suggested by Holger as well using
range..
API– nullpointer
Nov 22 at 14:15
@WassimMakni true, also a cleaner way could be as suggested by Holger as well using
range..
API– nullpointer
Nov 22 at 14:15
|
show 1 more comment
up vote
2
down vote
Using JDK 9, you can do:
IntStream.iterate(0, i -> i < source.size(), i -> i + 4)
.mapToObj(x -> new SearchCriteria(source.get(x), source.get(x + 1), source.get(x + 2)))
.collect(toList());
1
@WassimMakni it’s not working for Java 8, but you can replaceiterate(0, i -> i < source.size(), i -> i + 4)
withrange(0, source.size()/4).map(i -> i*4)
, which might even turn out to be more efficient.
– Holger
Nov 22 at 13:50
add a comment |
up vote
2
down vote
Using JDK 9, you can do:
IntStream.iterate(0, i -> i < source.size(), i -> i + 4)
.mapToObj(x -> new SearchCriteria(source.get(x), source.get(x + 1), source.get(x + 2)))
.collect(toList());
1
@WassimMakni it’s not working for Java 8, but you can replaceiterate(0, i -> i < source.size(), i -> i + 4)
withrange(0, source.size()/4).map(i -> i*4)
, which might even turn out to be more efficient.
– Holger
Nov 22 at 13:50
add a comment |
up vote
2
down vote
up vote
2
down vote
Using JDK 9, you can do:
IntStream.iterate(0, i -> i < source.size(), i -> i + 4)
.mapToObj(x -> new SearchCriteria(source.get(x), source.get(x + 1), source.get(x + 2)))
.collect(toList());
Using JDK 9, you can do:
IntStream.iterate(0, i -> i < source.size(), i -> i + 4)
.mapToObj(x -> new SearchCriteria(source.get(x), source.get(x + 1), source.get(x + 2)))
.collect(toList());
answered Nov 22 at 12:35
Aomine
32.1k62754
32.1k62754
1
@WassimMakni it’s not working for Java 8, but you can replaceiterate(0, i -> i < source.size(), i -> i + 4)
withrange(0, source.size()/4).map(i -> i*4)
, which might even turn out to be more efficient.
– Holger
Nov 22 at 13:50
add a comment |
1
@WassimMakni it’s not working for Java 8, but you can replaceiterate(0, i -> i < source.size(), i -> i + 4)
withrange(0, source.size()/4).map(i -> i*4)
, which might even turn out to be more efficient.
– Holger
Nov 22 at 13:50
1
1
@WassimMakni it’s not working for Java 8, but you can replace
iterate(0, i -> i < source.size(), i -> i + 4)
with range(0, source.size()/4).map(i -> i*4)
, which might even turn out to be more efficient.– Holger
Nov 22 at 13:50
@WassimMakni it’s not working for Java 8, but you can replace
iterate(0, i -> i < source.size(), i -> i + 4)
with range(0, source.size()/4).map(i -> i*4)
, which might even turn out to be more efficient.– Holger
Nov 22 at 13:50
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%2f53430409%2fforming-a-specific-list-with-java-8-streams%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
Are the elements at index 3, 7 and 11 always
and
, or could they also be i.e.or
, or maybe another boolean operation?– Federico Peralta Schaffner
Nov 22 at 12:38
2
@FedericoPeraltaSchaffner they could be
and / or
but for the moment i don't need it– Wassim Makni
Nov 22 at 13:24