Skip to content

Commit a3ac0fa

Browse files
committed
improve
1 parent 078da0d commit a3ac0fa

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@ Programming toolkit to avoid reinventing the wheel
2929
<artifactId>gson</artifactId>
3030
<version>2.8.6</version>
3131
</dependency>
32+
<!-- https://mvnrepository.com/artifact/org.eclipse.jdt/org.eclipse.jdt.core -->
33+
<dependency>
34+
<groupId>org.eclipse.jdt</groupId>
35+
<artifactId>org.eclipse.jdt.core</artifactId>
36+
<version>3.24.0</version>
37+
</dependency>
3238
```

lib/utils/CSVUtil.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88

99
public class CSVUtil {
1010

11-
public static void writeLine2Csv(List<String> strList, Path filePath) {
11+
public static void writeLine2Csv(List<String> strList, Path filePath, Boolean ifAppendNewLine) {
1212
try {
1313
FileWriter writer = new FileWriter(filePath.toString());
1414

1515
String collect = strList.stream().collect(Collectors.joining(","));
16+
if (ifAppendNewLine) {
17+
collect += "\n";
18+
}
1619
System.out.println(collect);
1720

1821
writer.write(collect);
@@ -23,9 +26,12 @@ public static void writeLine2Csv(List<String> strList, Path filePath) {
2326
}
2427
}
2528

26-
public static void appendLine2Csv(List<String> strList, Path filePath) {
29+
public static void appendLine2Csv(List<String> strList, Path filePath, Boolean ifAppendNewLine) {
2730
try {
2831
String collect = strList.stream().collect(Collectors.joining(","));
32+
if (ifAppendNewLine) {
33+
collect += "\n";
34+
}
2935
// System.out.println(collect);
3036

3137
// pass true for appending
@@ -85,10 +91,10 @@ public static void write2DArray2Csv(List<List<String>> strList, Path filePath) {
8591
* read csv line by line and parse single line to this function code example
8692
* String csvFile = "/Users/mkyong/csv/country2.csv";
8793
*
88-
* Scanner scanner = new Scanner(new File(csvFile)); while
89-
* (scanner.hasNext()) { List<String> line = parseLine(scanner.nextLine());
90-
* System.out.println("Country [id= " + line.get(0) + ", code= " +
91-
* line.get(1) + " , name=" + line.get(2) + "]"); } scanner.close();
94+
* Scanner scanner = new Scanner(new File(csvFile)); while (scanner.hasNext()) {
95+
* List<String> line = parseLine(scanner.nextLine());
96+
* System.out.println("Country [id= " + line.get(0) + ", code= " + line.get(1) +
97+
* " , name=" + line.get(2) + "]"); } scanner.close();
9298
*
9399
* @param cvsLine
94100
* @param separator

lib/utils/CollectionUtil.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,30 @@
55
public class CollectionUtil {
66

77
/**
8-
* https://stackoverflow.com/a/109389/4315608
9-
*
8+
* https://stackoverflow.com/a/109389/4315608 asc or desc
9+
*
1010
* @param map
1111
* @param <K>
1212
* @param <V>
1313
* @return
1414
*/
15-
public static <K, V> Map<K, V> sortByValue(Map<K, V> map) {
15+
public static <K, V> Map<K, V> sortByValue(Map<K, V> map, String order) {
16+
1617
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
1718
Collections.sort(list, new Comparator<Object>() {
1819
@SuppressWarnings("unchecked")
1920
public int compare(Object o1, Object o2) {
20-
return ((Comparable<V>) ((Map.Entry<K, V>) (o1)).getValue())
21-
.compareTo(((Map.Entry<K, V>) (o2)).getValue());
21+
if (order.equals("asc")) {
22+
return ((Comparable<V>) ((Map.Entry<K, V>) (o1)).getValue())
23+
.compareTo(((Map.Entry<K, V>) (o2)).getValue());
24+
} else if (order.equals("desc")) {
25+
return ((Comparable<V>) ((Map.Entry<K, V>) (o2)).getValue())
26+
.compareTo(((Map.Entry<K, V>) (o1)).getValue());
27+
} else {
28+
System.out.println("No such order!");
29+
System.exit(0);
30+
return 0;
31+
}
2232
}
2333
});
2434

@@ -29,6 +39,7 @@ public int compare(Object o1, Object o2) {
2939
}
3040

3141
return result;
42+
3243
}
3344

3445
public static Set<Object> mergeMultipleSet(List<Set<Object>> multiSet) {

lib/utils/MathUtil.java

100644100755
File mode changed.

0 commit comments

Comments
 (0)