Search code examples
javacollectionsresultset

Getting statistics from resultset


I have a resultset which returns 5 values. I set that data in a POJO, and added that pojo to a list.

datapojo = new OutputViewDataPOJO();
datapojo.setTime(resultado.getInt(1));
datapojo.setCommandDesc(resultado.getString(2));
datapojo.setOkError(resultado.getString(3));
datapojo.setResultDesc(resultado.getString(4));
datapojo.setResultCode(resultado.getInt(5));                    
listPojo.add(datapojo);

    [Description:Call connect, TimeStamp:0, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Waiting open complete, TimeStamp:2468, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Reading card, TimeStamp:0, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Waiting read card, TimeStamp:10329, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Reading card, TimeStamp:0, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Waiting read card, TimeStamp:6671, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Reading card, TimeStamp:0, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Waiting read card, TimeStamp:6704, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Reading card, TimeStamp:0, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Waiting read card, TimeStamp:6578, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Reading card, TimeStamp:0, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Waiting read card, TimeStamp:6578, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Reading card, TimeStamp:0, OkError:OK, ResultCode:0, ResultDesc:null.]
    [Description:Waiting read card, TimeStamp:6594, OkError:OK, ResultCode:0, ResultDesc:null.]

the top listed data is printed in console, now my question: how can I get the sum of TimeStamp for the Description "Waiting read card". I need to do some statistics, in this case is getting the media value of timeStamp for every Description and I don´t know how to do that. Please, help me!


Solution

  • If you want to store that data in database and then retrieve , you need create a database and table (f.e. user) with these columns

    name -age -city -car model -house type
    

    Then you can store data ( Insert into user(name, age, city, car_model, house_type) values (?,?,?,?,?) and retrieve (Select * from user where city = 'Madrid') etc.

    You could have a POJO entity as mentioned in one comments :

    public class User
    {
       private int id;
       private String name;
       private int age;
       private String city;
       private String carModel;
       private String houseType;
    
      //getters , setters
    }
    

    Create corresponding table in database, and manipulate with data with Insert, Select queries.

    If you don't want to store data in database, you can create a Maps for easy filtering. f.e.

     User user = ...; //an instance of User
     Map<String, List<User>> userCityMap = new HashMap<String,List<User>>(); 
     List<User> userWithThatCity = map.get(user.getCity());
     if(userWithThatCity == null)
     {
       userWithThatCity = new ArrayList<User>();
       userCityMap.put(user.getCity(), userWithThatCity);
     }
     userWithThatCity.add(user);
     ...
    

    I hope that will help.