• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Surodip Patra
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
 
Pankaj Kr. Singh
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
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 20820
68
Chrome Eclipse IDE Java 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.
 
Cay Horstmann
author
Ranch Hand
Posts: 172
15
  • 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.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!