1

im trying to find ways how i can create a Map with certain property/variable values of a object. My english is far from being perfect but i will try to go into details.

Lets say we have a simple Java object with these three variables and their public getters and setters

Integer id
String name
String street

Now i will collect/put some of these Objects in a List or Set (lets assume that all variable values will be unqiue). What i want to do next is to build a Map with the id and name of all Objects in the List/Set. Of course i could just iterate the List everytime and build a Map but does anybody know a, lets say more elegant, solution? I ve seen that Java 8 is offering things like Collector's and/or Filter but im searching for a Java 7 solution. I also read apache commons BeanUtils could help (with using reflection under the hood) but i cant find a example for my use case so far...

As a additional info: I m not allowoed to add a new ext lib like a google/guava :-/ BeanUtils (and some other commons) would exist already...

Does anybody have a idea?

StephanM
  • 1,350
  • 2
  • 24
  • 50

0 Answers0