黑马程序员——集合(上)(Collection及子类)

------- android培训java培训、期待与您交流! ----------

集合的用途:集合用来可以用来存储对象,可以简单的理解成数组

集合和数组的区别:

集合:1、用来存储对象 2、长度可变3、可以存储任意数据类型

数组:1、用来存储基本数据类型 2、数组一旦创建,不可改变


为什么会出现这么多种容器呢??因为每一种数据对数据存储的方式不同,这种方式称之为:数据结构。


由图知道:Collection下面有2个子类,一个是 List ,一个是 set。他们有什么区别?

List:元素是有序的,允许元素重复,因为他们都自动带有索引。

Set:元素是无序的,不允许元素重复。没有索引。

Collection方法:(集合中存储的都是对象的引用,地址值)

1、添加元素:

①add(Object)  :add参数类型 是Object

②addAll(Collection子类):将集合添加到你另一个集合中

2、获取长度: int  size() 

3、删除元素:

①remove(Object):删除集合中指定元素

②clear():清空集合

4、判断元素:

①contains(Object):判断某个元素是否存在

②containsAll(Collection):判断某一个集合中的所有元素是否存在

③isEmpty():判断集合是否为空(其实就是判断他的size() 是否等于0)

5、取交集  和   不交集

①retainAll(Collection):取2个集合中相同元素——取交集

②removeAll(Collection):取2个集合中不相同元素——(移Collection中的元素)

6、获取集合中的元素,List ,Set 通用方法:iterator();



List:元素是有序的,元素可以重复,自动带索引。List下的常用子类:List集合3种遍历方式(1、iterator 2、ListIterator 3、通过循环获取size。然后通过角标进行get查询)

  |--ArrayList :数组结构,特点:查询、修改速度快;增删稍慢,线程不同步

  |--LinkedList:链表结构,特点:增删速度快;查询、删除稍慢

  |--Vector:数组结构,特点:查询、修改速度快,增删稍慢,程序同步

ArrayList独有方法:

增:①add(index,element):在指定位置插入元素   ②add(index,Collection):在指定位置插入集合

改:①set(index,element):修改指定位置元素

查:①get(index):根据索引返回元素  ②subList(start,end):返回指定位置的集合,包含头不包含尾  ③listIterator():List集合特有,列表迭代器

删:①remove(index):根据索引删除元素。


Collection的迭代器只能删除元素,而listIterator则可以在迭代过程中增删改查。

去除ArrayList集合中重复元素:

public class Collection002 
{
	public static void main(String[] args) 
	{
		ArrayList<String> aList = new ArrayList<>();
		aList.add("java001");
		aList.add("java002");
		aList.add("java003");
		aList.add("java001");
		aList.add("java003");
		System.out.println("原集合:" + aList);
		
		ArrayList<String> newList = removeAgain(aList);
		
		System.out.println("先集合:" + newList);
	}

	public static ArrayList<String> removeAgain(ArrayList<String> aList) 
	{
		ArrayList<String> aaList = new ArrayList<>();	//定义临时容器
		Iterator<String> it = aList.iterator();
		while(it.hasNext())
		{
			String str = it.next();		
			if(aaList.contains(str))	//将拿到的元素,拿到临时容器中查询,有就继续循环,没有就添加。
				continue;
			aaList.add(str);
		}
		return aaList;	
	}
}

import java.util.ArrayList;
import java.util.Iterator;

/*将自定义对象元素存到ArrayList集合中,并去除重复元素
 * 
 * */
public class Collection003 
{
	public static void main(String[] args) 
	{
		ArrayList<Proson> aList = new ArrayList<>();
		aList.add(new Proson("小张",45));
		aList.add(new Proson("小张",44));
		aList.add(new Proson("小张",45));
		aList.add(new Proson("小花",47));
		aList.add(new Proson("小张",45));
		aList.add(new Proson("小花",47));
		
		System.out.println("原集合" + aList);//直接打印底层调用了 该类的 toString();
		
		ArrayList<Proson> aaList = removeAgain(aList);
		
		System.out.println("现集合" + aaList);
		
		
	}
	
