print map java
Java
map.forEach((key, value) -> System.out.println(key + ":" + value));import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
class MapUtil
{
// Program to print all keys present in the Map using keySet() in Java
public static void main (String[] args)
{
Map<Integer, String> map = new HashMap<>();
map.put(1, "One");
map.put(2, "Two");
// 1. using Iterator
Iterator<Integer> itr = map.keySet().iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
// 2. For-each Loop
for (Integer key : map.keySet()) {
System.out.println(key);
}
// 3. Java 8 - Collection.iterator() + Iterator.forEachRemaining()
map.keySet().iterator()
.forEachRemaining(System.out::println);
// 4. Java 8 - Collection.stream() + Stream.forEach()
map.keySet().stream()
.forEach(System.out::println);
// Java 8 - Stream.of() + Collection.toArray() + Stream.forEach()
Stream.of(map.keySet().toArray())
.forEach(System.out::println);
// 5. Convert to String
System.out.println(map.keySet().toString());
// Java 8
Stream.of(map.keySet().toString())
.forEach(System.out::println);
}
}
// Java 8 - Collection.iterator() + Iterator.forEachRemaining()
map.keySet().iterator()
.forEachRemaining(System.out::println);
Also in Java:
- Title
- how to count the number of occurrences of an element in a arraylist in java
- Category
- Java
- Title
- how to add a number to an array in java
- Category
- Java
- Title
- if statement java
- Category
- Java
- Title
- how to find substring in java
- Category
- Java
- Title
- java get folder content
- Category
- Java
- Title
- how to create an abstract method in java
- Category
- Java
- Title
- annotation spring notnull
- Category
- Java
- Title
- java random boolean
- Category
- Java
- Title
- filesaver javafx
- Category
- Java
- Title
- java android edit text set value
- Category
- Java
- Title
- java map geeksforgeeks
- Category
- Java
- Title
- convert char to string java
- Category
- Java
- Title
- pass list to intent in android java
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- java get annotation value
- Category
- Java
- Title
- java constructor genertic
- Category
- Java
- Title
- run java program maven
- Category
- Java
- Title
- how to substring in java
- Category
- Java
- Title
- how to add multiple filter condition in Java stream filter chain
- Category
- Java
- Title
- onbackpressed android
- Category
- Java
- Title
- iterator loop java
- Category
- Java
- Title
- change fab image programatically
- Category
- Java
- Title
- java random between two strings
- Category
- Java
- Title
- capitalize string java
- Category
- Java
- Title
- how to get the width and height of a string in java
- Category
- Java
- Title
- array string java in one line
- Category
- Java
- Title
- use regex in if statement java
- Category
- Java
- Title
- how to create a thread local variable in java
- Category
- Java
- Title
- how to iterate list in java selenium
- Category
- Java
- Title
- Java sort Map by values
- Category
- Java
- Title
- java letter alphabet index
- Category
- Java
- Title
- java list all non directory files in the directory
- Category
- Java
- Title
- executors java
- Category
- Java
- Title
- Java array nested equals
- Category
- Java
- Title
- loop collection android
- Category
- Java
- Title
- how to copy list item to another list in java
- Category
- Java
- Title
- how to print in java
- Category
- Java
- Title
- java best way to concatenate strings
- Category
- Java
- Title
- hide elements android
- Category
- Java
- Title
- declare String in java
- Category
- Java
- Title
- Filebody in java
- Category
- Java
- Title
- java import text file into arraylist
- Category
- Java
- Title
- java convert edittext to double
- Category
- Java
- Title
- even or odd in java
- Category
- Java
- Title
- java cast duration to long
- Category
- Java
- Title
- compile java to jar in mac
- Category
- Java
- Title
- date format in java
- Category
- Java
- Title
- sort array java
- Category
- Java
- Title
- java iterate through map
- Category
- Java
- Title
- if en une seul ligne java
- Category
- Java
- Title
- java output length of each line in a text document
- Category
- Java
- Title
- java array declaration
- Category
- Java
- Title
- system.out.println shortcut
- Category
- Java
- Title
- what is a float java
- Category
- Java
- Title
- generate objects with for loop java
- Category
- Java
- Title
- java fullscreen jframe
- Category
- Java
- Title
- diagonal difference hackerrank solution in java 8 using list
- Category
- Java
- Title
- how to get all the names of the files in a folder in java?
- Category
- Java
- Title
- java remove first character from string
- Category
- Java
- Title
- file to image javafx
- Category
- Java
- Title
- java read integer from text file into array scanner
- Category
- Java
- Title
- remove item from arraylist in java
- Category
- Java
- Title
- java how to make a gui
- Category
- Java
- Title
- java loop through arraylist
- Category
- Java
- Title
- java replace all html tags
- Category
- Java
- Title
- abs in java
- Category
- Java
- Title
- jbutton open jframe java
- Category
- Java
- Title
- caesar cipher java
- Category
- Java
- Title
- find frequency of each word in a string in java
- Category
- Java
- Title
- java do while
- Category
- Java
- Title
- get file path java
- Category
- Java
- Title
- java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: java.security.MessageDigest$Delegate
- Category
- Java
- Title
- tolowercase java
- Category
- Java
- Title
- java timestamp
- Category
- Java
- Title
- Scanner library showing element not found exception
- Category
- Java
- Title
- lightweight java game library
- Category
- Java
- Title
- bukkit java connect player to another server in bungeecord
- Category
- Java
- Title
- java test if a string is a int
- Category
- Java
- Title
- java binary exponentiation
- Category
- Java
- Title
- type javascirpt
- Category
- Java
- Title
- @fortawesome/free-solid-svg-icons
- Category
- Java
- Title
- waht does&& mean in java
- Category
- Java
- Title
- java execute jar from main
- Category
- Java
- Title
- java boolean even number
- Category
- Java
- Title
- testing the web layer without authentication spring
- Category
- Java
- Title
- how get started with LWJGL 3
- Category
- Java
- Title
- java if different ways
- Category
- Java
- Title
- java set value of arraylist
- Category
- Java
- Title
- how to calculate exponential in java
- Category
- Java
- Title
- libgdx move camera with touch
- Category
- Java
- Title
- how to make an arraylist java
- Category
- Java
- Title
- how to declare array java
- Category
- Java
- Title
- import
- Category
- Java
- Title
- iterate through hashmap in java
- Category
- Java
- Title
- How to loop through objects in java using streams
- Category
- Java
- Title
- springBoot
- Category
- Java
- Title
- java list to array
- Category
- Java
- Title
- int to string java
- Category
- Java
- Title
- how do i print text in java
- Category
- Java
- Title
- how to make one java class inherit from another
- Category
- Java