MESSAGE
DATE | 2016-05-22 |
FROM | Ruben Safir
|
SUBJECT | Subject: [Hangout-NYLXS] Fwd: Re: how does one explain this syntax?
|
-------- Forwarded Message -------- Subject: Re: how does one explain this syntax? Date: 22 May 2016 21:25:40 GMT From: Stefan Ram Organization: Stefan Ram Newsgroups: comp.lang.java.programmer References:
ruben safir writes: >"); < The problems in programming often lie elsewhere, but, FWIW, when one is often dealing with tree maps and is repelled by the boilerplater, one can hide most of the boilerplate into a utility class. The normal program then can look like this:
public final class Main { public static void main( final java.lang.String[] args ) { final TreeMapUtil< java.lang.Integer, java.lang.String >util = new TreeMapUtil<>(); util.withTreeMapDo ( map -> { map.put( 1, "Data1" ); map.put( 23, "Data2" ); map.put( 70, "Data3" ); map.put ( 4, "Data4" ); map.put( 2, "Data5" ); util.withEntriesLoop( map, ( k, v )-> java.lang.System.out.printf ( "key is: %2d & Value is: %s%n", k, v )); } ); }}
The boilerplate needs to be hidden somewhere:
class TreeMapUtil< I, S > { void withTreeMapDo ( final java.util.function.Consumer < java.util.TreeMap< I, S >> consumer ) { java.util.TreeMap< I, S > map = new java.util.TreeMap<>(); consumer.accept( map ); } void withEntriesLoop ( java.util.TreeMap< I, S > map, final java.util.function.BiConsumer< I, S > consumer ) { final java.util.Set< java.util.Map.Entry< I, S >>set = map.entrySet(); final java.util.Iterator< java.util.Map.Entry< I, S >>iterator = set.iterator(); while( iterator.hasNext() ) { final java.util.Map.Entry< I, S >entry = iterator.next(); consumer.accept( entry.getKey(), entry.getValue() ); }}}
_______________________________________________ hangout mailing list hangout-at-nylxs.com http://www.nylxs.com/
|
|