Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Tanuj Dua
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 53747
127
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tanuj Dua
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using group by? In Scala I would do something like:


In your case, it would be grouped(500)!
 
Tanuj Dua
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Scala I know its powerful and its there but can we do something in Java8.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tanuj Dua
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Junilu Lacar
Sheriff
Posts: 10878
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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-...?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Junilu Lacar
Sheriff
Posts: 10878
158
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 index and 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.
 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Shipp
Ranch Hand
Posts: 217
11
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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-
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!