hashSet的实现原理:
往Haset添加元素的时候,HashSet会先调用元素的hashCode方法得到元素的哈希值 , 然后通过元素 的哈希值经过移位等运算,就可以算出该元素在哈希表中 的存储位置。
情况1: 如果算出元素存储的位置目前没有任何元素存储,那么该元素可以直接存储到该位置上。
情况2: 如果算出该元素的存储位置目前已经存在有其他的元素了,那么会调用该元素的equals方法与该位置的元素再比较一次 ,如果equals返回的是true,那么该元素与这个位置上的元素就视为重复元素,不允许添加,如果equals方法返回的是false,那么该元素运行 添加。
@GetMapping("/about/{id}")
public String aboutId(@PathVariable String id, @PageableDefault(size = 6, sort = "updateTime", direction = Sort.Direction.DESC) Pageable pageable, Model model) {
System.out.println(id);
Long userId = Long.parseLong(id);
System.out.println(userId);
model.addAttribute("page", blogService.listBlogByUserId(userId, pageable));
List<Blog> blogs= blogService.listBlogByUserId(userId, pageable).getContent();
/**
* 循环遍历出个人的types,然后通过hashset进行去重
*/
List<String > typesName = new ArrayList<>();
for (Blog blog:blogs){
System.out.println("---------" + blog);
typesName.add(blog.getType().getName());
}
HashSet h = new HashSet(typesName);
typesName.clear();
typesName.addAll(h);
System.out.println(typesName);
model.addAttribute("typesName", typesName);
/**
* 要从中找出每个blog中的不相同的tags进行返回数据
*/
List<Tag> tags = new ArrayList<>();
return "/about";
}