目录
一、
public class H01 {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add(new News("新闻一上课健身房表达方式但是复活甲噶空间大时间啊打不开部分洗发水借古讽今"));
arrayList.add(new News("新闻二水晶湖郡"));
int size = arrayList.size();
for (int i = size-1; i >=0 ; i--) {
News news = (News) arrayList.get(i);
System.out.println(processTitle(news.getTitle()));
}
}
public static String processTitle(String title){
if(title==null){
return "";
}
if(title.length()>15){
return title.substring(0,15)+"...";
}else {
return title;
}
}
}
class News{
private String title;
private String content;
public News(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "News{" +
"title='" + title + '\'' +
'}';
}
}
二、
三、
四、
试分析HashSet和TreeSet分别如何实现去重的
(1)HashSet的去重机制: hashCode() + equals() ,底层先通过存入对象,进行运算得到一个hash值,通过hash值得到对应的索引,如果发现table索引所在的位置,没有数据,就直接存放如果有数据,就进行equals比较[遍历比较],如果比较后,不相同,就加入,否则就不加入.
(2)TreeSet的去重机制:如果你传入了一个Comparator匿名对象,就使用实现的compare去重,如果方法返回0,就认为是相同的元素/数据,就不添加,如果你没有传入一个Comparator匿名对象,则以你添加的对象实现的Compareable接口的compareTo去重.
五、
六、
这个要remove的cc的哈希值是根据1001 cc来计算的,但链表里的1001 cc之前是用aa计算出来的,所以删除失败,还是有两个对象