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;
        }
    }
目录
相关文章
|
应用服务中间件
IDEA出现问题:idea启动tomcat 很慢解决方案
IDEA出现问题:idea启动tomcat 很慢解决方案
2073 0
IDEA出现问题:idea启动tomcat 很慢解决方案
|
存储 安全 Java
java.util的Collections类
Collections 类位于 java.util 包下,提供了许多有用的对象和方法,来简化java中集合的创建、处理和多线程管理。掌握此类将非常有助于提升开发效率和维护代码的简洁性,同时对于程序的稳定性和安全性有大有帮助。
421 17
|
人工智能 JavaScript 开发工具
MCP详解:背景、架构与应用
模型上下文协议(MCP)是由Anthropic提出的开源标准,旨在解决大语言模型与外部数据源和工具集成的难题。作为AI领域的“USB-C接口”,MCP通过标准化、双向通信通道连接模型与外部服务,支持资源访问、工具调用及提示模板交互。其架构基于客户端-服务器模型,提供Python、TypeScript等多语言SDK,方便开发者快速构建服务。MCP已广泛应用于文件系统、数据库、网页浏览等领域,并被阿里云百炼平台引入,助力快速搭建智能助手。未来,MCP有望成为连接大模型与现实世界的通用标准,推动AI生态繁荣发展。
9897 66
|
XML Java API
List与String相互转化方法汇总
本文汇总了List与String相互转化的多种方法,包括使用`String.join()`、`StringBuilder`、Java 8的Stream API、Apache Commons Lang3的`StringUtils.join()`以及Guava的`Joiner.on()`方法实现List转String;同时介绍了使用`split()`方法、正则表达式、Apache Commons Lang3的`StringUtils.split()`及Guava的`Splitter.on()`方法实现String转List。
2677 1
List与String相互转化方法汇总
|
消息中间件 关系型数据库 MySQL
消息中间件系列教程(05) -RabbitMQ -管理控制台的使用
消息中间件系列教程(05) -RabbitMQ -管理控制台的使用
659 1
|
JSON Java 数据格式
如何优雅的使用 RestTemplate
如何优雅的使用 RestTemplate
|
Java 数据管理 关系型数据库
Spring Boot中实现多数据源配置
Spring Boot中实现多数据源配置
|
Java Spring
springboot项目读取 resources 目录下的文件的9种方式(总结)
springboot项目读取 resources 目录下的文件的9种方式(总结)
8609 1
|
JavaScript Java API
spring boot使用异步多线程
一文讲清楚spring boot如何结合异步多线程实现文件的导出这类耗时间的操作优化以及常用的场景,了解异步思想
459 0
spring boot使用异步多线程

热门文章

最新文章