概述
------- android培训、java培训、期待与您交流!
Collection
|——List::元素是有序的,元素可以重复,因为该集合体系有索引
|——ArrayList:底层的数据结构使用数组结构。特点:查询速度很快。但是增删稍慢。线程不同步。
|——LinkedList:底层使用的是链表数据结构。特点:增删的速度很快,查询速度稍慢。
|——Vector:底层是数组数据结构。和ArrayList功能相同,线程同步,被ArrayList取代
|——Set:Set是Collection子接口;
Set和Collection基本一样,一点除外:
Set无法记住添加的顺序,不允许包含重复的元素。当试图添加两个相同元素进Set集合,添加操作失败,add()方法返回false
Set判断两个对象是否相等用equals,而不是使用==。也就是说两个对象equals比较返回true,Set集合是不会接受这两个对象的
|——HashSet:底层数据结构是哈希表。线程是非同步的。
HashSet 类是Set接口最常用的实现类,采用hash算法存储数据,具有良好的存储和查找功能。
散列存储:不记录添加顺序;排列顺序时,顺序有可能发生变化;
HashSet 集合元素值允许时null,但是最多只能有一个
hash算法的功能:
保证通过一个对象快速找到另一个对象;其算法价值体现在速度,可以保证查询快速执行;
当从HashSet 中访问元素时,HashSet先计算该元素的hashCode(也就是该对象的hashCode方法返回值),然后直接到该HashCode对应的位置取出该元素;
在这里对象的hashCode就好比是数组里的索引,但是不是索引
HashSet元素添加
当向HashSet集合中存入一个元素是,HashSet会调用该对象的HashCode()方法来得到该对象的hashCode值,判断已经存储在集合中的对象的hashCode值是否与添加的对象的hashCode值一致:若不一致,直接添加进去;若一致,再进行equals方法比较,equals方法如果返回true,表明对象已经添加进去了,就不会添加新的对象了,否则添加进去;如果我们重写了equals方法,也要重写hashCode方法,反之亦然。HashSet集合判断连个元素相等的标准是两个对象通过equals方法比较相等,并且两个对象的hashCode方法返回值也相等。如果需要某个类的对象保存到HashSet集合中,覆写该类的equals()和hashCode()方法,应该尽量保证两个对象通过equals比较返回true时,他们的hashCode返回也相等。
class PersonDemo{
private String name;
public PersonDemo(String name){
super();
this.name = name();
}
@override
public String toString(){
return "name ="+name;
}
//没有覆写hashcode和equals方法前,显示三次(一样的)。覆写后,只剩下一个了!说明覆写后方法起作用了,重复的输入不进去!
@override
public int hashCode(){
final int prime = 31;
int result =1;
result =prime*result+((name==null)?0:name.hashCode());
return result;
}
@Override
public boolean equals(Object obj){
if(this ==obj)
return true;
if(obj==null)
return false;
if(getClass()!=obj.getClass())
return false;
PersonDemo other =(PersonDemo)obj;
if(name==null){
if(other.name!=null)
return false;
}else if(!name.equals(other.name))
return flase;
}
return true;
}
public class Demo12{
public static void main(String[] args)
{
Set s = new HashSet();
s .add(new Person("张三"));
s .add(new Person("张三"));
s .add(new Person("张三"));
System.out.println(s);
}
}
|——TreeSet:可以对set集合中的元素进行排序。底层数据结构是二叉树。
保证元素唯一性的一句:compareTo方法return 0;
TreeSet是SortedSet接口唯一的实现,与HashSet相比额外的方法有:
Comparator comparator():返回当前Set使用的Comparator,若返回null,表示以自然顺序排序。
Object first():返回此set中当前第一个(最低)元素。
Object last():返回此set中当前最后一个(最高)元素。
SortedSet subSet(Object fromElement,E toElement)返回此set的部子集,其元素从fromElement(包括)到toElement(不包括)。
SortedSet headSet(Object toElement)返回此set的部分子集,其元素严格小于toElement.
SortedSet tailSet(Object fromElement)返回此set的部分子集,其元素大于等于fromElement.
TreeSet排序的第一种方式:让元素自身具备比较性
元素需要实现Compareble接口,覆盖compareTo方法
这种方式也成为元素的自然顺序,或者叫做默认顺序。
TreeSet集合的第二种排序方式。
当元素自身不具备比较性时,或其比较性不是所需要的
这时需要让集合自身具备比较性。
在集合初始化时,就有了比较方式。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
class Cell implements Comparable<Object>{
private Integer row;
private Integer col;
public Cell(Integer c1, Integer col) {
super();
this.row = c1;
this.col = col;
}
public Integer getRow() {
return row;
}
public void setRow(Integer row) {
this.row = row;
}
public Integer getCol() {
return col;
}
public void setCol(Integer col) {
this.col = col;
}
@Override
public int compareTo(Object o) {
if(o instanceof Cell){
Cell c = (Cell) o;
return this.col.compareTo(c.col);
}
return 0;
}
}
public class CompareTest {
public static void main(String[] args) {
List<Cell> list= new ArrayList<Cell>();
list.add(new Cell(2,3));
list.add(new Cell(5,1));
list.add(new Cell(3,2));
Collections.sort(list);
Iterator it = list.iterator();
while(it.hasNext())
{
Cell c = (Cell) it.next();
System.out.println(c.getRow()+":"+c.getCol());
}
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CompareTest_2 {
public static void main(String[] args) {
List<Cell>cells = new ArrayList<Cell>();
cells.add(new Cell(2,3));
cells.add(new Cell(5,1));
cells.add(new Cell(3,2));
Collections.sort(cells,new Comparator<Cell>(){
@Override
public int compare(Cell o1, Cell o2) {
return o1.col-o2.col;
}
});
System.out.println(cells);
}
}
class Cell{
public int row;
public int col;
public Cell(int row,int col){
super();
this.row =row;
this.col =col;
}
public String toString(){
return row+","+col;
}
}
--------------------------------android培训、java培训、、期待与您交流! ----------------------详细请查看:http://edu.csdn.net/heima
最后
以上就是善良过客为你收集整理的黑马程序员:java集合框架(二)的全部内容,希望文章能够帮你解决黑马程序员:java集合框架(二)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复