/******************************************************************************* * * Copyright (c) Weaver Info Tech Co. LTD * * Sessioninfo * * App.backend.model.SessionInfo.java * todo:file description or class description. * * @author: Administrator *@since: 2014-5-26 *@version: 1.0.0 * * @changeLogs: * 1.0.0:first created this class. * ******************************************************************************/ Importjava.io.Serializable; /** * @authorAdministrator **/@SuppressWarnings ("Serial") Public classSessioninfoImplementsserializable{Private intID; PrivateString URL; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetUrl () {returnURL; } @Override PublicString toString () {return"sessioninfo{" + "id=" + ID + ", url= '" + url + ' \ ' + '} '; } Public voidseturl (String url) { This. url =URL; } /*(non-javadoc) * @see Java.lang.object#hashcode ()*/@Override Public inthashcode () {returnID; } /*(non-javadoc) * @see java.lang.object#equals (java.lang.Object)*/@Override Public Booleanequals (Object o) {if(O = =NULL) { return false; } Else { if(O.getclass ()! = This. GetClass ()) { return false; } Else { FinalSessioninfo s =(Sessioninfo) o; returnS.id = = This. ID; } } } }
ImportJava.util.*; Public classHashcodetest { Public Static voidMain (string[] args) {sessioninfo sessionInfo1=NewSessioninfo (); Sessioninfo SessionInfo2=NewSessioninfo (); Sessioninfo1.setid (1); Sessioninfo2.setid (1); Sessioninfo1.seturl ("Test"); Sessioninfo2.seturl ("Test"); Sessioninfo Msessioninfo=NULL;//entity class objectSet<sessioninfo> sessionlist =NewHashset<sessioninfo> ();//declaring a set collection object Finallist<sessioninfo> list =NewArraylist<sessioninfo> ();//declaring a list collection objectSessionlist.add (SESSIONINFO1);//to add an object to the Set collectionSessionlist.add (SessionInfo2);//to add an object to the Set collectionList.addall (sessionlist);//to add data to the List collection Object for(inti = 0; I < list.size (); i++) {System.out.println (List.get (i)); } }}
Object de-weight principle in Java list