如何根据它们的值将类的对象添加到哈希映射? [英] how to add objects of class to hashMap based on their values?
问题描述
我从数据库中检索数量的值,需要将其添加到列表中,然后根据值添加到具有特定键的MAP中,例如
<$第1行名称=A类别=1
第2行名称=B类别=2
第3行名称=C类别p $ p>
=1
row 4 name =Dcategory =3
I需要将行1和行3放入列表中,并将行2和行4中的每一行放在单独的列表中,然后
将这三个列表放入MAP中,并使用类别名称(1,2,3)作为MAP的关键字。
pre $
MAP
1 LIST1(2行值)
2 LIST2(1行值)
3 LIST3(1行的值)
我不知道如何将它们添加到列表和MAP。
我的约束是,不是每次检索值时都会检索相同的行,因此每次我可能需要不同数量的列表来保留已检索的每个类别的值。
$ b
我的代码如下,
Map map = new HashMap();
if(rs.next())
{
item.setRowID(rs.getString(ID));
item.setName(rs.getString(Name));
item.setValue(rs.getString(Value));
item.setCat(rs.getString(Cat));
if(!map.containsKey(rs.getString(Cat))){
map.put(rs.getString(CatID),new ArrayList());
}
map.get(rs.getString(CatID))。add(row);
}
最后一行运行到此错误>方法add(Rows)未定义为Object类型。
,即使我将该对象添加到列表中,但遇到同样的错误,方法add(List)对于类型Object也是未定义的。
您需要创建列表地图。在阅读记录时,如果存在一个键(这里是类别),则将当前行添加到列表中。
Map map = new HashMap()
for(Row row:resultset){
if(!map.containsKey(row.category)){
map.put(row.category,new ArrayList() );
}
map.get(row.category).add(row.name);
}
更新
$ b
问题是你需要在这里指定泛型类型
Map< String,List< Rows> > map = new HashMap< String,List< Rows>>()
while(rs.next()){
Rows row = new Rows();
/ *
*代码初始化记录中的行值
* /
String category = rs.getString(Cat);如果(!map.containsKey(category)){
map.put(category,new ArrayList< Rows>());
}
map.get(category).add(row);
}
I am retrieving number of values from database, that need to be added to a list and then to a MAP with specific Key based on their values, for example,
row 1 name = "A" category = "1"
row 2 name = "B" category = "2"
row 3 name = "C" category = "1"
row 4 name = "D" category = "3"
I need to put row1 and row 3 in a list and each of row2 and row 4 in a separate list, then put these three lists into MAP and use the category names (1,2,3) as keys of MAP.
MAP
1 LIST1 (values of 2 rows)
2 LIST2 (values of 1 row)
3 LIST3 (values of 1 row)
I am not sure how to add them to the list and MAP. My constraint is that not everytime that I retrieve the values the same rows will be retrieved, so everytime I may need different number of lists to keep the values of each category that has been retrieved.
My code is as following,
ResultSet rs = st.executeQuery(SelectRows);
Map map = new HashMap();
if(rs.next())
{
item.setRowID(rs.getString("ID"));
item.setName(rs.getString("Name"));
item.setValue(rs.getString("Value"));
item.setCat(rs.getString("Cat"));
if(!map.containsKey(rs.getString("Cat"))){
map.put(rs.getString("CatID"), new ArrayList());
}
map.get(rs.getString("CatID")).add(row);
}
the last line runs into this error > "" the method add(Rows) is undefined for the type Object. even if I add the object to a list I run into the same error, the method add(List) is undefined for the type Object.
You need to create a map of lists. As you read the records, if there exist a key (here is category) then add the current row to the list
Map map = new HashMap()
for( Row row : resultset ) {
if(!map.containsKey(row.category)){
map.put(row.category, new ArrayList());
}
map.get(row.category).add(row.name);
}
UPDATE
the problem is that you need to specify generic type here
Map<String, List<Rows>> map = new HashMap<String, List<Rows>>()
while( rs.next() ) {
Rows row = new Rows();
/*
* code to initialize the values of row from the record
*/
String category = rs.getString("Cat");
if(!map.containsKey(category)){
map.put(category, new ArrayList<Rows>());
}
map.get(category).add(row);
}
这篇关于如何根据它们的值将类的对象添加到哈希映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!