There are five cases for map iteration in the example.
main function for test:
1 2 3 4 5 6 7 8 9 10 11 12 | public static void main(String[] args) { Map<String,String> map = new TreeMap<>(); map.put( "one", "first"); map.put( "two", "second"); map.put( "three", "third"); printUsingIterator(map); printUsingKeySet(map); printUsingEntrySet(map); printUsingForEach(map); printUsingStream(map); } |
1. Map Iteration using keySet + Iterator
1 2 3 4 5 6 7 | static void printUsingIterator(Map<String,String> map) { Iterator<String> keys = map.keySet().iterator(); while( keys.hasNext() ){ String key = keys.next(); System.out.println( String.format("(%s -> %s)", key, map.get(key)) ); } } |
2. Map Iteration using keySet
1 2 3 4 5 | static void printUsingKeySet(Map<String,String> map) { for( String key : map.keySet() ) { System.out.println( String.format("(%s -> %s)", key, map.get(key)) ); } } |
3. Map Iteration using entrySet
1 2 3 4 5 | static void printUsingEntrySet(Map<String,String> map) { for( Map.Entry<String, String> m : map.entrySet() ){ System.out.println( String.format("(%s -> %s)", m.getKey(), m.getValue()) ); } } |
4. Map Iteration using forEach
1 2 3 | static void printUsingForEach(Map<String,String> map) { map.forEach( (k,v) -> System.out.println(String.format("(%s -> %s)", k, v )) ); } |
5. Map Iteration using stream + forEach
1 2 3 4 | static void printUsingStream(Map<String,String> map) { map.entrySet().stream() .forEach( m -> System.out.println(String.format("(%s -> %s)", m.getKey(), m.getValue()))); } |
Full Source:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package bloggerpostsnippetjava; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; public class Inerations { static void printUsingIterator(Map<String,String> map) { Iterator<String> keys = map.keySet().iterator(); while( keys.hasNext() ){ String key = keys.next(); System.out.println( String.format("(%s -> %s)", key, map.get(key)) ); } } static void printUsingKeySet(Map<String,String> map) { for( String key : map.keySet() ) { System.out.println( String.format("(%s -> %s)", key, map.get(key)) ); } } static void printUsingEntrySet(Map<String,String> map) { for( Map.Entry<String, String> m : map.entrySet() ){ System.out.println( String.format("(%s -> %s)", m.getKey(), m.getValue()) ); } } static void printUsingForEach(Map<String,String> map) { map.forEach( (k,v) -> System.out.println(String.format("(%s -> %s)", k, v )) ); } static void printUsingStream(Map<String,String> map) { map.entrySet().stream() .forEach( m -> System.out.println(String.format("(%s -> %s)", m.getKey(), m.getValue()))); } public static void main(String[] args) { Map<String,String> map = new TreeMap<>(); map.put( "one", "first"); map.put( "two", "second"); map.put( "three", "third"); printUsingIterator(map); printUsingKeySet(map); printUsingEntrySet(map); printUsingForEach(map); printUsingStream(map); } } |
Result:
(one -> first) (three -> third) (two -> second) ...
댓글 없음:
댓글 쓰기