# New To Java 8 | Need to sublist | Can I use Java 8 functional Programming

Tanuj Dua

Greenhorn

Posts: 5

posted 2 years ago

Hi all,

I have a real use case in one of the projects I am working upon. I have the list of tickers for which I need to pull out some information from the WebService and since WebService can only handle 500 tickers I have to break the list into multiple batches of size 500

The no of tickers are dynamic so it can be less than 500 and more that 500 as well :

TestCase1: No Of Tickers 1450

So it should be divided into 3 batches -

0 - 500 (1 List)

500 - 1000 (2nd List)

1000-1450 (3rd List)

TestCase1: No Of Tickers 200

So it should be divided into 1 batch -

0 - 200 (1 List)

Looks simple without functional programming as I have been doing this since 10 years . Can we write something efficient in Functional Programming.

I have a real use case in one of the projects I am working upon. I have the list of tickers for which I need to pull out some information from the WebService and since WebService can only handle 500 tickers I have to break the list into multiple batches of size 500

The no of tickers are dynamic so it can be less than 500 and more that 500 as well :

TestCase1: No Of Tickers 1450

So it should be divided into 3 batches -

0 - 500 (1 List)

500 - 1000 (2nd List)

1000-1450 (3rd List)

TestCase1: No Of Tickers 200

So it should be divided into 1 batch -

0 - 200 (1 List)

Looks simple without functional programming as I have been doing this since 10 years . Can we write something efficient in Functional Programming.

Campbell Ritchie

Marshal

Posts: 52578

119

posted 2 years ago

- 1

Welcome to the Ranch

Do you mean there is a List with 1450 tickets in, numbered randomly and you want to partition them into three Lists depending on their numbers? Yes, you use a Stream, filterBy, and collect the result to another List. If you simply want to partition the List sublist is probably simpler than a Stream, but you can use limit on a Stream.

Do you mean there is a List with 1450 tickets in, numbered randomly and you want to partition them into three Lists depending on their numbers? Yes, you use a Stream, filterBy, and collect the result to another List. If you simply want to partition the List sublist is probably simpler than a Stream, but you can use limit on a Stream.

Tanuj Dua

Greenhorn

Posts: 5

posted 2 years ago

1) Its a dynamic size list whose size can be anything.

2) But what I am surely need to do is to divide the list onto multiple sublists of size 500 . So it can be n sublists of each size 500 depending upon the total size of the original list. And list size not necessarily to be a multiple of 500

3) I want partitionaling by index and not the elements value . i.e. sublists should be like 0-500, 500-1000,1000-1500,1500-...

I tried looking the stream api but somehow not convinced to use it because I want partioning by index and not be list element value as it doesn't matter to me what the values are there in list.

2) But what I am surely need to do is to divide the list onto multiple sublists of size 500 . So it can be n sublists of each size 500 depending upon the total size of the original list. And list size not necessarily to be a multiple of 500

3) I want partitionaling by index and not the elements value . i.e. sublists should be like 0-500, 500-1000,1000-1500,1500-...

I tried looking the stream api but somehow not convinced to use it because I want partioning by index and not be list element value as it doesn't matter to me what the values are there in list.

posted 2 years ago

How about using group by? In Scala I would do something like:

In your case, it would be grouped(500)!

In your case, it would be grouped(500)!

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master

Did a rm -R / to find out that I lost my entire Linux installation!

Tanuj Dua

Greenhorn

Posts: 5

posted 2 years ago

There seems to be no equivalent of the scala grouped function in Java, at least that I'm not aware of. Looks like you have to implement one by yourself.

Did a rm -R / to find out that I lost my entire Linux installation!

Tanuj Dua

Greenhorn

Posts: 5

posted 2 years ago

May be to phrase it better, functional programming in Java is not mature enough like Scala!

Tanuj Dua wrote:Oops so does that mean I now need to go to non-functional way . So functional programming is good but can't solve all problems ?

May be to phrase it better, functional programming in Java is not mature enough like Scala!

Did a rm -R / to find out that I lost my entire Linux installation!

posted 2 years ago

