想学习架构师构建流程请跳转:Java架构师系统架构设计
1 Collections.singletonMap()用法
Collections.singletonMap()用于返回单集合
singletonMap() method is available in java.util package.
singletonMap()方法在java.util包中可用。
singletonMap() method is used to return an immutable map (i.e. immutable map is a map that contains the given key & value only & mapping would be based on the given key to the given value.
翻译:singletonMap()方法用于返回不可变的映射(即,不可变的映射是仅包含给定键和值的映射,并且映射将基于给定键到给定值。
singletonMap() method is a static method, so it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
翻译:singletonMap()方法是静态方法,因此可以使用类名进行访问,如果尝试使用类对象访问该方法,则不会收到错误。
singletonMap() method does not throw an exception at the time of returning an immutable map.
在返回不可变地图时, singletonMap()方法不会引发异常。
具体用法:
List<User> list = new ArrayList();
User user1 = User.builder().id("aaaaa").username("test1").build();
User user2 = User.builder().id("bbbbb").username("test2").build();
list.add(user1);
list.add(user2);
Map<String, List<User>> singletonMap = Collections.singletonMap( "list",list);
System.out.println(singletonMap);
2 使用场景
使用的话和普通map差不多:
public boolean putData(String tableName, String rowKey, String columnFamily, String column,
String value) {
//键值对
return putData(tableName, rowKey, columnFamily, Collections.singletonMap(column, value));
}
/**
* 插入数据(批量)
*
* @param tableName 表名
* @param rowKey rowKey
* @param columnFamily 列族
* @param columns 列值
* @return true/false
*/
public boolean putData(String tableName, String rowKey, String columnFamily,
Map<String, String> columns) {
try {
Table table = hbaseAdmin.getConnection().getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowKey));
for (Map.Entry<String, String> entry : columns.entrySet()) {
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(entry.getKey()),
Bytes.toBytes(entry.getValue()));
}
table.put(put);
table.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}