Reputation: 1919
I have following java object
Obj:
----
String id;
List<A>;
A:
--
String A_ID;
String A_PROPERTY;
What I am looking for is to be able to search in a given object. ex: search in a list where A.A_ID = 123
I need to dynamically pass
A_ID = 123
and it would give me
A_Property
I know I could search through a list through iterator, but is there already frameworks out there which lets you do this?
thanks
Upvotes: 1
Views: 1157
Reputation: 116334
lambdaj is a very nice library for Java 5.
For example:
Person me = new Person("Mario", "Fusco", 35);
Person luca = new Person("Luca", "Marrocco", 29);
Person biagio = new Person("Biagio", "Beatrice", 39);
Person celestino = new Person("Celestino", "Bellone", 29);
List<Person> people = asList(me, luca, biagio, celestino);
it is possible to filter the ones having more than 30 years applying the following filter:
List<Person> over30 = filter(having(on(Person.class).getAge(), greaterThan(30)), people);
Upvotes: 2
Reputation: 199225
Using a hashmap.
Map<String,A> map = new HashMap<String,A>();
A a = new A();
a.id = "123";
a.property="Hello there!";
map.put( a.id , a );
Later
map.get( "123"); // would return the instance of A and then a.property will return "Hello there!"
Upvotes: -1
Reputation: 3720
Hashmap if you only search by one property which has the same type for every inputed object. For example, a string. The object 1 has the string "Obj1" for key and the second object has the string "Test2" for key. When you use the get method with the parameter "Obj1", it will return the first object. P.S. It's really hard to read your "pseudo-code".
Upvotes: 0
Reputation: 189676
why do you need a framework? how about
public String lookupPropertyById(String id)
{
for (A a : myList)
{
if (id.equals(a.id))
return a.property;
}
return null; // not found
}
Upvotes: 0
Reputation: 7480
Something like Quaere. From their site:
Quaere is an open source, extensible framework that adds a querying syntax reminiscent of SQL to Java applications. Quaere allows you to filter, enumerate and create projections over a number of collections and other queryable resources using a common, expressive syntax.
Upvotes: 0