HashMap.put(String , Object )
时间: 2024-04-30 18:21:01 浏览: 16
HashMap.put(String, Object) 是 Java 中 HashMap 类的一个方法,用于将指定的键和值映射存储在 HashMap 中。该方法接受两个参数,第一个参数是字符串类型的键,第二个参数是一个对象,即要存储的值。如果该键已经存在于 HashMap 中,则该键的旧值将被新值替换。如果该键不存在于 HashMap 中,则该键值对将被添加到 HashMap 中。例如,下面的代码展示了如何使用 put() 方法将键值对添加到 HashMap 中:
```
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("key1", "value1");
hashMap.put("key2", "value2");
```
在上面的示例中,我们创建了一个 HashMap 对象,并使用 put() 方法将两个键值对添加到 HashMap 中。第一个键值对的键是 "key1",值是 "value1",第二个键值对的键是 "key2",值是 "value2"。如果键 "key1" 已经存在于 HashMap 中,则它的旧值将被新值 "value1" 替换。否则,将添加一个新的键值对。
相关问题
Cannot invoke "java.util.HashMap.put(Object, Object)" because "this.scoreMap" is null
This error message indicates that you are trying to add a key-value pair to a HashMap object (using the put() method), but the HashMap object has not been initialized yet.
To fix this error, you need to create a new instance of the HashMap object before adding any key-value pairs to it. For example:
```
Map<String, Integer> scoreMap = new HashMap<>();
scoreMap.put("John", 80);
```
This code creates a new HashMap object and adds a key-value pair to it. Make sure you initialize the HashMap object before trying to add any values to it.
public List<HashMap<String,String>> orderList1(String patiendid) throws SQLException { List<HashMap<String, String>> list=new ArrayList<>(); String sql="select record.rid,record.ordertime,record.visitdate," + "record.visittime,record.serialnumber,record.state,doctor.dname," + "doctor.office,doctor.fee,doctor.picpath " + "from record,doctor where record.did=doctor.did&&record.pid=?"; ResultSet rs = DBUtil.executeQuery(sql, new Object[]{patiendid}); while(rs.next()) { HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("rid",rs.getString(1)); hashMap.put("ordertime",rs.getString(2)); hashMap.put("visitdate",rs.getString(3)); hashMap.put("visittime",rs.getString(4)); hashMap.put("serialnumber",rs.getString(5)); hashMap.put("state",rs.getString(6)); hashMap.put("dname",rs.getString(7)); hashMap.put("office",rs.getString(8)); hashMap.put("fee",rs.getString(9)); hashMap.put("picpath",rs.getString(10)); list.add(hashMap); } return list; }
这段代码也是一个查询病人预约列表的方法,接收一个patiendid作为参数。它使用了SQL语句,通过连接record表和doctor表查询病人预约信息以及对应医生的信息,返回一个ResultSet结果集。然后遍历结果集,将每一条预约信息以及医生信息存入一个HashMap中,将所有HashMap存入一个List中,最后返回这个List。需要注意的是,这段代码没有使用数据库连接池的方式,因此需要手动关闭ResultSet和数据库连接。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)