黑马程序员_集合

Collection
    |--List:元素是有序的,元素可以重复。因为该集合体系有索引。
        |--ArrayList:底层的数据结构使用的是数组结构。特点:查询速度很快。但是增删稍慢。线程不同步。
        |--LinkedList:底层使用的链表数据结构。特点:增删速度很快,查询稍慢。线程不同步。
        |--Vector:底层是数组数据结构。线程同步。被ArrayList替代了。因为效率低。
List集合是用来存储对象的,其也只可存储对象,若不为对象。如:取出对象是时可用

    |--Set:元素是无序,元素不可以重复。、
集合是用来存储对象的,其也只可存储对象,若不为对象。如:取出对象是时可用

List:
    特有方法。凡是可以操作角标的方法都是该体系特有的方法。


    add(index,element);
    addAll(index,Collection);


    remove(index);


    set(index,element);

    get(index):
    subList(from,to);
    listIterator();
    int indexOf(obj):获取指定元素的位置。
    ListIterator listIterator();


List集合特有的迭代器。ListIterator是Iterator的子接口。

在迭代时,不可以通过集合对象的方法操作集合中的元素。
因为会发生ConcurrentModificationException异常。

所以,在迭代器时,只能用迭代器的放过操作元素,可是Iterator方法是有限的,
只能对元素进行判断,取出,删除的操作,
如果想要其他的操作如添加,修改等,就需要使用其子接口,ListIterator。

该接口只能通过List集合的listIterator方法获取。

如:

public static void main(String[] args) 
	{

		//演示列表迭代器。
		ArrayList al = new ArrayList();

		//添加元素
		al.add("java01");
		al.add("java02");
		al.add("java03");

		sop(al);

		
		ListIterator li = al.Iterator();

		while(li.hasNext())
		{
			Object obj = li.next();

			if(obj.equals("java02"))
				li.remove();
                        System.out.println(obj);
		}

public static void main(String[] args) 
	{

		//演示列表迭代器。
		ArrayList al = new ArrayList();

		//添加元素
		al.add("java01");
		al.add("java02");
		al.add("java03");

		ListIterator li = al.listIterator();

		
		//sop("hasPrevious():"+li.hasPrevious());

		while(li.hasNext())
		{
			Object obj = li.next();

			if(obj.equals("java02"))
				//li.add("java009");
				li.set("java006");


		}
list去除重复元素
public static ArrayList singleElement()
	{
                List al = new ArrayList();
                al.add("java1");
                al.add("java1");
                al.add("java2");
                al.add("java3");
		//定义一个临时容器。
		ArrayList newAl = new ArrayList();

		Iterator it = al.iterator();

		while(it.hasNext())
		{
			Object obj = it.next();

			if(!newAl.contains(obj))
				newAl.add(obj);

		}

		return newAl;
	}</span>


 
Set:无序,不可以重复元素。    |--HashSet:数据结构是哈希表。线程是非同步的。                保证元素唯一性的原理:判断元素的hashCode值是否相同。                如果相同,还会继续判断元素的equals方法,是否为true。    |--TreeSet:可以对Set集合中的元素进行排序。                底层数据结构是二叉树。                保证元素唯一性的依据:                compareTo方法return 0.                TreeSet排序的第一种方式:让元素自身具备比较性。                元素需要实现Comparable接口,覆盖compareTo方法。                也种方式也成为元素的自然顺序,或者叫做默认顺序。                TreeSet的第二种排序方式。                当元素自身不具备比较性时,或者具备的比较性不是所需要的。                这时就需要让集合自身具备比较性。 

                在集合初始化时,就有了比较方式。

treeset元素第一种让元素自身具备比较性实现Comparable接口

compareTo(T t)
返回 负整数、零或正整数,根据此对象是小于、等于还是大于指定对象。

tree第一种排列方式

class Student implements Comparable//该接口强制让学生具备比较性。
{
	private String name;
	private int age;

	Student(String name,int age)
	{
		this.name = name;
		this.age = age;
	}

	public int compareTo(Object obj)
	{

		//return 0;
		
		if(!(obj instanceof Student))
			throw new RuntimeException("不是学生对象");
		Student s = (Student)obj;

		System.out.println(this.name+"....compareto....."+s.name);
		if(this.age>s.age)
			return 1;
		if(this.age==s.age)
		{
			return this.name.compareTo(s.name);
		}
		return -1;
		/**/
	}

	public String getName()
	{
		return name;

	}
	public int getAge()
	{
		return age;
	}
}class TreeSetDemo 
{
	public static void main(String[] args) 
	{
		TreeSet ts = new TreeSet();

		ts.add(new Student("lisi02",22));
		ts.add(new Student("lisi007",20));
		ts.add(new Student("lisi09",19));
		ts.add(new Student("lisi08",19));
		//ts.add(new Student("lisi007",20));
		//ts.add(new Student("lisi01",40));

		Iterator it = ts.iterator();
		while(it.hasNext())
		{
			Student stu = (Student)it.next();
			System.out.println(stu.getName()+"..."+stu.getAge());
		}
tree第二种排列方式
public static void main(String[] args) 
	{
		TreeSet ts = new TreeSet(new StrLenComparator());

		ts.add("abcd");
		ts.add("cc");
		ts.add("cba");
		ts.add("aaa");
		ts.add("z");
		ts.add("cc");

		Iterator it = ts.iterator();

		while(it.hasNext())
		{
			System.out.println(it.next());
		}
	}
}

class StrLenComparator implements Comparator
{
	public int compare(Object o1,Object o2)
	{
		String s1 = (String)o1;
		String s2 = (String)o2;
		int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));
		if(num==0)
			return s1.compareTo(s2);
		return num;
	}



	}
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值