您的位置:首页 > 其它

HashSet和TreeSet中最好只放入不可变对象

2015-11-05 15:25 225 查看
1234567891011121314151617181920212223242526272829303132333435363738394041
class
 
UN 
implements
 
Comparable
{
    
private
 
final
 
int
 
count;
    
public
 
UN()
    
{
        
this
.count=
0
;
    
}
    
public
 
UN(
int
 
count)
    
{
        
this
.count=count;
    
}
    
public
 
int
 
getCount()
    
{
        
return
 
this
.count;
    
}
    
public
 
String toString()
    
{
        
return
 
"R[count:"
+count+
"]"
;
    
}
    
public
 
boolean
 
equals(Object obj)
    
{
        
if
(
this
==obj)
        
{
            
return
 
true
;
        
}
        
if
(obj!=
null
&&obj.getClass()==UN.
class
)
        
{
            
UN r=(UN)obj;
            
if
(r.count==
this
.count)
            
{
                
return
 
true
;
            
}
        
}
        
return
 
false
;
    
}
    
public
 
int
 
compareTo(Object obj)
    
{
        
UN r=(UN)obj;
        
return
 
count>r.count? 
1
:count<r.count? -
1
:
0
;
    
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: