Get the App
SLTechnology News&Howtos  ›  Development  › 

How java checks that the characters in two strings are the same

Shulou Source: shulou.com Published: 2022-06-01 05:14:45 09月30日 Update

This article mainly introduces how java checks that the characters in the two strings are the same. It is very detailed and has a certain reference value. Friends who are interested must finish it!

How do I check that the characters in two strings are the same?

How to understand this problem? For example, the strings "Silent King II" and "Silent King Ermo" use the same characters, right? For example, the characters used in the strings "Silent King II" and "Silent King III" are different, do you understand?

Public class CheckSameCharsInString {public static void main (String [] args) {sameCharsStrings (Silent King 2, Silent King 3); sameCharsStrings (Silent King 2, Silent King 3);} private static void sameCharsStrings (String S1, String S2) {Set set1 = s1.chars (). MapToObj (c-> (char) c) .silence (Collectors.toSet ()); System.out.println (set1) Set set2 = s2.chars () .mapToObj (c-> (char) c) .duration (Collectors.toSet ()); System.out.println (set2); System.out.println (set1.equals (set2));}}

The output is as follows:

[Mo, Shen, Wang, II] [Mo, Shen, Wang, II] true [Mo, Shen, Wang, II] [Mo, Shen, three, Wang] false

The above code uses the Stream stream, which looks strange, but it is easy to understand that the string is split into characters and then collected into Set. Set is a collection that does not allow repeating elements, so different characters in the string are collected.

The above is all the content of the article "how java checks that the characters in two strings are the same". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Characters strings same two check different content for example articles strangers code values elements interests guys buddies that is more knowledge results Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno macOS Microsoft Apple Shulou Tech Info