So the elements 500, 1000, 1500, etc. each belong to two groups? Or did you really mean 0-500, 501-1000, 1001-1500, 1501-...? Or perhaps you meant 0-499, 500-999, 1000-1499, 1500-...?

Tanuj Dua wrote:

3) I want partitionaling by index and not the elements value . i.e. sublists should be like 0-500, 500-1000,1000-1500,1500-...

So the elements 500, 1000, 1500, etc. each belong to two groups? Or did you really mean 0-500, 501-1000, 1001-1500, 1501-...? Or perhaps you meant 0-499, 500-999, 1000-1499, 1500-...?

Junilu - [How to Ask Questions] [How to Answer Questions]

posted 2 years ago

Actually it was quite simple. The OP wanted to partition the list in chunks of 500!

Junilu Lacar wrote:Tanuj Dua wrote:

3) I want partitionaling by index and not the elements value . i.e. sublists should be like 0-500, 500-1000,1000-1500,1500-...

So the elements 500, 1000, 1500, etc. each belong to two groups? Or did you really mean 0-500, 501-1000, 1001-1500, 1501-...? Or perhaps you meant 0-499, 500-999, 1000-1499, 1500-...?

Actually it was quite simple. The OP wanted to partition the list in chunks of 500!

Did a rm -R / to find out that I lost my entire Linux installation!

posted 2 years ago

I know, Joe. However, the examples the OP gave contradict that requirement. Perhaps I should state this more directly and simply rather than being circumspect.

If you have a list of items with indices 0 - 500, that list will have 501 items in it. That is, the list will contain the item at index 0, plus items with indices from 1 to 500. That's 501 items. The same goes for a list of items with indices 500 - 1000. This is the first inconsistency.

Assuming the ranges stated are inclusive, then the list of items from index 0 to 500 and the list of items with index from 500 to 1000 have one element in common: the item with index of 500. This is the second inconsistency. Does the OP really want this?

If you want a correct program, you have to be precise in the specification. If I remove the inconsistencies, then the

1. Given a list, break it up into groups of 500 items (or N items)

2. The groups will be determined by the original list index. The first group will have items with index from 0 to N-1. The second group will have items with index N to 2N - 1. Third group will have items with index 2N to 3N - 1 and so on. The last group will have the remainder after all possible groups of N have been formed and may have less than N items in it.

Joe Harry wrote:Actually it was quite simple. The OP wanted to partition the list in chunks of 500!

I know, Joe. However, the examples the OP gave contradict that requirement. Perhaps I should state this more directly and simply rather than being circumspect.

TestCase1: No Of Tickers 1450

So it should be divided into 3 batches -

0 - 500 (1 List)

500 - 1000 (2nd List)

1000-1450 (3rd List)

...

I want partitionaling by indexand not the elements value . i.e. sublists should be like 0-500, 500-1000,1000-1500,1500-...

If you have a list of items with indices 0 - 500, that list will have 501 items in it. That is, the list will contain the item at index 0, plus items with indices from 1 to 500. That's 501 items. The same goes for a list of items with indices 500 - 1000. This is the first inconsistency.

Assuming the ranges stated are inclusive, then the list of items from index 0 to 500 and the list of items with index from 500 to 1000 have one element in common: the item with index of 500. This is the second inconsistency. Does the OP really want this?

If you want a correct program, you have to be precise in the specification. If I remove the inconsistencies, then the

**real**requirement would be:

1. Given a list, break it up into groups of 500 items (or N items)

2. The groups will be determined by the original list index. The first group will have items with index from 0 to N-1. The second group will have items with index N to 2N - 1. Third group will have items with index 2N to 3N - 1 and so on. The last group will have the remainder after all possible groups of N have been formed and may have less than N items in it.

Junilu - [How to Ask Questions] [How to Answer Questions]

posted 2 years ago

The default Spliterator will batch up your big huge list into batch sizes that follow this arithmetic progression e.g. 1024, 2048 etc...

In order to send chunks of 500, you can use this solution by Marko Topolnik.

In order to send chunks of 500, you can use this solution by Marko Topolnik.

Regards,

Pho

posted 2 years ago

Does Campbell's answer (List.subList) somehow not answer the question? http://docs.oracle.com/javase/8/docs/api/java/util/List.html#subList-int-int-