一行代码将10个值put进map
时间: 2024-01-31 14:04:20 浏览: 17
这是一行代码将10个值put进map的示例:
Map<String, Integer> map = new HashMap<String, Integer>() {{
put("value1", 1);
put("value2", 2);
put("value3", 3);
put("value4", 4);
put("value5", 5);
put("value6", 6);
put("value7", 7);
put("value8", 8);
put("value9", 9);
put("value10", 10);
}};
相关问题
帮我用java代码实现一个excel文件导入java,并且将这个excel转为Map类,并且Map类的key值为excel的第一行
好的,我可以回答你的问题。
以下是Java代码实现将Excel文件导入Java,并将其转换为Map类的示例代码:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelToMapConverter {
public static Map<String, String> convertToMap(File file) throws Exception {
Map<String, String> map = new HashMap<>();
InputStream inputStream = new FileInputStream(file);
Workbook workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0);
Row firstRow = sheet.getRow(0);
int columnCount = firstRow.getLastCellNum();
for (int i = 0; i < columnCount; i++) {
Cell cell = firstRow.getCell(i);
String key = cell.getStringCellValue();
map.put(key, "");
}
int rowCount = sheet.getLastRowNum();
for (int i = 1; i <= rowCount; i++) {
Row row = sheet.getRow(i);
for (int j = 0; j < columnCount; j++) {
Cell cell = row.getCell(j);
String key = firstRow.getCell(j).getStringCellValue();
String value = "";
if (cell != null) {
switch (cell.getCellType()) {
case STRING:
value = cell.getStringCellValue();
break;
case NUMERIC:
value = Double.toString(cell.getNumericCellValue());
break;
case BOOLEAN:
value = Boolean.toString(cell.getBooleanCellValue());
break;
default:
value = "";
break;
}
}
map.put(key, value);
}
}
return map;
}
}
```
这个示例代码使用Apache POI库来读取Excel文件,将Excel文件的第一行作为Map类的key值,并将Excel文件的其余部分转换为Map类的值。你可以将此代码放入Java项目中,并使用以下代码来调用它:
```java
File file = new File("example.xlsx");
Map<String, String> map = ExcelToMapConverter.convertToMap(file);
```
其中"example.xlsx"是你要导入的Excel文件的名称,你需要将其放置在Java项目的根目录中。当然,你可以根据自己的需求修改代码以适应不同的Excel文件格式。
怎么使用java代码将map集合行变列,列变行存入数据库
您可以使用Java中的循环和SQL语句来实现将Map集合行变列,列变行存入数据库的功能。具体步骤如下:
1. 遍历Map集合,将每个键值对的键和值分别存入两个数组中。
2. 创建一个新的Map集合,将原Map集合中的键值对的键作为新Map集合中的值,将原Map集合中的键值对的值作为新Map集合中的键。
3. 遍历新Map集合,将每个键值对的键和值分别存入两个数组中。
4. 使用循环将两个数组中的数据逐个插入到数据库中,注意要使用SQL语句来实现数据的插入。
以下是示例代码:
```java
// 原Map集合
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
// 将原Map集合行变列
String[] keys = new String[map.size()];
String[] values = new String[map.size()];
int i = 0;
for (Map.Entry<String, String> entry : map.entrySet()) {
keys[i] = entry.getKey();
values[i] = entry.getValue();
i++;
}
// 将原Map集合列变行
Map<String, String> newMap = new HashMap<>();
for (int j = 0; j < keys.length; j++) {
newMap.put(values[j], keys[j]);
}
String[] newKeys = new String[newMap.size()];
String[] newValues = new String[newMap.size()];
int k = 0;
for (Map.Entry<String, String> entry : newMap.entrySet()) {
newKeys[k] = entry.getKey();
newValues[k] = entry.getValue();
k++;
}
// 将数据插入到数据库中
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
conn.setAutoCommit(false);
String sql = "INSERT INTO table_name (key, value) VALUES (?, ?)";
ps = conn.prepareStatement(sql);
for (int m = 0; m < newKeys.length; m++) {
ps.setString(1, newKeys[m]);
ps.setString(2, newValues[m]);
ps.addBatch();
}
ps.executeBatch();
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
if (conn != null) {
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
希望这个回答能够帮助您解决问题。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)