Java中的 RuleBasedCollator hashCode() 方法与示例
Java.text.RuleBasedCollator类的hashCode()方法用于获取此 Collator 对象的 hashCode。
句法:
public abstract int hashCode()
参数:此方法不接受任何参数。
返回值:此方法以整数格式返回哈希码值。
以下是说明hashCode()方法的示例:
示例 1:
Java
// Java program to demonstrate
// hashCode() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
// Creating and initializing new simple rule
String simple = "< a < b < c < d";
// Creating and initializing
// new RuleBasedCollator Object
RuleBasedCollator col
= new RuleBasedCollator(simple);
// getting hashCode of this object
// using hashCode() method
int hash = col.hashCode();
// display result
System.out.println("hashCode is :- "
+ hash);
}
catch (ClassCastException e) {
System.out.println("Exception thrown : "
+ e);
}
catch (ParseException e) {
System.out.println("Exception thrown : "
+ e);
}
}
}
Java
// Java program to demonstrate
// hashCode() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
// Creating and initializing new simple rule
String simple = "< a < c & a < b";
// Creating and initializing
// new RuleBasedCollator Object
RuleBasedCollator col
= new RuleBasedCollator(simple);
// getting hashCode of this object
// using hashCode() method
int hash = col.hashCode();
// display result
System.out.println("hashCode is :- "
+ hash);
}
catch (ClassCastException e) {
System.out.println("Exception thrown : "
+ e);
}
catch (ParseException e) {
System.out.println("Exception thrown : "
+ e);
}
}
}
输出:
hashCode is :- 1882448026
示例 2:
Java
// Java program to demonstrate
// hashCode() method
import java.text.*;
import java.util.*;
import java.io.*;
public class GFG {
public static void main(String[] argv)
{
try {
// Creating and initializing new simple rule
String simple = "< a < c & a < b";
// Creating and initializing
// new RuleBasedCollator Object
RuleBasedCollator col
= new RuleBasedCollator(simple);
// getting hashCode of this object
// using hashCode() method
int hash = col.hashCode();
// display result
System.out.println("hashCode is :- "
+ hash);
}
catch (ClassCastException e) {
System.out.println("Exception thrown : "
+ e);
}
catch (ParseException e) {
System.out.println("Exception thrown : "
+ e);
}
}
}
输出:
hashCode is :- 2022902017
参考: https://docs.oracle.com/javase/9/docs/api/ Java/text/RuleBasedCollator.html#hashCode–