Aziz
Aziz

Reputation: 23

Add user defined object as key in hashmap

public class Dashboard {
int REQUEST_ID, PRICE;           
String LOGIN_USER;

public int getREQUEST_ID() {
 return REQUEST_ID;
}

public void setREQUEST_ID(int rEQUEST_ID) {
 REQUEST_ID = rEQUEST_ID;
 }

 //all getters and setters
public class DBConnection {
public ArrayList<Dashboard>  getStoreResult() {
  ArrayList<Dashboard> dashRec=new ArrayList<Dashboard>();
  Dashboard dash = new Dashboard();

try{
  Class.forName("");
  Connection con=DriverManager.getConnection("");
  Statement st=con.createStatement();
  ResultSet rs=st.executeQuery("");

  HashMap<Object, List<Dashboard>> map = new HashMap<>();
  while (rs.next()) {
    Integer id = rs.getInt(1);
    if (!map.containsKey(id)) {
        dashRec= new ArrayList<Dashboard>();
        map.put(id, dashRec);
    }
    dash = new Dashboard();
    dash.setREQUEST_ID(id);
    dash.setLOGIN_USER(rs.getString(2));
    dash.setPRICE(rs.getInt(3));
    map.get(id).add(dash);
     } 
    }

I want to add name and Reqid for each set of row as key object in map.So for like id 123 I need a map with id=123,Name=A as key.The value would be arraylist which contains all rows specific to id.For id=123,arraylist contains first 3 rows as 3 objects.I want to add the id 123 and name as key now for all sets of row.

SQL DB

Upvotes: 0

Views: 594

Answers (3)

Sachin Kumar
Sachin Kumar

Reputation: 1167

@Aziz

try this

 //all getters and setters
public class DBConnection {
public ArrayList<Dashboard>  getStoreResult() {
  ArrayList<Dashboard> dashRec=new ArrayList<Dashboard>();
  Dashboard dash = new Dashboard();

try{
  Class.forName("");
  Connection con=DriverManager.getConnection("");
  Statement st=con.createStatement();
  ResultSet rs=st.executeQuery("");

  HashMap<Object, List<Dashboard>> map = new HashMap<>();

while (rs.next()) {

     Integer id = rs.getInt(1);
    Set<Dashboard> keysets = map.keySet();
    Dashboard d = null;
    for (Dashboard dash : keysets) {
        if(dash.getREQUEST_ID().equals.(id)){
            d = dash;
            break;
        }
    }

    Dashboard dash1 = new Dashboard();
    dash1.setREQUEST_ID(id);
    dash1.setLOGIN_USER(rs.getString(2));
    dash1.setPRICE(rs.getInt(3));

    if(d!=null){
        map.get(d).add(dash1)
    }else{
        List<Dashboard> lst = new ArrayList<>();
        lst.add(dash1);
        map.put(dash1,lst)
    }
    }
}   

Upvotes: 0

Sachin Kumar
Sachin Kumar

Reputation: 1167

try this for unique ness

public class Dashboard {
int REQUEST_ID, PRICE;           
String LOGIN_USER;

public int getREQUEST_ID() {
 return REQUEST_ID;
}

public void setREQUEST_ID(int rEQUEST_ID) {
 REQUEST_ID = rEQUEST_ID;
 }


  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Dashboard dash = (Dashboard) o;
    return Objects.equals(REQUEST_ID, dash.REQUEST_ID) && Objects.equals(LOGIN_USER, dash.LOGIN_USER) && && Objects.equals(PRICE, dash.PRICE);
  }

  @Override
  public int hashCode() {
    return Objects.hash(REQUEST_ID, PRICE+","+LOGIN_USER
    );
  }


 }

Upvotes: 1

CodeScale
CodeScale

Reputation: 3314

You can materialize your key as a specific type.

public class MapKey {
  private Integer id;
  private String name;

  MapKey(Integer id, String name) {
    this.id = id;
    this.name = name;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    MapKey mapKey = (MapKey) o;
    return Objects.equals(id, mapKey.id) &&
            Objects.equals(name, mapKey.name);
  }

  @Override
  public int hashCode() {
    return Objects.hash(id, name);
  }
}

and then use this object as key of your map.

map.put(new MapKey("", 0), new Dash...);

ps: don't forget most important parts --> override correctly equals and hashcode in MapKey

Extraction:

MapKey key = new MapKey(rs.getInt(1), rs.getString(2));
if (!map.containsKey(key)) {
    dashRec= new ArrayList<Dashboard>();
    map.put(key, dashRec);
}

Upvotes: 1

Related Questions