主要讲解集合类中Collection和Map两种集合。
Collectin主要包括两种类型:
1、 List集合
该集合的特点是有序,并且其元素可以重复。
List集合中又包括以下三种集合:
ArrayList:其底层数据结构为数组数据结构,这种数据结构的特点是查询速度快,增删元素速度相对较慢,并且不同步。
LinkedList:其底层数据结构为链表数据结构,这种数据结构的特点是查询速度较慢,增删元素速度较快。
Vector:器底层数据结构为数组数据结构,这种数据结构的特点是查询速度快,增删元素速度相对较慢,并且同步。这种数据结构是早期版本出现的,现在用的比较少。
List集合的示例代码:
package Collection;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList al=new ArrayList();
al.add(new Person("list0",10));
al.add(new Person("list0",10));
al.add(new Person("list1",15));
al=singleElement(al);
for(Iterator it=al.iterator();it.hasNext();){
Person p=(Person)it.next();
System.out.println(p.getName()+"..."+p.getAge());
}
}
public static ArrayList singleElement(ArrayList al){
System.out.println("**");
ArrayList a=new ArrayList();
for(Iterator it=al.iterator();it.hasNext();){
Object obj=it.next();
if(!a.contains(obj))
a.add(obj);
}
System.out.println("a::"+a);
return a;
}
}
class Person{
private String name;
private int age;
Person(String name,int age){
this.name=name;
this.age=age;
}
@Override
public boolean equals(Object obj){
if(!(obj instanceof Person))
return false;
Person p=(Person) obj;
System.out.println(this.name+""+p.name);
return this.name.equals(p.name)&&this.age==p.age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
}
2、 Set集合
Set集合的特点是元素无序,且不可以存在重复元素。
Set集合中又包括以下两种集合:
HashSet:这种集合的底层数据结构是哈希表,线程是非同步的。保证元素唯一性的原理:判断元素的hashCode值是否相同。如果相同,还会继续判断元素的equals方法,是否为true。
TreeSet:底层数据结构是二叉树。可以对Set集合中的元素进行排序。保证元素唯一性的依据:compareTo()方法。
TreeSet集合对元素排序有两种方法:
第一种方法:是根据元素自己的自然顺序进行排序,如果该类元素不具备排序方法,则应该在该类的内部复写CompareTo()方法,指定其排序的依据。代码如下:
Import java..util.*;
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));
Iterator it = ts.iterator();
while(it.hasNext())
{
Student stu = (Student)it.next();
System.out.println(stu.getName()+"..."+stu.getAge());
}
}
}
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)
{
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;
}
}
第二种方法:TreeSet集合的构造方法中可以自己传一个比较器,来为其元素进行排序,这种比较器可以根据自己要求的顺序自己定义其排序方法。并且当此方法与第一种方法同时存在的时候,以此方法为主。代码如下:
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)
{
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 TreeSetDemo2
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Student("lisi02",22));
ts.add(new Student("lisi02",21));
ts.add(new Student("lisi007",20));
ts.add(new Student("lisi09",19));
Iterator it = ts.iterator();
while(it.hasNext())
{
Student stu = (Student)it.next();
System.out.println(stu.getName()+"..."+stu.getAge());
}
}
}
class MyCompare implements Comparator
{
public int compare(Object o1,Object o2)
{
Student s1 = (Student)o1;
Student s2 = (Student)o2;
int num = s1.getName().compareTo(s2.getName());
if(num==0)
{
return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
/*
if(s1.getAge()>s2.getAge())
return 1;
if(s1.getAge()==s2.getAge())
return 0;
return -1;
*/
}
return num;
}
}
Map集合:该集合存储键值对。一对一对往里存。而且要保证键的唯一性。
Map集合主要包括三种类型的集合。
HashTable集合:其数据结构是哈希表。这种集合中不可以存入null键和null值。并且该集合线程同步。
HashMap集合:其底层数据结构是哈希表。这种集合中可以存入null键和null值。并且该集合线程不同步。
TreeMap集合:其底层数据结构是二叉树。这种集合的特点是可以用于给Map集合中的键进行排序。
map集合的两种取出方式:
1, Set<k> keySet:将map中所有的键存入到Set集合。因为set具备迭代器。所有可以代方式取出所有的键,在根据get方法。获取每一个键对应的值。Map集合的取出原理:将map集合转成set集合。在通过迭代器取出。
2,Set<Map.Entry<k,v>> entrySet:将map集合中的映射关系存入到了set集合中,而这个关系的数据类型就是:Map.Entry。Entry其实就是Map中的一个static内部接口。
为什么要定义在内部呢?因为只有有了Map集合,有了键值对,才会有键值的映射关系。关系属于Map集合中的一个内部事物。而且该事物在直接访问Map集合中的元素。
上述两种方法的代码如下:
import java.util.*;
class MapDemo2
{
public static void main(String[] args)
{
Map<String,String> map = new HashMap<String,String>();
map.put("02","zhangsan2");
map.put("03","zhangsan3");
map.put("01","zhangsan1");
//将Map集合中的映射关系取出。存入到Set集合中。
Set<Map.Entry<String,String>> entrySet = map.entrySet();
Iterator<Map.Entry<String,String>> it = entrySet.iterator();
while(it.hasNext())
{
Map.Entry<String,String> me = it.next();
String key = me.getKey();
String value = me.getValue();
System.out.println(key+":"+value);
}
/*
//先获取map集合的所有键的Set集合,keySet();
Set<String> keySet = map.keySet();
//有了Set集合。就可以获取其迭代器。
Iterator<String> it = keySet.iterator();
while(it.hasNext())
{
String key = it.next();
//有了键可以通过map集合的get方法获取其对应的值。
String value = map.get(key);
System.out.println("key:"+key+",value:"+value);
}*/
}
}