CodeGym /Blog Jawa /Acak /Cara nggunakake metode entrySet () ing Jawa
John Squirrels
tingkat
San Francisco

# Cara nggunakake metode entrySet () ing Jawa

## Apa metode entrySet () ing Jawa?

Kelas HashMap nyedhiyakake metode java.util.HashMap.entrySet () ing Jawa. Iki digunakake kanggo nggawe banjur ngasilake 'set' saka unsur sing padha sing wis ana ing HashMap . Bisa digunakake kanthi daur ulang kanggo ngulang kabeh entri HashMap .

Header metode entrySet () diwenehi ing ngisor iki. Iki ngasilake tampilan set kabeh entri sing ngemot pasangan kunci-nilai. Kanggo nggunakake ing kode kita kudu ngimpor paket java.util.HashMap .
``````
public Set<Map.Entry<key, value>> entrySet()
``````

## Paramèter

Cara entrySet () ora njupuk paramèter.

## Jinis bali

Cara java.util.HashMap.entrySet () ngasilake conto saka set kelas.

``````
import java.util.HashMap;

public class Driver1 {

public static void main(String[] args) {

// declare a custom hash map
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

// add data to the hash map
hashMap.put(1, "Monday");
hashMap.put(2, "Tuesday");
hashMap.put(3, "Wednesday");
hashMap.put(4, "Thursday");
hashMap.put(5, "Friday");
hashMap.put(6, "Saturday");
hashMap.put(7, "Sunday");

// print the original hash map
System.out.println("Original HashMap: " + hashMap + '\n');
// print the entrySet of the hash map
System.out.println("HashMap.entrySet(): " + hashMap.entrySet() + '\n');

// Try adding null value in the hash map
hashMap.put(0, null);
System.out.println("hashMap.put(0, null)");
System.out.println("HashMap.entrySet(): " + hashMap.entrySet() + '\n');

// Try adding null key and value pair to the hash map
hashMap.put(null, null);
System.out.println("hashMap.put(null, null)");
System.out.println("HashMap.entrySet(): " + hashMap.entrySet() + '\n');

// Try adding a null character as a value in the hash map
hashMap.put(null, "\0");
System.out.println("hashMap.put(null, \"\\0\")");
System.out.println("HashMap.entrySet(): " + hashMap.entrySet() + '\n');

}
}
``````

### Output

HashMap Asli: {1=Senin, 2=Selasa, 3=Rebo, 4=Kemis, 5=Jumuah, 6=Sabtu, 7=Minggu} HashMap.entrySet(): [1=Senin, 2=Selasa, 3=Rebo , 4 = Kamis, 5 = Jum'at, 6 = Setu, 7 = Minggu] hashMap.put (0, null) HashMap.entrySet (): [0 = null, 1 = Senin, 2 = Selasa, 3 = Rebo, 4 = Kamis, 5 = Jum'at, 6 = Setu, 7 = Minggu] hashMap.put(null, null) HashMap.entrySet(): [0=null, null=null, 1=Senin, 2=Selasa, 3=Rebo, 4 = Kamis, 5 = Jumuah, 6 = Setu, 7 = Minggu] hashMap.put(null, "\0") HashMap.entrySet(): [0=null, null=, 1=Senin, 2=Selasa, 3= Rebo, 4 = Kemis, 5 = Jumuah, 6 = Setu, 7 = Minggu]

## Panjelasan

Ing cuplikan kode ing ndhuwur, kita wis ngimpor paket java.util.HashMap dhisik . Iki ngidini kita nggunakake metode HashMap lan entrySet () . Banjur nggawe hashMap sing dadi obyek saka kelas HashMap . HashMap kita ngemot strings minangka nilai. Tombol iku integer. Kita banjur ngisi hashMap . Ana pitung entri total. Kita banjur nggunakake setEntry () cara kanggo bali tampilan pesawat lan banjur print ing console.

## Kesimpulan

Iki minangka implementasine prasaja saka metode Java HashMap entrySet() . Muga-muga sampeyan ngerti panggunaan metode kasebut sawise ngliwati kiriman iki. Kaya biasane, sampeyan dianjurake kanggo latihan bola-bali supaya bisa dadi apik. Nganti saiki, terus latihan lan terus berkembang!
Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION