Wednesday, 15 August 2012

java - Streams Collections.toMap() from List . How to keep the order? -


I create maps from the list as follows:

  Maps & lt; String, item variant & gt; Map = itemExt.getVariants () .stream (). Map ((extVar) - & gt; {// Convert Variety ItemVariator ItemWire = Conversion Service. Convert (extVar, ItemVariant.class); Return ItemWire;}). Moderator (.mdvierient :: mill sq, function .twitter; itemwidth & gt; detection ()););  

I want to place the same order as the list: itemExt.getVariants ()

. These collectors How to Create LinkedHashMap Using Map () Tasks?

2 parameter version collectors. To map () does the following:

/ P>
  public stable  Collector and lieutenant; T,?, Map & lt; K, U & gt; & Gt; Return to the ToMap (Function & Lt; Super T, K> Keymapers, Functions & lt; Super T, Spread U & gt; Value Mapper) (Return to Market (Main Mapper, Valuemapper, FencingMarger (), Hashmap :: New};}  

then you can replace it:

  collectors. (ItemWrint :: Milenska, Function.  

with:

  Collectors.toMap (ItemVariant :: getSku, & lt; ItemVariant & gt; Identification of the function (), (U, V) - & gt; {New IllegalStateException Throw (String.Format ("Key% s Duplicate", U));} LinkedHashMap :: New) or to make it a bit cleaner, about a new toLinkedMap () Method:  
  Public class morecliclers {public static  gt; collector & lt; t,?, Map & lt; k, u & gt; & Gt; String {New IllegalStateException Throw (; -TellinkedMap (? & Lt;? Super T, K & gt; spreads; keyMapper, for function & lt;? SuperT, spreads u & gt; valueMapper?) & Gt { Return Collectors.toMap (keyMapper, valueMapper, (U, V) .format (" Duplicate key% s ", u));}, linkedhash: new:); }}  

and use it.


No comments:

Post a Comment