Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

How to access a Stream<Tuple>?

 
Ranch Hand
Posts: 74
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Stream<Tuple> that outputs something like:

I'm wondering how I can get the value for Index?

So I could get an integer returned that equals 24.

Thanks so much!
 
Marshal
Posts: 71699
312
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that a Stream or a single [Sex]Tuple[t] object? In the case of Streams, let's imagine you have a List<Sextuplet>:-Line 1 creates a Stream<Sextuplet>. I haven't filtered the Stream or selected any of its elements or anything. Simply use all elements.
Line 2 uses the method reference to the getIndex() method, which I presume it has, to get it as an int and the Stream creates an IntStream.
Line 3: The IntStream#boxed() method turns the ints to Integers and creates a Stream<Integer>.
Line 4: You can use a Collector as an argument to the collect() method, but it is easier to go to the Collectors class, which has a method supplying just what you want.
 
Bartender
Posts: 4272
160
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Almost. The link to the API is given here: Tuple

So, instead of the method reference: Tuple::getIndex, it should be or
 
Glenda Karen
Ranch Hand
Posts: 74
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much! I combined both solutions and it worked!


and then I get the value:
 
Marshal
Posts: 26291
80
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Stream class has a findFirst() method which might be useful, rather than making a List and extracting its first entry.
 
Campbell Ritchie
Marshal
Posts: 71699
312
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Glenda Karen wrote:Thanks so much!

That's a pleasure: and well done getting it to work.

. . . .getString("A").equals(-3.82)). . . .

That use of equals() looks dubious to me; maybe you have missed out some quote marks.
 
Sheriff
Posts: 22055
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Glenda Karen wrote:


You can make that slightly shorter by moving the boxing into the mapping, where the double is auto-boxed.
 
Rob Spoor
Sheriff
Posts: 22055
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

. . . .getString("A").equals(-3.82)). . . .

That use of equals() looks dubious to me; maybe you have missed out some quote marks.


Campbell's right. That expression always returns false, because a String never equals a Double. Your list will be empty.
 
Glenda Karen
Ranch Hand
Posts: 74
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That use of equals() looks dubious to me; maybe you have missed out some quote marks.



Yes sorry, I had to convert to a string and then was able to use it.
 
Glenda Karen
Ranch Hand
Posts: 74
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:

Glenda Karen wrote:


You can make that slightly shorter by moving the boxing into the mapping, where the double is auto-boxed.


Thanks so much, that worked as well!
 
Rob Spoor
Sheriff
Posts: 22055
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
Are you okay? You look a little big. Maybe this tiny ad will help:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic