英文:
Simplifying/optimizing massive if...else if...else statement(s)
问题
Sure, here's the translated part of your text:
好的,基本上,我有一些代码,使用contains()
方法来检测两个字符串中特定字符的存在。为了提供额外的上下文,这个问题是我遇到的问题的一个良好资源(第三个解决方案也是我为此问题考虑过的)。无论如何,这是我的一些代码:
// 上面的代码基本上只是将不同的字符连接到字符串stringX和stringY中
if (stringX.contains("!#")) {
} else if (stringX.contains("$%&")) {
} else if (stringX.contains("'()")) {
} else if (stringX.contains("!'&")) {
} else if (stringX.contains("\"%(")) {
// 还有70多个类似的else-if语句
if (stringY.contains("!#")) {
} else if (stringY.contains("$%&")) {
} else if (stringY.contains("'()")) {
} else if (stringY.contains("!'&")) {
} else if (stringY.contains("\"%(")) {
// 还有70多个类似的else-if语句,所有这些语句都与处理stringX的语句完全相同
我对Java编程还不太熟悉,所以不太确定应该如何处理这个问题。也许这不是问题?另外,如果可以不使用正则表达式来解决,那将更好;我目前对正则表达式了解不多。但如果唯一合理的解决方案是使用正则表达式,我当然会这样做。编辑:所有这些else-if语句中的代码都不会有太大的不同;基本上只是一个System.out.println()
,输出有关stringX/stringY包含哪些字符的一些信息。
英文:
Okay so essentially, I have some code that uses the contains() method to detect the presence of specific characters in two strings. For extra context, this question is a good resource as to what kind of problem I'm having (and the third solution is also something I've looked into for this). Regardless, here is some of my code:
// code up here basically just concatenates different
// characters to Strings: stringX and stringY
if (stringX.contains("!\"#")) {
} else if (stringX.contains("$%&")) {
} else if (stringX.contains("\'()")) {
} else if (stringX.contains("!$\'")) {
} else if (stringX.contains("\"%(")) {
// literally 70+ more else-if statements
}
if (stringY.contains("!\"#")) {
} else if (stringY.contains("$%&")) {
} else if (stringY.contains("\'()")) {
} else if (stringY.contains("!$\'")) {
} else if (stringY.contains("\"%(")) {
// literally 70+ more else-if statements, all of which are
// exactly the same as those working with stringX
}
I'm still pretty new to Java programming, so I'm not sure how I should go about this. Maybe it is a non-issue? Also, if I can remedy this without using RegEx, that would be preferable; I am not very knowledgeable in it at this point it time. But if the only rational solution would be to utilize it, I will obviously do so.
Edit: The code within all of these else-if statements will not be very different from each other at all; basically just a System.out.println()
with some information about what characters stringX/stringY contains.
答案1
得分: 3
可以简单地制作一个案例列表,然后使用java 8 stream filter:
List<String> patterns = Arrays.asList("!", "$%&", ...);
Optional<String> matched = patterns.stream().filter(p -> stringX.contains(p));
if (matched.isPresent()) {
System.console().printf(matched.get())
}
java stream
可能会使性能变慢,但不会太多。
英文:
can simply by make a list of your case. then using java 8 stream filter
List<String> pattems = Arrays.asList("!\"#", "$%&", ...);
Optional<String> matched = pattems.stream().filter(p -> stringX.contains(p));
if(matched.isPresent()) {
System.console().printf(matched.get())
}
java stream
could make your peformance slower but not too much
答案2
得分: 3
写相同的代码超过一次应该立即让你意识到将该代码移到函数中以便重用。
至于简化表达式,最好的方法可能是将要查找的模式存储为数组,并使用条件迭代数组。
private static final String[] patterns = new String[] { "!\u0023", "$%\u0026", "\u0027()", "!$\u0027\u0027", "\u0025(\u0022", ... };
private static void findPatterns(String input) {
for (String pattern : patterns) {
if (input.contains(pattern)) {
System.out.println("Found pattern: " + pattern);
}
}
}
// 在其他地方...
findPatterns(stringX);
findPatterns(stringY);
这种模式在函数式和函数式风格的语言中特别常见。Java 8 流是一个很好的例子,因此你可以等效地执行以下操作:
List<String> patterns = Arrays.asList("!\u0023", "$%\u0026", "\u0027()", "!$\u0027\u0027", "\u0025(\u0022", ...);
patterns.stream()
.filter(pattern -> stringX.contains(pattern))
.forEach(pattern -> System.out.println("Found pattern: " + pattern));
希望这对你有帮助!
英文:
Writing the same code more than once should immediately set off alarm bells in your head to move that code into a function so it can be reused.
As for simplifying the expression, the best approach is probably storing the patterns you're looking for as an array and iterating over the array with your condition.
private static final String[] patterns = new String[] {"!\"#", "$%&", "\'()", "!$\'", "\"%(", ...};
private static void findPatterns(String input) {
for (String pattern : patterns) {
if (input.contains(pattern) {
System.out.println("Found pattern: " + pattern);
}
}
}
// Elsewhere...
findPatterns(stringX);
findPatterns(stringY);
This pattern is especially common in functional and functional-style languages. Java 8 streams are a good example, so you could equivalently do
List<String> patterns = Arrays.asList("!\"#", "$%&", "\'()", "!$\'", "\"%(", ...);
patterns.stream()
.filter(pattern -> stringX.contains(pattern))
.forEach(pattern -> System.out.println("Found pattern: " + pattern));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论