2
2
import java .util .AbstractMap .SimpleEntry ;
3
3
import java .util .ArrayList ;
4
4
import java .util .Collections ;
5
+ import java .util .List ;
5
6
import java .util .Map .Entry ;
7
+ import java .util .stream .Collectors ;
6
8
7
9
/**
8
10
* @author GDS-PDD
@@ -29,19 +31,25 @@ public String toString() {
29
31
30
32
public String toString (String delimiter , Boolean sort , Boolean quote )
31
33
{
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 ());
40
51
}
41
-
42
- /* Sort statement*/
43
- if (sort ) Collections .sort (list );
44
-
52
+
45
53
return String .join (delimiter , list );
46
54
}
47
55
}
0 commit comments