22import java .util .AbstractMap .SimpleEntry ;
33import java .util .ArrayList ;
44import java .util .Collections ;
5+ import java .util .List ;
56import java .util .Map .Entry ;
7+ import java .util .stream .Collectors ;
68
79/**
810 * @author GDS-PDD
@@ -29,19 +31,25 @@ public String toString() {
2931
3032 public String toString (String delimiter , Boolean sort , Boolean quote )
3133 {
32- ArrayList <String > list = new ArrayList <String >();
33-
34- String format = "%s=%s" ;
35- if (quote ) format = "%s=\" %s\" " ;
36-
37- for (Entry <String , String > item : this )
38- {
39- list .add (String .format (format , item .getKey (), item .getValue ()));
34+ List <String > list = new ArrayList <String >();
35+
36+ final String format = (quote ? "%s=\" %s\" " : "%s=%s" );
37+
38+ /* Sort key first then value*/
39+ if (sort ){
40+ list = this .stream ()
41+ .sorted ((Entry <String ,String > l1 , Entry <String ,String > l2 ) ->
42+ {
43+ return l1 .getKey ().equals (l2 .getKey ()) ? l1 .getValue ().compareTo (l2 .getValue ())
44+ : l1 .getKey ().compareTo (l2 .getKey ());
45+ })
46+ .map (e -> String .format (format , e .getKey (), e .getValue ()))
47+ .collect (Collectors .toList ());
48+ } else {
49+ list = this .stream ().map (e -> String .format (format , e .getKey (), e .getValue ()))
50+ .collect (Collectors .toList ());
4051 }
41-
42- /* Sort statement*/
43- if (sort ) Collections .sort (list );
44-
52+
4553 return String .join (delimiter , list );
4654 }
4755}
0 commit comments