Heim > Java > javaLernprogramm > Hauptteil

Java-Sammlungskarte

WBOY
Freigeben: 2024-08-30 15:46:23
Original
1028 Leute haben es durchsucht
  • The Java collection map is a method to store keys and values in pairs using java language.
  • It is a type of collection interface to operate data lists using their keys.
  • This is also a function to collect different data, classes, and methods using key values.
  • The collection map is based on the “java util” package to store, operate, and manage data list in key and value pair.
  • This collection map is an interface to insert, manage, and remove values using a unique key.
  • It helps to stores unique keys of value and each key of the map.
  • Also, is supports inserting, store, search, and sorting the data list or value using a map key.

Syntax

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • The java collection used three maps to operate key and values.
  • The category of the map is called HashMap, TreeMap, and LinkedHashMap.
  • The java collection HashMap syntax is below.
Map<key - data type, key - data type> object = new HashMap<key - data type, value - data type> ();
Nach dem Login kopieren
  • The java collection TreeMap syntax is below.
Map<key - data type, key - data type> object = new TreeMap<key - data type, value - data type> ();
Nach dem Login kopieren
  • The java collection LinkedHashMap syntax is below.
Map<key - data type, key - data type> object = new LinkedHashMap<key - data type, value - data type> ();
Nach dem Login kopieren
  • The java collection insert value in the map syntax is below.
Object.put (new data_type (key), value);
Nach dem Login kopieren
  • The java collection removes value from the map syntax is below.
Object.remove (new data_type (key));
Nach dem Login kopieren
  • Display the collection map syntax is below.
System.out.println(map_object);
Nach dem Login kopieren
  • The iterating collection map syntax is below.
for (Map.Entry temporary_object : main_object.entrySet()){
system.out.print(temporary_object.getKey() + "-" + temporary_object.getValue()  )
}
Nach dem Login kopieren

How does the Java collection map method work?

  • The key of the map must be unique and does not null.
  • Import the “java. util” package for the collection map.
import java.util.*;
Nach dem Login kopieren
  • Create a class with an initial capital letter and unique name.
public class JavaCollectionMap{ include variable, method, and object here… }
Nach dem Login kopieren
  • Create the main class to include a collection map and get output.
public static void main(String args[]){
write java collection map code here…
}
Nach dem Login kopieren
  • The write java collection map syntax here.
Map<String,String > jcm = new HashMap<String,String >();
Nach dem Login kopieren
  • The insert, remove or update map value syntax use as per requirement.
jcm.put("A", "HashMap");
Nach dem Login kopieren
  • The displays the output using simple syntax or iteration method.
System.out.println(jcm);
Nach dem Login kopieren
  • Combine all processes and get a working procedure of the collection map.
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);
}}
Nach dem Login kopieren

Examples

The following examples help you to understand insert, update, remove values from the collection map.

Example #1

The java collection map with insert value example and output are below.

Code:

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);
}}
Nach dem Login kopieren

Output:

Java-Sammlungskarte

Description

  • The map uses integer key and string value in the collection map.
  • The “put” keyword helps to add new keys and values.

Example #2

The collection map with iteration example and output is below.

Code:

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());
}
}}
Nach dem Login kopieren

Output:

Java-Sammlungskarte

Description

  • The map uses integer key and string value in the collection map.
  • The “for” loop uses to iterate value from the entire list.
  • The Entry and entrySet avoid the repetition of a similar algorithm.
  • The “getKey()” and “getValue()” helps to display key and value in the format.

Example #3

The collection map with change value example and output is below.

Code:

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());
}
}}
Nach dem Login kopieren

Output:

Java-Sammlungskarte

Description

  • The map uses integer key and string value in the collection map.
  • The “put” keyword helps to add and update values.
  • The key helps to change data from old to new.

Example #4

The collection map with delete value example and output is below.

Code:

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());
}
}}
Nach dem Login kopieren

Output:

Java-Sammlungskarte

Description

  • The map uses integer key and string value in the collection map.
  • The “remove” keyword helps to delete keys and values.
  • The key helps to delete available data on the map.
  • The map deletes key and value simultaneously.

Example #5

The collection map with data type’s example and output is below.

Code:

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);
}}
Nach dem Login kopieren

Output:

Java-Sammlungskarte

Description

  • Die erste Karte verwendet einen String-Datentyp für Schlüssel und Wert.
  • Die zweite Karte verwendet einen Ganzzahlschlüssel und einen Zeichenfolgenwert in der Sammlungskarte.
  • Die dritte Karte verwendet ganzzahlige Datentypen für Schlüssel und Wert.
  • Sie verwenden einen beliebigen Datentyp für Schlüssel- und Wert- und Speicherdaten.

Fazit

  • Die Sammlungskarte hilft beim einfachen Suchen, Sortieren und Aktualisieren von Daten.
  • Die Sammlungskarte verwaltet die Datenliste einfach über einen Schlüssel.
  • Die Karte vermeidet Wiederholungen und Komplexität der Listenoperation.

 Empfohlene Artikel

Dies ist eine Anleitung zur Java-Sammlungskarte. Hier diskutieren wir zusammen mit den Beispielen und Ausgaben, wie die Java-Collection-Map-Methode funktioniert. Sie können auch einen Blick auf die folgenden Artikel werfen, um mehr zu erfahren –

  1. JavaScript-Liste
  2. String in Java sortieren
  3. Javafx Scrollpane
  4. JavaScript querySelector

Das obige ist der detaillierte Inhalt vonJava-Sammlungskarte. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!