forming a specific list with Java 8 streams











up vote
8
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
    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















up vote
8
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
    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













up vote
8
down vote

favorite
1









up vote
8
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 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 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


















  • 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
















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












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
    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 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




    @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 using range.. API
    – nullpointer
    Nov 22 at 14:15


















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
    Nov 22 at 13:50











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
    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 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




    @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 using range.. API
    – nullpointer
    Nov 22 at 14:15















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
    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 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




    @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 using range.. API
    – nullpointer
    Nov 22 at 14:15













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 Nov 22 at 14:14

























answered Nov 22 at 13:44









nullpointer

35.7k1071141




35.7k1071141








  • 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










  • @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 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




    @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 using range.. API
    – nullpointer
    Nov 22 at 14:15














  • 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










  • @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 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




    @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 using range.. 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












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
    Nov 22 at 13:50















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
    Nov 22 at 13:50













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 Nov 22 at 12:35









Aomine

32.1k62754




32.1k62754








  • 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














  • 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








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


















 

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

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...