RuleBasedCollator hashCode() method in Java with ExampleLast Updated : 29 Mar 2025 The java.text.RuleBasedCollator class has a hashCode() function. The hashCode for this Collator object can be obtained using the RuleBasedCollator class. Syntax: Parameter: No parameters are accepted by this method. Return Value: The hash code value is returned by this method in integer format. Example 1:A custom string comparison based on a specified rule is demonstrated using the Java code that is provided, which uses RuleBasedCollator. In other words, 'a' should come before 'b', 'b' before 'c', and so on. The rule defines this as "< a < b \ c \ d". With this rule, an object called RuleBasedCollator is created, and its hash code is obtained by calling the hashCode() method. To catch and display ClassCastException or ParseException in the event that the rule or typecasting is wrong, exception handling is implemented. Implementation:FileName: RuleBasedCollatorExample1.java Output: The hashCode is given by :- 1882448026 Example 2:The Java code that is provided defines a custom string sorting rule using RuleBasedCollator. 'A' should appear before 'c' and 'b,' and 'c' and 'b' should be compared according to their natural sequence, according to the rule "< a < c & a < b." With this custom rule, a RuleBasedCollator object is produced. The object's hash code is then retrieved by the program using the hashCode() method, and it is printed. The code handles runtime exceptions like ClassCastException and ParseException, which are used to handle possible flaws in the rule format or type problems. Implementation:FileName: RuleBasedCollatorExample2.java Output: The hashCode is given by :- 2022902017 Next TopicPOJO |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India