Collections.singletonMap()用法


在这里插入图片描述

想学习架构师构建流程请跳转: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;
        }
    }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵广陆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值