集合类singletonMap()方法 (Collections Class singletonMap() method)

  • 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()方法不会引发异常。

Syntax:

句法:

    public static Map singletonMap(Type key_ele, Type val_ele);

Parameter(s):

参数:

  • Type key_ele – represents the key to be saved in the returned map.

    key_ele类型 –表示要保存在返回的映射中的密钥。

  • Type val_ele – represents the value(val) associated with the key_ele.

    类型val_ele –表示与key_ele关联的value( val )。

Return value:

返回值:

The return type of this method is Map, it returns an immutable map that contains the given key-value pairs only of the map.

此方法的返回类型为Map ,它返回一个不可变的映射,其中仅包含该映射的给定键值对。

Example:

例:

// Java program is to demonstrate the example of
// singletonMap(Type key_ele, Type val_ele)
// method of Collections
import java.util.*;
public class SingletonMapOfCollections {public static void main(String args[]) {// Instatiates a hash map object
Map < Integer, String > map = new HashMap < Integer, String > ();
// By using put() method is to add
// objects in a hash map
map.put(10, "C");
map.put(20, "C++");
map.put(30, "JAVA");
map.put(40, "C");
map.put(50, "C++");
// Display Map
System.out.println("Map: " + map);
// By using singletonMap() method is to
// list the given key-value pair only
map = Collections.singletonMap(30, "JAVA");
// Display SingletonMap
System.out.println("Collections.singletonMap(30,JAVA): " + map);
}
}

Output

输出量

Map: {50=C++, 20=C++, 40=C, 10=C, 30=JAVA}
Collections.singletonMap(30,JAVA): {30=JAVA}

翻译自: https://www.includehelp.com/java/collections-singletonmap-method-with-example.aspx

Java Collections singletonMap()方法与示例相关推荐

  1. java rotate,Java Collections rotate()方法与示例

    集合类rotate()方法rotation()方法在java.util包中可用. rotation()方法用于将List(l)元素旋转给定距离(dis). rotation()方法是静态方法,因此可以 ...

  2. Java Collections.emptyList() 方法的使用及注意事项

    Java Collections.emptyList方法的使用及注意事项 一.emptyList() 作用:返回一个空的List(使用前提是不会再对返回的list进行增加和删除操作): 好处: 1. ...

  3. Java Collections.frequency()方法具有什么功能呢?

    转自: Java Collections.frequency()方法具有什么功能呢? 下文笔者讲述Collections.frequency()方法的功能简介说明,如下所示: Collections. ...

  4. List元素互换,List元素转换下标,Java Collections.swap()方法实例解析

    Java Collections.swap()方法解析 jdk源码: public static void swap(List<?> list, int i, int j) {// ins ...

  5. Java IOUtils.copy方法代码示例(亲测)

    本文整理汇总了Java中org.apache.commons.io.IOUtils.copy方法的典型用法代码示例.如果您正苦于以下问题:Java IOUtils.copy方法的具体用法?Java I ...

  6. java reverselist_Java Collections reverse()方法与示例

    集合类reverse()方法reverse()方法在java.util包中可用. reverse()方法用于反转给定list(l)元素的顺序,换句话说,可以说此方法用于从右侧开始更改其元素的顺序. r ...

  7. java singletonlist_Java Collections singletonList()方法及示例

    集合类singletonList()方法 (Collections Class singletonList() method) singletonList() method is available ...

  8. java user directory,Java ProcessBuilder directory()方法与示例

    语法:public File directory (); public ProcessBuilder directory (File dir); ProcessBuilder类directory()方 ...

  9. Java序列化魔术方法及其示例使用

    在上一篇文章中, 您需要了解有关Java序列化的所有知识 ,我们讨论了如何通过实现Java序列化来启用类的可序列化性. Serializable接口. 如果我们的类未实现Serializable接口, ...

最新文章

  1. [转载]ESFramework 4.0 快速上手(15) -- 客户端登录验证
  2. 图像处理:图像复原与重建之逆滤波、维纳滤波、约束最小二乘滤波——Matlab实现
  3. dw项目符号空心圆怎么设置_项目规范配置之editorconfigamp;prettier
  4. Springcloud gateway获取post请求内容
  5. ASP.NET 网页之间传递值
  6. 飞龙的程序员书单 – Linux
  7. 【C11】complex
  8. mysql实时写入查询_MySQL实时写入表
  9. 常用的排序算法(java版)
  10. 被众多车企“抛弃”、增长放缓,Mobileye值不值500亿美元?
  11. javaScript之原生js封装组件(弹窗为例)
  12. LM4871(3W音频功放芯片)中文资料
  13. 查询数据表中重复数据及重复次数
  14. 华东师范计算机考研科目,华东师范大学计算机技术研究生考试科目和考研参考书目...
  15. /proc/config.gz 是什么
  16. 河北省会计证年检流程
  17. 基于神经网络的文字识别,神经网络如何识别图像
  18. PIC反汇编流程(详解)
  19. transformers5--t5模型中encoder与decoder内容不同解读
  20. 【秋钓皮皮】 奔跑吧,皮皮!(有图了)

热门文章

  1. oracle精度说明符1~38_Oracle 错误代码总结及解决方案
  2. 怎么判断一个字符串的最长回文子串是否在头尾_LeetCode 5 迅速判断回文串的Manacher算法...
  3. mysql 升级 openssl_【1分钟教程】LNMP架构应用实战 Openssl升级操作
  4. 二叉树的层序遍历和前中后序遍历代码 迭代/递归
  5. Shell脚本多行换行报错:- unrecognized arguments- \
  6. macos降级_iOS12.3 beta2更新了什么 iOS12.3测试版2新特性与升降级方法
  7. mysql百万数据根据索引查询_mysql创建多列索引查询百万表数据的性能优化经验分享...
  8. element走马灯自动_详细element-ui的走马灯carousel轻松实现自适应全屏banner详细过程...
  9. 高性能MySQL(1)——MYSQL架构
  10. 【动态规划】简单背包问题II