Java Collections singletonMap()方法与示例

集合类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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541862.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java访问登录网页_===java怎样访问需要登录才能查看的网页????急!!===...

java中可以用java.net包下的东西访问网页&#xff0c;但是有的网页要求用户先输入用户名和密码才能查看&#xff0c;这些网页java怎么访问呢&#xff1f;&#xff1f;&#xff1f;注意&#xff1a;我说的要输入用户名和密码不是浏览器弹出一个框的那种&#xff0c;而是象csdn这…

javascript OOP(下)(九)

一、javascript模拟重载 java中根据参数类型和数量的区别来实现重载&#xff0c;javascript弱类型&#xff0c;没有直接的机制实现重载&#xff0c;javascript中参数类型不确定和参数个数任意&#xff0c;通过判断实际传入的参数的个数来实现重载。 <script> function Pe…

java calendar_Java Calendar getDisplayNames()方法与示例

java calendar日历类的getDisplayNames()方法 (Calendar Class getDisplayNames() method) getDisplayNames() method is available in java.util package. getDisplayNames()方法在java.util包中可用。 getDisplayNames() method is used to return Map that contains all fie…

Linux如何查找大文件或目录总结

转载&#xff1a;http://www.cnblogs.com/kerrycode/p/4391859.html 在Windows系统中&#xff0c;我们可以使用TreeSize工具查找一些大文件或文件夹&#xff0c;非常的方便高效&#xff0c;在Linux系统中&#xff0c;如何去搜索一些比较大的文件呢&#xff1f;下面我整理了一下…

java编写简单邮件_Javamail,编写简单的程序发送邮件

代码&#xff1a;package com.dai.mail; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.In…

java calendar_Java Calendar getLeastMaximum()方法与示例

java calendarCalendar类的getLeastMaximum()方法 (Calendar Class getLeastMaximum() method) getLeastMaximum() method is available in java.util package. getLeastMaximum()方法在java.util包中可用。 getLeastMaximum() method is used to get the least maximum value …

Shell 标准输入、输出和错误

防伪码&#xff1a;桃花潭水深千尺&#xff0c;不及汪伦送我情。文件描述符&#xff08;fd&#xff09;&#xff1a;文件描述符是一个非负整数&#xff0c;在打开现存文件或新建文件时&#xff0c;内核会返回一个文件描述符&#xff0c;读写文件也需要使用文件描述符来访问文件…

java需要会的工具_Java开发者必备的几款工具,一定要掌握!

原标题&#xff1a;Java开发者必备的几款工具&#xff0c;一定要掌握&#xff01;NotepadNotepad是用于编辑xml、脚本以及记笔记的最佳工具。这个工具的最好部分在于&#xff0c;你在Notepad上打开的任何一个文档&#xff0c;在关闭后都会有一个残留文档&#xff0c;它有助于在…

java的equals方法_Java LocalDateTime类| 带示例的equals()方法

java的equals方法LocalDateTime类equals()方法 (LocalDateTime Class equals() method) equals() method is available in java.time package. equals()方法在java.time包中可用。 equals() method is used to check whether this date-time and the given object are equal or…

portlet java_Java Portlet 规范概述

前言1、portlet是一种类似servlet的规范。2、servlet是web组件&#xff0c;portlet也是web组件。参考1、百度百科&#xff1a;portlethttp://baike.baidu.com/link?urlvMzVwpkf5WzOL23GLkgM4C5C7Sarqh1XXShS73L7k-MbGgM0ooZ4Dl2Efor3bb4tZmmLo6v-muG5UW7_CYMTUahttp://hintcnu…

Java LocalDate类| minusWeeks()方法与示例

LocalDate类minusWeeks()方法 (LocalDate Class minusWeeks() method) minusWeeks() method is available in java.time package. minusWeeks()方法在java.time包中可用。 minusWeeks() method is used to subtract the given weeks from this LocalDate and return the LocalD…

Android推荐的几本书

2019独角兽企业重金招聘Python工程师标准>>> 第一阶段 <<第一行代码Android>><<疯狂Android>>第二阶段 <<Android开发艺术探索>><<Android群英传>>Android源码 第三阶段 <<Android开发艺术探索>><&…

java数据类型后缀_java基础知识---基本数据类型

1)四种整数类型(byte、short、int、long)&#xff1a;byte&#xff1a;8 位&#xff0c;用于表示最小数据单位&#xff0c;如文件中数据&#xff0c;-128~127short&#xff1a;16 位&#xff0c;很少用&#xff0c;-32768 ~ 32767int&#xff1a;32 位、最常用&#xff0c;-2^3…

duration java_Java Duration类| minusMillis()方法与示例

duration java持续时间类minusMillis()方法 (Duration Class minusMillis() method) minusMillis() method is available in java.time package. minusMillis()方法在java.time包中可用。 minusMillis() method is used to subtract the given duration in milliseconds from t…

Thread 和 Runnable创建新线程的区别,Runnable可以共享线程类的实例属性

Thread实现多线程&#xff1a; public class Thread2 extends Thread{ public int i; public void run(){ for(; i < 100 ;i) { System.out.println(getName() " " i); } } public static void main(String[] args){ for (int j 0; j < 100; j) { System…

java编程求最小公约数_java求最大公约数与最小公倍数

public class Gongyueshu{public static void main(String[] args){//从控制台输入两个数据int m Integer.parseInt(args[0]);int n Integer.parseInt(args[1]);int y 1 ;int b 1;if (m > 0 && n >0){//先判定这两个数是否为倍数关系&#xff0c;如果是则小数…

tdr上升时间什么设定_TDR的完整形式是什么?

tdr上升时间什么设定TDR&#xff1a;时域反射仪/车票寄存收据/定期存款收据 (TDR: Time Domain Reflectometer/ Ticket Deposit Receipt/ Term Deposit Receipt) 1)TDR&#xff1a;时域反射仪 (1) TDR: Time Domain Reflectometer) TDR is an abbreviation of the "Time D…

【java】反射+poi 导出excel

2019独角兽企业重金招聘Python工程师标准>>> 反射 导出的数组转变成对象 private static Object expexcelMaptobean(Class<?> cobj,Map<String,String> map,int[] expColums,String[] params) throws InstantiationException, IllegalAccessException…

css设置背景图片大小_如何使用CSS设置背景图片大小?

css设置背景图片大小Introduction: 介绍&#xff1a; As we all know that the images are a very responsive yet very creative way to display your web page or website to the users. The images also play a major role in indulging users to websites or web pages. T…

java gc的特性_Java12新特性 -- 可中断的G1 Mixed GC

Java 12 中增强了 G1 垃圾收集器关于混合收集集合的处理策略&#xff0c;这节主要介绍在 Java 12 中同时也对 G1垃圾回收器进行了改进&#xff0c;使其能够在空闲时自动将 Java 堆内存返还给操作系统&#xff0c;这也是 Java 12 中的另外一项重大改进。目前 Java 11 版本中包含…