Collections.singletonMap()用法

简介: Collections.singletonMap()用法

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;
        }
    }
目录
相关文章
|
19天前
|
搜索推荐 Java
Collections.sort()方法总结
Collections.sort()方法总结
|
24天前
|
Java 测试技术
collections.shuffle用法详解
collections.shuffle用法详解
|
2月前
Collection和Collections区别
Collection和Collections区别
|
9月前
|
存储 安全 Java
每日一道面试题之Collection 和 Collections 有什么区别?
每日一道面试题之Collection 和 Collections 有什么区别?
|
11月前
|
安全 Java 索引
collections类
collections类
33 0
|
存储 算法 Java
Set、可变参数、Collections工具类、Map集合
Set、可变参数、Collections工具类、Map集合
集合Collections工具类
针对集合进行操作的工具类。
Collections.singletonList使用方法
Collections.singletonList使用方法
Java基础:Collections.sort的两种用法详解
Java基础:Collections.sort的两种用法详解
871 0
Java基础:Collections.sort的两种用法详解
Collections集合工具类的常用方法
Collections集合工具类的方法 addAll与shuffle import java.util.ArrayList; import java.util.Collections; /* - java.util.Collections是集合工具类,用来对集合进行操作。部分方法如下: - publ