Hash small demo hashcode modulo

Source: Internet
Author: User

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] ----------------------------------


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.