Skip to content

Commit f24332c

Browse files
New Examples
1 parent 81c8d2f commit f24332c

File tree

4 files changed

+210
-0
lines changed

4 files changed

+210
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.howtodoinjava.algorithms;
2+
3+
public class IsomorphicStrings {
4+
5+
public boolean areIsomorphic(String s1, String s2) {
6+
if (s1 == null || s2 == null
7+
|| s1.length() != s2.length()) {
8+
return false;
9+
}
10+
11+
int[] arr1 = new int[256];
12+
int[] arr2 = new int[256];
13+
14+
for (int i = 0; i < s1.length(); i++) {
15+
16+
char c1 = s1.charAt(i);
17+
char c2 = s2.charAt(i);
18+
19+
if (arr1[c1] != arr2[c2]) {
20+
return false;
21+
}
22+
23+
arr1[c1] = i + 1;
24+
arr2[c2] = i + 1;
25+
}
26+
return true;
27+
}
28+
29+
public static void main(String[] args) {
30+
IsomorphicStrings iso = new IsomorphicStrings();
31+
System.out.println(iso.areIsomorphic("abbcdd", "qwwcrr")); // true
32+
System.out.println(iso.areIsomorphic("aab", "que")); // false
33+
}
34+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.howtodoinjava.core.string;
2+
3+
import java.util.StringJoiner;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.Stream;
6+
7+
@SuppressWarnings("StringTemplateMigration")
8+
public class MultilineStringExample {
9+
10+
public static void main(String[] args) {
11+
12+
//Text Blocks
13+
String content = """
14+
Line 1
15+
'Line 2'
16+
"Line 3"
17+
Line 4
18+
""";
19+
20+
System.out.println(content);
21+
22+
String json = """
23+
{
24+
"name": "John Doe",
25+
"age": 30,
26+
"city": "New York"
27+
}
28+
""";
29+
System.out.println(json);
30+
31+
// String Concatenation
32+
33+
String NEW_LINE = System.getProperty("line.separator");
34+
35+
content = "Line 1" +
36+
NEW_LINE +
37+
"'Line 2'" +
38+
NEW_LINE +
39+
"\"Line 3\"" +
40+
NEW_LINE +
41+
"Line 4 ";
42+
43+
System.out.println(content);
44+
45+
StringBuffer sb = new StringBuffer();
46+
47+
content = sb.append("Line 1")
48+
.append(NEW_LINE)
49+
.append("'Line 2'")
50+
.append(NEW_LINE)
51+
.append("\"Line 3\"")
52+
.append(NEW_LINE)
53+
.append("Line 4 ").toString();
54+
55+
System.out.println(content);
56+
57+
//String Join
58+
content = String.join(NEW_LINE,
59+
"Line 1",
60+
"'Line 2'",
61+
"\"Line 3\"",
62+
"Line 4 ");
63+
64+
System.out.println(content);
65+
66+
//StringJoiner
67+
68+
StringJoiner stringJoiner = new StringJoiner(NEW_LINE);
69+
70+
content = stringJoiner.add("Line 1")
71+
.add("'Line 2'")
72+
.add("\"Line 3\"")
73+
.add("Line 4 ").toString();
74+
75+
System.out.println(content);
76+
77+
//Stream API
78+
79+
content = Stream.of("Line 1", "'Line 2'", "\"Line 3\"", "Line 4 ")
80+
.collect(Collectors.joining(NEW_LINE));
81+
82+
System.out.println(content);
83+
}
84+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.howtodoinjava.core.string;
2+
3+
public class StringIndentExample {
4+
5+
public static void main(String[] args) {
6+
String line1 = "ABC".indent(8);
7+
System.out.println(line1.replace(" ", "-"));
8+
9+
String line2 = "ABC".indent(8).indent(-5);
10+
System.out.println(line2.replace(" ", "-"));
11+
12+
String textBlock = """
13+
Line 1
14+
Line 2""";
15+
16+
String indentedBlock = """
17+
Line 1
18+
Line 2""".indent(8);
19+
20+
System.out.println(textBlock.replace(" ", "-"));
21+
System.out.println(indentedBlock.replace(" ", "-"));
22+
}
23+
24+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.howtodoinjava.core.string;
2+
3+
import java.text.MessageFormat;
4+
5+
public class TextBlockFormatting {
6+
7+
public static void main(String[] args) {
8+
9+
//Expected Output
10+
/*{
11+
"id": 1001,
12+
"name": "John Doe",
13+
"age": 34
14+
}*/
15+
16+
int id = 1001;
17+
int age = 34;
18+
String name = "John Doe";
19+
20+
Object[] formatArguments = {
21+
1001, // id
22+
"John Doe", // name
23+
34 // age
24+
};
25+
26+
27+
/*String greeting = """
28+
Hello, """ + name + """
29+
Welcome to Java text blocks!
30+
""";
31+
32+
System.out.println(greeting.toString()); //Prints "Hello,John DoeWelcome to Java text blocks!"*/
33+
34+
35+
36+
String textBlock = """
37+
{
38+
"id": %d,
39+
"name": "%s",
40+
"age": %d
41+
}
42+
""";
43+
44+
String formattedTextBlock1 = String.format(textBlock, formatArguments);
45+
System.out.println(formattedTextBlock1);
46+
47+
String formattedTextBlock2 = String.format(textBlock, id, name, age);
48+
System.out.println(formattedTextBlock2);
49+
50+
51+
String json = """
52+
'{'
53+
"id": {0, number, #},
54+
"name": "{1}",
55+
"salary": "{2, number, integer}"
56+
'}'
57+
""";
58+
59+
Object[] arguments = {
60+
1001, // id
61+
"John Doe", // name
62+
34000 // salary
63+
};
64+
65+
String formattedTextBlock3 = MessageFormat.format(json, arguments);
66+
System.out.println(formattedTextBlock3);
67+
}
68+
}

0 commit comments

Comments
 (0)