forming a specific list with Java 8 streams











up vote
9
down vote

favorite
1












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
}









share|improve this question
























  • 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
    13 hours ago






  • 2




    @FedericoPeraltaSchaffner they could be and / or but for the moment i don't need it
    – Wassim Makni
    12 hours ago















up vote
9
down vote

favorite
1












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
}









share|improve this question
























  • 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
    13 hours ago






  • 2




    @FedericoPeraltaSchaffner they could be and / or but for the moment i don't need it
    – Wassim Makni
    12 hours ago













up vote
9
down vote

favorite
1









up vote
9
down vote

favorite
1






1





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
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 12 hours ago









nullpointer

35k1070140




35k1070140










asked 14 hours ago









Wassim Makni

178113




178113












  • 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
    13 hours ago






  • 2




    @FedericoPeraltaSchaffner they could be and / or but for the moment i don't need it
    – Wassim Makni
    12 hours ago


















  • 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
    13 hours ago






  • 2




    @FedericoPeraltaSchaffner they could be and / or but for the moment i don't need it
    – Wassim Makni
    12 hours ago
















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
13 hours ago




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
13 hours ago




2




2




@FedericoPeraltaSchaffner they could be and / or but for the moment i don't need it
– Wassim Makni
12 hours ago




@FedericoPeraltaSchaffner they could be and / or but for the moment i don't need it
– Wassim Makni
12 hours ago












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());





share|improve this answer



















  • 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
    12 hours ago










  • @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
    11 hours ago






  • 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
    11 hours ago






  • 2




    @nullpointer I think that replacing .limit(filter.size() / 4 + 1) with .limit(filter.size() / 4 + 1) do the job correctly
    – Wassim Makni
    11 hours ago






  • 2




    @WassimMakni true, also a cleaner way could be as suggested by Holger as well using range.. API
    – nullpointer
    11 hours ago


















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());





share|improve this answer

















  • 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
    12 hours ago











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',
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
});


}
});














 

draft saved


draft discarded


















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

























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());





share|improve this answer



















  • 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
    12 hours ago










  • @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
    11 hours ago






  • 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
    11 hours ago






  • 2




    @nullpointer I think that replacing .limit(filter.size() / 4 + 1) with .limit(filter.size() / 4 + 1) do the job correctly
    – Wassim Makni
    11 hours ago






  • 2




    @WassimMakni true, also a cleaner way could be as suggested by Holger as well using range.. API
    – nullpointer
    11 hours ago















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());





share|improve this answer



















  • 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
    12 hours ago










  • @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
    11 hours ago






  • 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
    11 hours ago






  • 2




    @nullpointer I think that replacing .limit(filter.size() / 4 + 1) with .limit(filter.size() / 4 + 1) do the job correctly
    – Wassim Makni
    11 hours ago






  • 2




    @WassimMakni true, also a cleaner way could be as suggested by Holger as well using range.. API
    – nullpointer
    11 hours ago













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());





share|improve this answer














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());






share|improve this answer














share|improve this answer



share|improve this answer








edited 11 hours ago

























answered 12 hours ago









nullpointer

35k1070140




35k1070140








  • 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
    12 hours ago










  • @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
    11 hours ago






  • 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
    11 hours ago






  • 2




    @nullpointer I think that replacing .limit(filter.size() / 4 + 1) with .limit(filter.size() / 4 + 1) do the job correctly
    – Wassim Makni
    11 hours ago






  • 2




    @WassimMakni true, also a cleaner way could be as suggested by Holger as well using range.. API
    – nullpointer
    11 hours ago














  • 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
    12 hours ago










  • @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
    11 hours ago






  • 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
    11 hours ago






  • 2




    @nullpointer I think that replacing .limit(filter.size() / 4 + 1) with .limit(filter.size() / 4 + 1) do the job correctly
    – Wassim Makni
    11 hours ago






  • 2




    @WassimMakni true, also a cleaner way could be as suggested by Holger as well using range.. API
    – nullpointer
    11 hours ago








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
12 hours ago




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
12 hours ago












@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
11 hours ago




@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
11 hours ago




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
11 hours ago




@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
11 hours ago




2




2




@nullpointer I think that replacing .limit(filter.size() / 4 + 1) with .limit(filter.size() / 4 + 1) do the job correctly
– Wassim Makni
11 hours ago




@nullpointer I think that replacing .limit(filter.size() / 4 + 1) with .limit(filter.size() / 4 + 1) do the job correctly
– Wassim Makni
11 hours ago




2




2




@WassimMakni true, also a cleaner way could be as suggested by Holger as well using range.. API
– nullpointer
11 hours ago




@WassimMakni true, also a cleaner way could be as suggested by Holger as well using range.. API
– nullpointer
11 hours ago












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());





share|improve this answer

















  • 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
    12 hours ago















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());





share|improve this answer

















  • 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
    12 hours ago













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());





share|improve this answer












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());






share|improve this answer












share|improve this answer



share|improve this answer










answered 13 hours ago









Aomine

31.7k52754




31.7k52754








  • 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
    12 hours ago














  • 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
    12 hours ago








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
12 hours ago




@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
12 hours ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno