Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get datas from List<Object> (Java)?

Tags:

java

I`m new in Java and have a problem with showing data from a list of objects. I have a simple method, which should collect data across multiple tables and return it to my controller:

public List<Object> getHouseInfo(){
Query q = em.createNativeQuery("SELECT houses.id, addresses.country, addresses.region, house_details.rooms, house_details.square FROM houses, addresses, house_details");
List<Object> myList = q.getResultList(); 
return myList;}

Now I want to get this data in controller, but I don`t know how to get single results from the list. I tried to do something like this:

List<Object> list = getHouseInfo();
for (int i=0; i<list.size; i++){
System.out.println("Element "+i+list.get(0));}

but I`m getting only references to this objects (for example [Ljava.lang.Object;@167a47b). I also tried to use Iterator, but the result is the same. I tried to use a code like this:

List<Object> list = getHouseInfo();
for (int i=0; i<list.size; i++){
System.out.println("Element "+i+list.get(0)[0]);}

but it doesn`t help me too - this ends with a compile error.

Can someone tell me how to get an 'id'(integer value) from this list? I`m using MyFaces in my 'View' where I have a code like this (houseControll is the name of my JSF Managed Bean - the controller):

<t:dataList id="myDataList" value="#{houseControll.fullOffer}" var="element" rows="3" >
...
<t:outputText id="houseId" value="#{element[0]}"/>
...
</t:dataList>

this code shows an 'id' value properly - I have 1,2,3,... values. How can I get the same result in my controller? How to print the data in controller?

like image 340
charles5300 Avatar asked Nov 08 '10 11:11

charles5300


People also ask

How do I get a list of fields from a list of objects?

The list of all declared fields can be obtained using the java. lang. Class. getDeclaredFields() method as it returns an array of field objects.

How do you find an object in a list?

You can get the object at an index using obj = listName. get(index) and set the object at an index using listName. set(index,obj) .

What does get () do in Java?

get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array.

What is list of objects in Java?

It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. The List interface is found in java. util package and inherits the Collection interface.


2 Answers

For starters you aren't iterating over the result list properly, you are not using the index i at all. Try something like this:

List<Object> list = getHouseInfo();
for (int i=0; i<list.size; i++){
   System.out.println("Element "+i+list.get(i));
}

It looks like the query reutrns a List of Arrays of Objects, because Arrays are not proper objects that override toString you need to do a cast first and then use Arrays.toString().

 List<Object> list = getHouseInfo();
for (int i=0; i<list.size; i++){
   Object[] row = (Object[]) list.get(i);
   System.out.println("Element "+i+Arrays.toString(row));
}
like image 79
brain Avatar answered Oct 11 '22 08:10

brain


Do like this

List<Object[]> list = HQL.list(); // get your lsit here but in Object array

your query is : "SELECT houses.id, addresses.country, addresses.region,..."

for(Object[] obj : list){
String houseId = String.valueOf(obj[0]); // houseId is at first place in your query
String country = String.valueof(obj[1]); // country is at second and so on....
.......
}

this way you can get the mixed objects with ease, but you should know in advance at which place what value you are getting or you can just check by printing the values to know. sorry for the bad english I hope this help

like image 44
Sumit Avatar answered Oct 11 '22 07:10

Sumit