	public static ArrayList<Proson> removeAgain(ArrayList<Proson> aList)
	{
		ArrayList<Proson> aaList = new ArrayList<>();
		Iterator<Proson> it = aList.iterator();
		while(it.hasNext())
		{
			Proson p = it.next();
			if(aaList.contains(p))		//而contains呢,调用了 equals方法。
				continue;
			aaList.add(p);
		}
		return aaList;
	}
}

class Proson
{
	private String name;
	private int age;
	Proson(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	
	public boolean equals(Object obj)
	{
		if(!(obj instanceof Proson))	//类型不相同,肯定不是同一个事物
			return false;
		Proson p = (Proson)obj;
		return this.name.equals(p.name) && this.age == p.age;	//名字和年龄都相同的话,那肯定是同一个人
	}
	
	public String toString()
	{
		return name + "::" + age;
	}
}


set:元素是无序的,元素不能重复,没有自带索引

  |--hashSet:哈希表结构,保证唯一性的关键(hashCode  和 equals),首先对比hashCode,hashCode一样,才比较equals。

|--TreeSet:二叉树 结构,保证唯一性的关键( Comparator 接口中的 Compare()   和  Compareble 接口中的 CompareTo()),用来排序

将自定义对象存入到hashSet中,并确保唯一性:

import java.util.HashSet;

/*将自定义对象存入到HashSet中,并确保唯一性
 * */
public class Collection004 
{

	public static void main(String[] args) 
	{
		HashSet<Student1> hhs = new HashSet<>();
		hhs.add(new Student1("小马",20));
		hhs.add(new Student1("小黑",20));
		hhs.add(new Student1("小剑",20));
		hhs.add(new Student1("小马",20));
		
		System.out.println(hhs);
	}

}

class Student1
{
	private String name;
	private int age;
	Student1(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	
	public int hashCode()
	{
		return name.hashCode() + age*3 ; //覆盖hashCode的时候,最好根据他的成员属性来返回值
	}
	
	public boolean equals(Object obj)
	{
		if(!(obj instanceof Student1))	//类型不相同,肯定不是同一个事物
			return false;
		Student1 p = (Student1)obj;
		return this.name.equals(p.name) && this.age == p.age;	//名字和年龄都相同的话,那肯定是同一个人
	}
	
	public String toString()
	{
		return name + "::" + age;
	}
}



根据学生年龄排序,我个人比较使用 Comparator,所以我就演示 Comparator:

import java.util.Comparator;
import java.util.TreeSet;

/*由于Set都是无序的,但是TreeSet提供了排序功能,但是需要实现接口
 * 第一种:类自己实现 Compareble接口
 * 第二种:我在创建TreeSet集合的时候,实现 Comparator 比较器接口
 * */
public class Collection004 
{
	public static void main(String[] args) 
	{
		TreeSet<Student1> hhs = new TreeSet<>(new Comparator<Student1>(){//内部类:通过构造函数实现接口,并覆盖方法
			public int compare(Student1 s1,Student1 s2)
			{
				int age = s1.getAge() - s2.getAge();	//我是通过升序,如果要降序 就换成 s2 - s1
				if(age==0)		//如果年龄相同,我们还得比较姓名
					return s1.getName().compareTo(s2.getName());
				return age;
			}	
		});
		hhs.add(new Student1("小马",30));
		hhs.add(new Student1("小黑",20));
		hhs.add(new Student1("小剑",10));
		hhs.add(new Student1("小马",20));
		hhs.add(new Student1("小名",19));
		hhs.add(new Student1("小名",19));	
		
		System.out.println(hhs);
	}

}

class Student1
{
	private String name;
	private int age;
	Student1(String name,int age)
	{
		this.name = name;
		this.age = age;
	}
	
/*	public int hashCode()
	{
		return name.hashCode() + age*3 ; //覆盖hashCode的时候,最好根据他的成员属性来返回值
	}
	
	public boolean equals(Object obj)
	{
		if(!(obj instanceof Student1))	//类型不相同,肯定不是同一个事物
			return false;
		Student1 p = (Student1)obj;
		return this.name.equals(p.name) && this.age == p.age;	//名字和年龄都相同的话,那肯定是同一个人
	}*/
	
	public String getName()
	{
		return name;
	}
	
	public int getAge()
	{
		return age;
	}
	
	public String toString()
	{
		return name + "::" + age;
	}
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章