语法
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
Map<key - data type, key - data type> object = new HashMap<key - data type, value - data type> ();
Map<key - data type, key - data type> object = new TreeMap<key - data type, value - data type> ();
Map<key - data type, key - data type> object = new LinkedHashMap<key - data type, value - data type> ();
Object.put (new data_type (key), value);
Object.remove (new data_type (key));
System.out.println(map_object);
for (Map.Entry temporary_object : main_object.entrySet()){ system.out.print(temporary_object.getKey() + "-" + temporary_object.getValue() ) }
import java.util.*;
public class JavaCollectionMap{ include variable, method, and object here… }
public static void main(String args[]){ write java collection map code here… }
Map<String,String > jcm = new HashMap<String,String >();
jcm.put("A", "HashMap");
System.out.println(jcm);
public class JavaCollectionMap{ public static void main(String args[]){ Map<String,String > jcm = new HashMap<String,String >(); jcm.put("A", "HashMap"); jcm.put("B", "TreeMap"); jcm.put("C", "LinkedHashMap"); System.out.println(jcm); }}
以下示例可帮助您了解从集合映射中插入、更新、删除值。
带有插入值示例和输出的 java 集合映射如下。
代码:
import java.util.*; class JavaCollectionMap{ public static void main(String args[]){ Map<Integer,String > jcm1 = new HashMap<Integer,String>(); jcm1.put (01, "HashMap"); jcm1.put (02, "TreeMap"); jcm1.put (03, "LinkedHashMap"); jcm1.put (04, "Map class"); jcm1.put (05, "Map interface"); System.out.println (jcm1); }}
输出:
描述
带有迭代示例和输出的集合映射如下。
代码:
import java.util.*; class JavaCollectionMap{ public static void main(String args[]){ Map<Integer, String > jcm1 = new HashMap<Integer, String>(); jcm1.put(01, "HashMap"); jcm1.put(02, "TreeMap"); jcm1.put(03, "LinkedHashMap"); jcm1.put(04, "Map class"); jcm1.put(05, "Map interface"); for(Map.Entry jcm:jcm1.entrySet()){ System.out.println(jcm.getKey()+" "+jcm.getValue()); } }}
输出:
描述
带有更改值示例和输出的集合映射如下。
代码:
import java.util.*; class JavaCollectionMap{ public static void main(String args[]){ Map<Integer, String > jcm1 = new HashMap<Integer, String>(); jcm1.put(01, "HashMap"); jcm1.put(02, "TreeMap"); jcm1.put(03, "LinkedHashMap"); jcm1.put(04, "Map class"); jcm1.put(05, "Map interface"); System.out.println("original key and value of the Map"); for(Map.Entry jcm:jcm1.entrySet()){ System.out.println(jcm.getKey()+" "+jcm.getValue()); } jcm1.put(new Integer(01), "Java HashMap"); jcm1.put(new Integer(02), "Java TreeMap"); jcm1.put(new Integer(03), "java LinkedHashMap"); System.out.println("Updated key and value of the Map"); for(Map.Entry jcm:jcm1.entrySet()){ System.out.println(jcm.getKey()+" "+jcm.getValue()); } }}
输出:
描述
带有删除值示例和输出的集合映射如下。
代码:
import java.util.*; class JavaCollectionMap{ public static void main(String args[]){ Map<Integer, String > jcm1 = new HashMap<Integer, String>(); jcm1.put(01, "HashMap"); jcm1.put(02, "TreeMap"); jcm1.put(03, "LinkedHashMap"); jcm1.put(04, "Map class"); jcm1.put(05, "Map interface"); System.out.println("original key and value of the Map"); for(Map.Entry jcm:jcm1.entrySet()){ System.out.println(jcm.getKey()+" "+jcm.getValue()); } jcm1.remove(new Integer(04)); jcm1.remove(new Integer(05)); System.out.println("Deleted key and value of the Map"); for(Map.Entry jcm:jcm1.entrySet()){ System.out.println(jcm.getKey()+" "+jcm.getValue()); } }}
输出:
描述
带有数据类型示例和输出的集合映射如下。
代码:
import java.util.*; class JavaCollectionMap{ public static void main(String args[]){ Map<String, String> jcm = new HashMap<String, String>(); jcm.put ("A", "HashMap"); jcm.put ("B", "TreeMap"); jcm.put ("C", "LinkedHashMap"); System.out.println(jcm); Map<Integer, String > jcm1 = new HashMap<Integer, String>(); jcm1.put (01, "HashMap"); jcm1.put (02, "TreeMap"); jcm1.put (03, "LinkedHashMap"); System.out.println(jcm1); Map<Integer, Integer > jcm2 = new HashMap<Integer, Integer>(); jcm2.put (01, 71098223); jcm2.put (02, 89901232); jcm2.put (03, 98089921); System.out.println(jcm2); }}
输出:
描述
这是Java集合图指南。在这里,我们讨论 Java 集合映射方法如何工作以及示例和输出。您还可以查看以下文章以了解更多信息 –
以上是Java集合图的详细内容。更多信息请关注PHP中文网其他相关文章!