Say I have an list of objects with two fields field1
and field2
, both of String type.
How do I get a list of all field1
values wit
No, you can't. You have to traverse the whole list and get each "field1" value from each object.
Without Iterating this is not possible .You can reduce iteration somehow but it is must.
... whether your questions refers to avoiding iterating over the collection
either:
Concretely, do you mean:
See below for solutions and options.
If it's the first one, then look at Google Guava, LambdaJ, FunctionalJava or other libraries that implement basic functional constructs and will allow you to do what you want in a few expressive calls. But keep in mind these do what is says on the tin: they will filter, collect or transform a collection, and will iterate through its elements to do this.
For instance:
Google Guava:
Set<String> strings = buildSetStrings();
Collection<String> filteredStrings =
Collections2.filter(strings, Predicates.containsPattern("^J"));
Functional Java:
Array<Integer> a = array(97, 44, 67, 3, 22, 90, 1, 77, 98, 1078, 6, 64, 6, 79, 42);
Array<Integer> b = a.filter(even);
LambdaJ:
List<Integer> biggerThan3 = filter(greaterThan(3), asList(1, 2, 3, 4, 5));
If it's the second one, this is not possible as-is, except if you architectured everything from the start so that your objects should be managed by a custom collection class that would indexing your objects based on their field values on insertion.
It would keep them in buckets indexed by said value to be readily available for you to retrieve them as a list or set on demand.
As mentioned in the comments below dounyy's answer, designing such a custom collection would probably have an impact on the API of the elements it would accept (most likely by defining a super interface to use for element types), or would require a fairly intricate implementation to resolve members dynamically (most likely by using reflection), if you ever wanted this collection to be generic.
An object is a reference towards a memory address. Then, the fields of this objects are other references towards other memory addresses. Hence, a list of objects is a list of references. So, it's impossible for the list to direclty access the object fields (references given by the references). The short answer is no.
Note: anyway you'll find an API that does what you want, it still loops in the inside.
Yes!! Its possible. But you have to do some crooked work.
Create a inner class with required variables. Eg: here am going to use 2 variables.( TNAME and TABTYPE).
public class storeTables {
private String tablename = "";
private String tabletype = "";
public storeTables(String a, String b)
{
this.setTablename(a);
this.setTabletype(b);
}
public void setTablename(String tablename) {
this.tablename = tablename;
}
public String getTablename() {
return tablename;
}
public void setTabletype(String tabletype) {
this.tabletype = tabletype;
}
public String getTabletype() {
return tabletype;
}}
Create a List of the inner class created above and dont forget to encapsulate it.
private List<storeTables> objlist= new ArrayList<storeTables>();
Get the value stored to the list as a inner class object.
String Query="SELECT * FROM TAB";
while(rs.next())
{
String tname=rs.getString("TNAME");
String tabtype=rs.getString("TABTYPE");
getObjlist().add(new storeTables(tname,tabtype));
}
create a DATAMODEL and push the list to the datamodel
private DataModel selectableItems= new ListDataModel(objlist);
get the warpped data to another list.
List<storeTables> items= (List<storeTables>)selectableItems.getWrappedData();
Finally!!! printing the data.
for(storeTables item:items){
System.out.println(item.getTablename());
}
TADA !!!! it will print only the tablename and not the tabtype ;) Believe that there is nothing impossible in java Any doubts!!
Maybe you could create an hashmap in this way:
HashMap<String, ArrayList<Object>> map;
The key would be the field. Doing this way, when you ask the HashMap to retrieve you the object corresponding to the field that you want, the map would return you an ArrayList cointaing all of the elements which has that field.