• Java Coding Problems Second Edition --chapter 01


    第一章

    1、节字符串连接

    1、在jdk 8之前版本,可以使用 + 连接字符串

    2、StringBuilder 进行append操作

    3、string.concat进行操作

    4、string format进行插空

    5、jdk 8 s可以使用String ,join 进行字符串连接

    6、java.util.StringJoiner 操作

    7、使用 StringJoiner

    8、 using Collectors#joining()

    9、Starting with JDK 13/15using text blocks

    1. package chaptor01.P01_TextBlockSQLJSONHTML;
    2. import java.util.StringJoiner;
    3. import java.util.stream.Collectors;
    4. import java.util.stream.Stream;
    5. public class Main {
    6. public static void main(String[] args) {
    7. //字符串连接
    8. // 在jdk 8 之前,我们使用+进行字符串连接
    9. System.out.println("Multiline SQL/JSON/HTML before text blocks "
    10. + "(using concatenation via the '+' operator):");
    11. String sql1
    12. = "UPDATE \"public\".\"office\"\n"
    13. + "SET (\"address_first\", \"address_second\", \"phone\") =\n"
    14. + " (SELECT \"public\".\"employee\".\"first_name\",\n"
    15. + " \"public\".\"employee\".\"last_name\", ?\n"
    16. + " FROM \"public\".\"employee\"\n"
    17. + " WHERE \"public\".\"employee\".\"job_title\" = ?";
    18. String json1
    19. = "{\n"
    20. + " \"widget\": {\n"
    21. + " \"debug\": \"on\",\n"
    22. + " \"window\": {\n"
    23. + " \"title\": \"Sample Widget 1\",\n"
    24. + " \"name\": \"back_window\"\n"
    25. + " },\n"
    26. + " \"image\": {\n"
    27. + " \"src\": \"images\\sw.png\"\n"
    28. + " },\n"
    29. + " \"text\": {\n"
    30. + " \"data\": \"Click Me\",\n"
    31. + " \"size\": 39\n"
    32. + " }\n"
    33. + " }\n"
    34. + "}";
    35. String html1
    36. = "\n"
    37. + "
    38. \n"
    39. + "
    40. \n"
    41. + "
    42. \n"
    43. + "
    44. \n"
    45. + "
    46. \n"
    47. + "
    48. \n"
    49. + "
    50. \n"
    51. + "
    52. \n"
    53. + "
    54. \n"
    55. + "
    56. NameAge
      JohnSmith22
      ";
    57. System.out.println(sql1);
    58. System.out.println();
    59. System.out.println(json1);
    60. System.out.println();
    61. System.out.println(html1);
    62. // 我们同样可以 StringBuilder 连接 append 连接
    63. System.out.println("\n Multiline SQL/JSON/HTML before text blocks (using StringBuilder):");
    64. StringBuilder sql2 = new StringBuilder();
    65. sql2.append("UPDATE \"public\".\"office\"\n")
    66. .append("SET (\"address_first\", \"address_second\", \"phone\") =\n")
    67. .append(" (SELECT \"public\".\"employee\".\"first_name\",\n")
    68. .append(" \"public\".\"employee\".\"last_name\", ?\n")
    69. .append(" FROM \"public\".\"employee\"\n")
    70. .append(" WHERE \"public\".\"employee\".\"job_title\" = ?;");
    71. StringBuilder json2 = new StringBuilder();
    72. json2.append("{\n")
    73. .append(" \"widget\": {\n")
    74. .append(" \"debug\": \"on\",\n")
    75. .append(" \"window\": {\n")
    76. .append(" \"title\": \"Sample Widget 1\",\n")
    77. .append(" \"name\": \"back_window\"\n")
    78. .append(" },\n")
    79. .append(" \"image\": {\n")
    80. .append(" \"src\": \"images\\sw.png\"\n")
    81. .append(" },\n")
    82. .append(" \"text\": {\n")
    83. .append(" \"data\": \"Click Me\",\n")
    84. .append(" \"size\": 39\n")
    85. .append(" }\n")
    86. .append(" }\n")
    87. .append("}");
    88. StringBuilder html2 = new StringBuilder();
    89. html2.append("
    90. \n")
    91. .append("
    92. \n")
    93. .append("
    94. \n")
    95. .append("
    96. \n")
    97. .append("
    98. \n")
    99. .append("
    100. \n")
    101. .append("
    102. \n")
    103. .append("
    104. \n")
    105. .append("
    106. \n")
    107. .append("
    108. \n")
    109. .append("
    110. NameAge
      JohnSmith22
      ");
    111. System.out.println(sql2);
    112. System.out.println();
    113. System.out.println(json2);
    114. System.out.println();
    115. System.out.println(html2);
    116. // using String#concat()
    117. System.out.println("\n Multiline SQL/JSON/HTML before text blocks (using String#concat()):");
    118. String sql3 = "UPDATE \"public\".\"office\"\n"
    119. .concat("SET (\"address_first\", \"address_second\", \"phone\") =\n")
    120. .concat(" (SELECT \"public\".\"employee\".\"first_name\",\n")
    121. .concat(" \"public\".\"employee\".\"last_name\", ?\n")
    122. .concat(" FROM \"public\".\"employee\"\n")
    123. .concat(" WHERE \"public\".\"employee\".\"job_title\" = ?;");
    124. String json3 = "{\n"
    125. .concat(" \"widget\": {\n")
    126. .concat(" \"debug\": \"on\",\n")
    127. .concat(" \"window\": {\n")
    128. .concat(" \"title\": \"Sample Widget 1\",\n")
    129. .concat(" \"name\": \"back_window\"\n")
    130. .concat(" },\n")
    131. .concat(" \"image\": {\n")
    132. .concat(" \"src\": \"images\\sw.png\"\n")
    133. .concat(" },\n")
    134. .concat(" \"text\": {\n")
    135. .concat(" \"data\": \"Click Me\",\n")
    136. .concat(" \"size\": 39\n")
    137. .concat(" }\n")
    138. .concat(" }\n")
    139. .concat("}");
    140. String html3 = "
    141. \n"
    142. .concat("
    143. \n")
    144. .concat("
    145. \n")
    146. .concat("
    147. \n")
    148. .concat("
    149. \n")
    150. .concat("
    151. \n")
    152. .concat("
    153. \n")
    154. .concat("
    155. \n")
    156. .concat("
    157. \n")
    158. .concat("
    159. \n")
    160. .concat("
    161. NameAge
      JohnSmith22
      ");
    162. System.out.println(sql3);
    163. System.out.println();
    164. System.out.println(json3);
    165. System.out.println();
    166. System.out.println(html3);
    167. // 使用 string format连接
    168. System.out.println("\n Multiline SQL/JSON/HTML before text blocks (using String#format()):");
    169. String sql4 = String.format("%s%s%s%s%s%s",
    170. "UPDATE \"public\".\"office\"\n",
    171. "SET (\"address_first\", \"address_second\", \"phone\") =\n",
    172. " (SELECT \"public\".\"employee\".\"first_name\",\n",
    173. " \"public\".\"employee\".\"last_name\", ?\n",
    174. " FROM \"public\".\"employee\"\n",
    175. " WHERE \"public\".\"employee\".\"job_title\" = ?;");
    176. String json4 = String.format("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
    177. "{\n",
    178. " \"widget\": {\n",
    179. " \"debug\": \"on\",\n",
    180. " \"window\": {\n",
    181. " \"title\": \"Sample Widget 1\",\n",
    182. " \"name\": \"back_window\"\n",
    183. " },\n",
    184. " \"image\": {\n",
    185. " \"src\": \"images\\sw.png\"\n",
    186. " },\n",
    187. " \"text\": {\n",
    188. " \"data\": \"Click Me\",\n",
    189. " \"size\": 39\n",
    190. " }\n",
    191. " }\n",
    192. "}");
    193. String html4 = String.format("%s%s%s%s%s%s%s%s%s%s%s",
    194. "
    195. \n",
    196. "
    197. \n",
    198. "
    199. \n",
    200. "
    201. \n",
    202. "
    203. \n",
    204. "
    205. \n",
    206. "
    207. \n",
    208. "
    209. \n",
    210. "
    211. \n",
    212. "
    213. \n",
    214. "
    215. NameAge
      JohnSmith22
      ");
    216. System.out.println(sql4);
    217. System.out.println();
    218. System.out.println(json4);
    219. System.out.println();
    220. System.out.println(html4);
    221. // jdk 8 s可以使用String ,join 进行字符串连接
    222. System.out.println("\n Multiline SQL/JSON/HTML before text blocks (using String#join()):");
    223. String sql5 = String.join("\n",
    224. "UPDATE \"public\".\"office\"",
    225. "SET (\"address_first\", \"address_second\", \"phone\") =",
    226. " (SELECT \"public\".\"employee\".\"first_name\",",
    227. " \"public\".\"employee\".\"last_name\", ?",
    228. " FROM \"public\".\"employee\"",
    229. " WHERE \"public\".\"employee\".\"job_title\" = ?;");
    230. String json5 = String.join("\n",
    231. "{",
    232. " \"widget\": {",
    233. " \"debug\": \"on\",",
    234. " \"window\": {",
    235. " \"title\": \"Sample Widget 1\",",
    236. " \"name\": \"back_window\"",
    237. " },",
    238. " \"image\": {",
    239. " \"src\": \"images\\sw.png\"",
    240. " },",
    241. " \"text\": {",
    242. " \"data\": \"Click Me\",",
    243. " \"size\": 39",
    244. " }",
    245. " }",
    246. "}");
    247. String html5 = String.join("\n",
    248. "
    249. ",
    250. "
    251. ",
    252. "
    253. ",
    254. "
    255. ",
    256. "
    257. ",
    258. "
    259. ",
    260. "
    261. ",
    262. "
    263. ",
    264. "
    265. ",
    266. "
    267. ",
    268. "
    269. NameAge
      JohnSmith22
      ");
    270. System.out.println(sql5);
    271. System.out.println();
    272. System.out.println(json5);
    273. System.out.println();
    274. System.out.println(html5);
    275. // 使用 StringJoiner
    276. System.out.println("\n Multiline SQL/JSON/HTML before text blocks (using StringJoiner):");
    277. StringJoiner sql6 = new StringJoiner("\n");
    278. sql6.add("UPDATE \"public\".\"office\"")
    279. .add("SET (\"address_first\", \"address_second\", \"phone\") =")
    280. .add(" (SELECT \"public\".\"employee\".\"first_name\",")
    281. .add(" \"public\".\"employee\".\"last_name\", ?")
    282. .add(" FROM \"public\".\"employee\"")
    283. .add(" WHERE \"public\".\"employee\".\"job_title\" = ?;");
    284. StringJoiner json6 = new StringJoiner("\n");
    285. json6.add("{")
    286. .add(" \"widget\": {")
    287. .add(" \"debug\": \"on\",")
    288. .add(" \"window\": {")
    289. .add(" \"title\": \"Sample Widget 1\",")
    290. .add(" \"name\": \"back_window\"")
    291. .add(" },")
    292. .add(" \"image\": {")
    293. .add(" \"src\": \"images\\sw.png\"")
    294. .add(" },")
    295. .add(" \"text\": {")
    296. .add(" \"data\": \"Click Me\",")
    297. .add(" \"size\": 39")
    298. .add(" }")
    299. .add(" }")
    300. .add("}");
    301. StringJoiner html6 = new StringJoiner("\n");
    302. html6.add("
    303. ")
    304. .add("
    305. ")
    306. .add("
    307. ")
    308. .add("
    309. ")
    310. .add("
    311. ")
    312. .add("
    313. ")
    314. .add("
    315. ")
    316. .add("
    317. ")
    318. .add("
    319. ")
    320. .add("
    321. ")
    322. .add("
    323. NameAge
      JohnSmith22
      ");
    324. System.out.println(sql6);
    325. System.out.println();
    326. System.out.println(json6);
    327. System.out.println();
    328. System.out.println(html6);
    329. // using Collectors#joining()
    330. System.out.println("\n Multiline SQL/JSON/HTML before text blocks (using Collectors#joining()):");
    331. String sql7 = Stream.of(
    332. "UPDATE \"public\".\"office\"",
    333. "SET (\"address_first\", \"address_second\", \"phone\") =",
    334. " (SELECT \"public\".\"employee\".\"first_name\",",
    335. " \"public\".\"employee\".\"last_name\", ?",
    336. " FROM \"public\".\"employee\"",
    337. " WHERE \"public\".\"employee\".\"job_title\" = ?;")
    338. .collect(Collectors.joining(String.valueOf("\n")));
    339. String json7 = Stream.of(
    340. "{",
    341. " \"widget\": {",
    342. " \"debug\": \"on\",",
    343. " \"window\": {",
    344. " \"title\": \"Sample Widget 1\",",
    345. " \"name\": \"back_window\"",
    346. " },",
    347. " \"image\": {",
    348. " \"src\": \"images\\sw.png\"",
    349. " },",
    350. " \"text\": {",
    351. " \"data\": \"Click Me\",",
    352. " \"size\": 39",
    353. " }",
    354. " }",
    355. "}")
    356. .collect(Collectors.joining(String.valueOf("\n")));
    357. String html7 = Stream.of(
    358. "
    359. ",
    360. "
    361. ",
    362. "
    363. ",
    364. "
    365. ",
    366. "
    367. ",
    368. "
    369. ",
    370. "
    371. ",
    372. "
    373. ",
    374. "
    375. ",
    376. "
    377. ",
    378. "
    379. NameAge
      JohnSmith22
      ")
    380. .collect(Collectors.joining(String.valueOf("\n")));
    381. System.out.println(sql7);
    382. System.out.println();
    383. System.out.println(json7);
    384. System.out.println();
    385. System.out.println(html7);
    386. // Starting with JDK 13/15
    387. // using text blocks
    388. System.out.println("\n Multiline SQL/JSON/HTML using text blocks:");
    389. String sql8 = """
    390. UPDATE "public"."office"
    391. SET ("address_first", "address_second", "phone") =
    392. (SELECT "public"."employee"."first_name",
    393. "public"."employee"."last_name", ?
    394. FROM "public"."employee"
    395. WHERE "public"."employee"."job_title" = ?""";
    396. String json8 = """
    397. {
    398. "widget": {
    399. "debug": "on",
    400. "window": {
    401. "title": "Sample Widget 1",
    402. "name": "back_window"
    403. },
    404. "image": {
    405. "src": "images\\sw.png"
    406. },
    407. "text": {
    408. "data": "Click Me",
    409. "size": 39
    410. }
    411. }
    412. }""";
    413. String html8 = """
    414. NameAge
      JohnSmith22
      """;
    415. System.out.println(sql8);
    416. System.out.println();
    417. System.out.println(json8);
    418. System.out.println();
    419. System.out.println(html8);
    420. }
    421. }
    422. 2、举例说明文本块分隔符的用法

      这个语法跟python的类似

      1. public class Main {
      2. public static void main(String[] args) {
      3. System.out.println("The common usage of text block delimiters:\n");
      4. String sql1 = """
      5. UPDATE "public"."office"
      6. SET ("address_first", "address_second", "phone") =
      7. (SELECT "public"."employee"."first_name",
      8. "public"."employee"."last_name", ?
      9. FROM "public"."employee"
      10. WHERE "public"."employee"."job_title" = ?""";
      11. System.out.println("-- BEFORE TEXT BLOCK --");
      12. System.out.println(sql1);
      13. System.out.println("-- AFTER TEXT BLOCK --");
      14. System.out.println("\nKeep the opening delimiter and the content on the "
      15. + "same line is not accepted (does not compile)\n");
      16. /*
      17. String not_compile = """UPDATE "public"."office"
      18. SET ("address_first", "address_second", "phone") =
      19. (SELECT "public"."employee"."first_name",
      20. "public"."employee"."last_name", ?
      21. FROM "public"."employee"
      22. WHERE "public"."employee"."job_title" = ?""";
      23. */
      24. System.out.println("\nShift-left the content has no effect:\n");
      25. String sql2 = """
      26. UPDATE "public"."office"
      27. SET ("address_first", "address_second", "phone") =
      28. (SELECT "public"."employee"."first_name",
      29. "public"."employee"."last_name", ?
      30. FROM "public"."employee"
      31. WHERE "public"."employee"."job_title" = ?""";
      32. System.out.println("-- BEFORE TEXT BLOCK --");
      33. System.out.println(sql2);
      34. System.out.println("-- AFTER TEXT BLOCK --");
      35. System.out.println("\nShift-right the opening/closing delimiter has no effect:\n");
      36. String sql3 = """
      37. UPDATE "public"."office"
      38. SET ("address_first", "address_second", "phone") =
      39. (SELECT "public"."employee"."first_name",
      40. "public"."employee"."last_name", ?
      41. FROM "public"."employee"
      42. WHERE "public"."employee"."job_title" = ? """;
      43. System.out.println("-- BEFORE TEXT BLOCK --");
      44. System.out.println(sql3);
      45. System.out.println("-- AFTER TEXT BLOCK --");
      46. System.out.println("\nMoving the closing delimiter on its own line (1):\n");
      47. String sql4 = """
      48. UPDATE "public"."office"
      49. SET ("address_first", "address_second", "phone") =
      50. (SELECT "public"."employee"."first_name",
      51. "public"."employee"."last_name", ?
      52. FROM "public"."employee"
      53. WHERE "public"."employee"."job_title" = ?
      54. """;
      55. System.out.println("-- BEFORE TEXT BLOCK --");
      56. System.out.println(sql4);
      57. System.out.println("-- AFTER TEXT BLOCK --");
      58. System.out.println("\nMoving the closing delimiter on its own line (2):\n");
      59. String sql5 = """
      60. UPDATE "public"."office"
      61. SET ("address_first", "address_second", "phone") =
      62. (SELECT "public"."employee"."first_name",
      63. "public"."employee"."last_name", ?
      64. FROM "public"."employee"
      65. WHERE "public"."employee"."job_title" = ?
      66. """;
      67. System.out.println("-- BEFORE TEXT BLOCK --");
      68. System.out.println(sql5);
      69. System.out.println("-- AFTER TEXT BLOCK --");
      70. System.out.println("\nMoving on its own line and shift-left the closing delimiter:\n");
      71. String sql6 = """
      72. UPDATE "public"."office"
      73. SET ("address_first", "address_second", "phone") =
      74. (SELECT "public"."employee"."first_name",
      75. "public"."employee"."last_name", ?
      76. FROM "public"."employee"
      77. WHERE "public"."employee"."job_title" = ?
      78. """;
      79. System.out.println("-- BEFORE TEXT BLOCK --");
      80. System.out.println(sql6);
      81. System.out.println("-- AFTER TEXT BLOCK --");
      82. System.out.println("\nMoving on its own line and shift-right the closing delimiter:\n");
      83. String sql7 = """
      84. UPDATE "public"."office"
      85. SET ("address_first", "address_second", "phone") =
      86. (SELECT "public"."employee"."first_name",
      87. "public"."employee"."last_name", ?
      88. FROM "public"."employee"
      89. WHERE "public"."employee"."job_title" = ?
      90. """;
      91. System.out.println("-- BEFORE TEXT BLOCK --");
      92. System.out.println(sql7);
      93. System.out.println("-- AFTER TEXT BLOCK --");
      94. }
      95. }

    423. 相关阅读:
      外包干了一个月,技术明显进步。。。。。
      SciChart WPF Charts SDK v6.x
      设计模式 14 模板模式
      kubernetes集群搭建
      期货怎么克服频繁止损(期货交易怎么止损)
      Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
      美团2017年CodeM大赛-资格赛 NC13224 送外卖(记忆化搜索 or 预处理 dfs)
      文件包含漏洞详解
      [AI]Python中的Restful
      【解决方法】错误:无法和SFTP服务器建立FTP连接,请选择合适的协议
    424. 原文地址:https://blog.csdn.net/wangjunji34478/article/details/133392469