• 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
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

How to access a Stream<Tuple>?

 
Ranch Foreman
Posts: 74
2
  • Mark post as helpful
  • send pies
  • 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: 70997
291
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Saloon Keeper
Posts: 4140
160
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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 Foreman
Posts: 74
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much! I combined both solutions and it worked!


and then I get the value:
 
Marshal
Posts: 26090
71
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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: 70997
291
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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: 22037
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 22037
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • 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 Foreman
Posts: 74
2
  • Mark post as helpful
  • send pies
  • 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 Foreman
Posts: 74
2
  • Mark post as helpful
  • send pies
  • 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: 22037
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic