java - Is it safe to return 0 as hashcode -


i have class has list of article (or want). safe implement/override hashcode() function in class :

class place{     list <article> list = new list<article>();      public int hashcode() {         if(list.isempty())             return 0; //is safe return 0         else             return list.get(0).hashcode();     } }  public class main{     private hashset<place> places = new hashset<>();      //this function must donne     public boolean isarticlein(article article){         place place = new place();         place.add(article);         return places.contains(place);     } } 

is there possibility have list not empty , return 0.

if want store objects of class in container uses hashcode, should make sure "if 2 objects equal should return same hash code" (otherwise container may store duplicates / confused). objects of class compare equal if both have empty list?

best advice on how implement equals , hashcode capture information want while remaining consistent available here (using equalsbuilder , hashcodebuilder apache commons lang library recommended). seem elements in list should contribute hashcode - after all, if second or third element of list different, objects return false 'equals' - right?


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -