Rumah >Java >javaTutorial >Peta koleksi Java
Sintaks
Mulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
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); }}
Contoh berikut membantu anda memahami memasukkan, mengemas kini, mengalih keluar nilai daripada peta koleksi.
Peta koleksi java dengan contoh nilai sisipan dan output ada di bawah.
Kod:
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); }}
Output:
Penerangan
Peta koleksi dengan contoh lelaran dan output adalah di bawah.
Kod:
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()); } }}
Output:
Penerangan
Peta koleksi dengan contoh nilai perubahan dan output adalah di bawah.
Kod:
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()); } }}
Output:
Penerangan
Peta koleksi dengan contoh nilai padam dan output adalah di bawah.
Kod:
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()); } }}
Output:
Penerangan
Peta koleksi dengan contoh dan output jenis data adalah di bawah.
Kod:
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); }}
Output:
Penerangan
Ini adalah panduan untuk peta koleksi Java. Di sini kita membincangkan Bagaimana kaedah peta koleksi Java berfungsi bersama-sama dengan contoh dan output. Anda juga boleh melihat artikel berikut untuk mengetahui lebih lanjut –
Atas ialah kandungan terperinci Peta koleksi Java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!