Package demo; import Java. util. arraylist; import Java. util. list; class person {private string username; private int age; Public String GetUserName () {return username;} public void setusername (string username) {This. username = username;} public int getage () {return age;} public void setage (INT age) {This. age = age;} public person (string username, int age) {super (); this. username = username; this. age = age ;}@ overridepublic string tostring () {return "person [username =" + username + ", age =" + age + "]" ;}} public class test {public static void printlist (list) {for (INT I = 0; I <list. size (); I ++) {system. out. println (list. get (I);} system. out. println ("--------------------------------");} public static void main (string [] ARGs) {int partition = 3; person p1 = new person ("A", 12 ); person P2 = new person ("B", 13); person P3 = new person ("C", 14); person P4 = new person ("D", 15 ); person P5 = new person ("e", 16); person P [] = new person [] {P1, P2, P3, P4, P5 }; list list0 = new arraylist (); List list1 = new arraylist (); List list2 = new arraylist (); For (INT I = 0; I <p. length; I ++) {person Pi = P [I]; int hash = pi. hashcode (); system. out. println (hash); If (hash % partition = 0) {list0.add (PI);} If (hash % partition = 1) {list1.add (PI );} if (hash % partition = 2) {list2.add (PI) ;}} printlist (list0); printlist (list1); printlist (list2 );}}
Running result
638559109128716090417105372976367682901253637029 person [username = C, age = 14] ---------------------------------- person [username = A, age = 12] registrant person [username = B, age = 13] person [username = D, age = 15] person [username = E, age = 16] ----------------------------------