• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to get array of an specific attribute values from a list of object in java 7/8

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

I have a question.
Suppose I have a List<SomeClass> and there is an attribute named "SomeAttribute" in "SomeClass". Now is there any way to get all the "SomeAttribute" values from List<SomeClass> as an array using reflection, collection api or lambda expression?

Thanks,
Surodip
 
Ranch Hand
Posts: 32
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

you can use as follows
 
Surodip Patra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you are right Pankaj. Thanks for your reply. But again here I have to execute a loop, which I don't want.

Surodip
 
Sheriff
Posts: 21758
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Raymond Tong wrote:For Java 8 Lambda, you may use map.
http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#approach9
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html

For Java Guava library, you may use transform
http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Collections2.html#transform(java.util.Collection, com.google.common.base.Function)

But those only give you Stream and Collection.


I think that using Stream's collect method(s) you can get a List back. Collectors.toList() should give you exactly the Collector required for this.

In short (untested):
 
Surodip Patra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I have found the solution:

Collection<String> names = CollectionUtils.collect(list, TransformerUtils.invokerTransformer("getName"));

Here list is the list of someclass objects, getname is the getter method name of attribute "name" of someclass.

Apache commons collections api is used here.

Remember you have to give the getter method name of the attribute.

Suppose I have a class User:

Class User{
private String firstname;
private String lastname;

public String getFirstname(){
return this.firstname;
}

public String getlastname(){
return this.lastname;
}

...

}

Now if I have a userlist which is List<User>and I want to get all the values of firstname from the list, I can simply get it by:

Collection<String> firstnames = CollectionUtils.collect(userlist, TransformerUtils.invokerTransformer("getFirstname"));

Thanks,
Surodip
 
Rob Spoor
Sheriff
Posts: 21758
102
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume that's CollectionUtils from Apache Commons Collections?
 
Surodip Patra
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:I assume that's CollectionUtils from Apache Commons Collections?


Yes.
 
author
Posts: 254
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't require the field lookup to be dynamic, you can just use a method expression, like this:



I use Locale because it's easy to get a bunch of interesting objects. Turn into a stream, and then map the method expression. That gives a stream of the attribute values. The last line prints the all. Or you can collect them into a list.

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!