您的位置:首页 > 编程语言 > Java开发

【Java就业培训教材】——集合的学习

2014-08-17 20:13 302 查看
集合这部分的内容比较多也比较杂,把这几天学习的东西都总结一下,坚持写blog



上图是Java集合类的一个关系图,其中虚线表示的是接口,实线表示的是类。

我们都知道对象是用来封装数据的,当对象多了之后就产生了集合。集合就是用来存储对象的数据结构。

集合的特点:(1)用于存储对象的容器

(2)集合的长度是可以变的

(3)集合中不可以存储基本数据类型,存储的必须是对象

一.Collection

集合容器因为内部的数据结构不同,有多种具体容器,不断向上抽取就形成了集合框架,框架的顶层就是collection接口。

collection接口中常用的方法分类

1.添加:

boolean add(E e)在这里我们把E当成Object,添加成功就返回true,添加失败就返回false。

boolean addAll(Collection c)将指定的Collection中所有的元素都添加到此Collection中

2.删除

boolean remove(Object obj)从此Collection中删除指定元素,删除成功返回true,否则返回false

boolean removeAll(Collectin c)将C1和C2共同拥有的元素删除

boolean retainAll(Collection c)将C1和C2取交集

3.判断

boolean contains(Object obj)如果此C1 中包含指定的元素则返回true

boolean containsAll(Object obj)如果此Collection中包含指定obj中所有元素则返回true

boolean isEmpty()如果此Collection不包含元素,则返回true

4.获取

int size() 返回此Collection中的元素数

Iterator<E> iterator()返回此collection的元素上进行迭代的迭代器。

collection接口常用方法举例:

import java.util.*;

public class CollectionDemo
{
public static void main(String[] args)
{
System.out.println("Hello World!");

Collection col = new ArrayList();
Collection col_temp = new ArrayList();

//add
col.add("abc1");
col.add("abc2");
col_temp.add("halo1");
col_temp.add("halo2");
col_temp.addAll(col);
System.out.println(col);
System.out.println(col_temp);
System.out.println("*******************************");

//delete
col.remove("abc1");
System.out.println(col);
System.out.println(col_temp);
col_temp.retainAll(col);
System.out.println(col);
System.out.println(col_temp);
System.out.println("*******************************");

//judge
System.out.println(col.isEmpty());
System.out.println(col.contains("abc2"));
System.out.println(col_temp.containsAll(col));
System.out.println("*******************************");

//get
System.out.println("size: " + col.size());
System.out.println("size: " + col_temp.size());
Iterator it = col.iterator();
Iterator it_temp = col.iterator();
while(it.hasNext()){System.out.println(it.next());}
while(it_temp.hasNext()){System.out.println(it_temp.next());}
System.out.println("*******************************");
}
}


二.Collection下的两个子接口

Collection下有两个常用的接口,分别是List和Set,两个接口的特点分别如下:

List:元素都有索引(脚标),元素可以重复。

Set:元素不能重复

1.List常用的方法

List是Collection的子接口,上面将的Collection方法在List中都有,而且用法也相似。下面说说List接口中特有的方法

Iterator<E> iterator()用法和Collection接口中的方法相似

ListIterator<E> listIterator()返回列表元素的列表迭代器

ListIterator<E> listIterator(int index)返回列表中元素的列表迭代器,从列表中指定的索引开始。ListIterator接口是列表迭代器,允许程序员从任一方向遍历列表

E set(int index,E element)用指定的元素替换列表中指定的位置的元素,返回以前在指定未知的元素

List<E> subList(int fromIndex,int toIndex)返回列表总指定的fromIndex(包括)和toIndex(不包括)之间的列表

E get(int index)返回列表中指定位置的元素

例子:

import java.util.*;

public class CollectionDemo
{
public static void main(String[] args)
{
System.out.println("Hello World!");

List col = new ArrayList();

col.add("abc1");
col.add("abc2");
col.add("abc3");
col.add("abc4");
col.add("abc5");

ListIterator it = col.listIterator();
while(it.hasNext())
{
System.out.println(it.next());
}
System.out.println("******************************");
while(it.hasPrevious())
{
System.out.println(it.previous());
}
System.out.println("******************************");
col = col.subList(1,4);
System.out.println(col);
col.set(0,"abc0");
System.out.println(col);
}
}


2.List接口下实现的类

常用的有三个分别时:

ArrayList:内部是数组数据结构,是不实现线程同步,增删元素会改变长度,查询的速度快。

LInkedList:内部是链表数据结构,是不实现线程同步,增删元素的速度很快。

Vector:内部是数组数据结构,是实现线程同步,增删查询速度都很慢。

(1)ArrayList类常用的方法:

ArrayList的构造函数

ArrayList()构造一个初始容量为10的空列表

ArrayList(Collection col)构造一个包含指定collection的元素的列表,这些是按照该collection的迭代器返回它们的顺序排列的

ArrayList*int initialCapacity)构造一个具有指定初始化容量的空列表

下面这个例子说明怎么使用contains(Object obj)方法判断自己自定义对象是否已经包含在ArrayList中,此时我们需要复写equals()方法,因为ArrayList类中的contains()方法调用的是equals()方法进行判断,我们根据我们自定义对象的需求修改比较规则。

import java.util.*;

public class ArrayListDemo
{
public static void main(String[] args)
{
System.out.println("Hello World!");

ArrayList list = new ArrayList();

//add
list.add(new Student("zhangsan",14));
list.add(new Student("lisi",15));
list.add(new Student("wangwu",16));
list.add(new Student("zhaoliu",17));
list.add(new Student("zhangsan",14));

Iterator it = list.iterator();
while(it.hasNext())
{
Student student = (Student)it.next();
System.out.println("name: " + student.getName() + " ... " + "age: " + student.getAge());
}
System.out.println("****************************************");
System.out.println(list.contains(new Student("zhangsan",14)));
}
}

class Student
{
private String name;
private int age;

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

public void setName(String name)
{
this.name = name;
}

public void setAge(int age)
{
this.age = age;
}

public String getName(){return name;}
public int getAge(){return age;}

public boolean equals(Object obj)
{
Student student = (Student)obj;
int temp = this.age - student.age;
if(temp == 0)
return this.name.equals(student.name);
else
return false;
}

}


(2)LinkedLIst类常用的方法:

LinkedList的构造方法

LinkedLIst()构造一个空列表

LinkedLIst(Collection c)构造一个包含指定collection中的元素的列表,这些元素按其collection的迭代器返回的顺序排序。

void addFirst(E e)将指定元素插入此列表的开头

void addLast(E e)将指定元素添加到此列表的结尾

E get(int index)返回此列表中指定位置处的元素 获取但不移除元素,如果列表为空,则抛出异常

E getFirst()返回此列表的第一个元素 获取但不移除元素,如果列表为空,则抛出异常

E getLast()返回此列表的最后一个元素 获取但不移除元素,如果列表为空,则抛出异常

E peekFirst() 获取但不移除此列表中的第一个元素,如果此列表为空,则返回null

E peekLast()获取但不移除此列表中的最后一个元素,如果此列表为空,则返回null

E pollFirst() 获取并移除此列表的第一个元素,如果此列表为空,则返回null

E pollLast()获取并移除此列表的最后一个元素,如果此列表为空,则返回null

E remove(Object o)移除并返货此列表中首次出现的指定元素

E removeFirst()移除并返回此列表的第一个元素

E remove(int index)移除此列表中指定位置处的元素

3.Set接口常用的方法

Set接口中的方法和Collection接口中的方法大同小异,使用方法也类似。

4.Set接口下常用的子类

HashSet:内部数据结构是Hash表(哈希表),是无序迭代而且不同步的。

TreeSet:内部数据结构是二叉树,可以按照自然顺序对元素进行排序,是不同步的。

(1)Hashset

HashSet的构造方法:

HashSet()构造一个新的空set

HashSet(Collection c)构造一个包含指定collection中的元素的新set

HashSet(int initialCapacity)构造一个指定容量的空set

HashSet的常用方法

boolean add(E e)向HashSet中添加指定元素

void clear()  将HashSet中的元素都清除掉

boolean contains(Object obj) 判断HashSet中是否包含指定的元素,如果有则返回ture,否则返回false

boolean isEmpty() 判断此HashSet是否为空

Iterator<E> iterator() 返回此HashSet中元素进行迭代的迭代器

boolean remove(Object obj) 如果指定元素存在此HashSet中,则将其移除

int size() 返回此HashSet中元素的数量,即HashSet中的容量

举例:

import java.util.*;

public class HashSetDemo
{
public static void main(String[] args)
{
System.out.println("Hello World!");

HashSet hs = new HashSet();
hs.add("abc1");
hs.add("abc2");
hs.add("abc3");
hs.add("abc4");
hs.add("abc5");
System.out.println(hs);
System.out.println(hs.contains("abc2"));
System.out.println(hs.isEmpty());

Iterator it = hs.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
System.out.println(hs.size());
}
}


下面说说HashSet是怎么保证元素不重复的,在HashSet中通过HashCode()和equals()两个方法来保证元素的唯一性,首先先判断两个元素的哈希值是否相同,如果相同再判断两个元素的内容是否相同。判断元素的哈希值是否相同使用的就是HashCode的方法,判断两个对象的内容是否相同则使用的是equals方法。当我们在HashSet中添加自定义对象的时候就要重新复写HashCode和equals方法,否则不能保证HashSet中的元素唯一性。其中hashCode方法用来说明自定义对象哈希值的运算方法,而equals方法则是用来说明自定义对象的比较方法。

举例:

import java.util.*;

public class HashSetDemo
{
public static void main(String[] args)
{
System.out.println("Hello World!");

HashSet hs = new HashSet();
hs.add(new Student("zhangsan",12));
hs.add(new Student("xiaosi",13));
hs.add(new Student("wangwu",14));
hs.add(new Student("zhaoliu",15));
hs.add(new Student("xiaosi",13));

Iterator it = hs.iterator();
while(it.hasNext())
{
Student student = (Student)it.next();
System.out.println("name:" + student.GetName() + " ... " +"age:" + student.GetAge());
}
}
}

class Student
{
private String name;
private int age;

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

public void SetName(String name){this.name = name;}
public void SetAge(int age){this.age = age;}
public String GetName(){return name;}
public int GetAge(){return age;}

public int hashCode()
{
System.out.println("hashCode");
return name.hashCode() + age;
}

public boolean equals(Object obj)
{
Student student = (Student)obj;
int temp = this.age - student.age;
System.out.println("equals");
if(temp == 0)
return this.name.equals(student.name);
else
return false;
}
}


(2)TreeSet接口

TreeSet的构造函数

TreeSet() 构造一个心得空TreeSet,该set根据其元素的自然顺序进行排序

TreeSet(Collectin c)构造一个包含指定collection元素的新TreeSet,它按照其元素的自然顺序进行排序

TreeSet(Comparator comparator)构造一个新的空TreeSet,它根据指定比较器进行排序

TreeSet的常用方法:

Comparator<E> comparator()返回对此TreeSet中的元素进行排序的比较器,如果此TreeSet使用其元素的自然顺序,则返回null

由于TreeSet的内部的数据结构是二叉树,对于添加到其中的数据有进行排序,当我们添加的元素是自定义对象的时候,系统由于不知道我们自定义对象的排序方法,此时我们就需要复写TreeSet中的比较器,同时在创建TreeSet对象的时候要将比较器添加进来。举例:

import java.util.*;

public class TreeSetDemo
{
public static void main(String[] args)
{
System.out.println("Hello World!");

TreeSet ts = new TreeSet();
ts.add("abc1");
ts.add("abc2");
ts.add("abc3");
ts.add("abc2");
ts.add("abc1");
System.out.println(ts);
Comparator comp = ts.comparator();
System.out.println(comp);
System.out.println("**********************************");

//方法一:在对象类中实现Comparable接口,并复写comparaTo方法
TreeSet ts_student = new TreeSet();
ts_student.add(new Student("zhangsan",13));
ts_student.add(new Student("lisi",14));
ts_student.add(new Student("wangwu",15));
ts_student.add(new Student("lisi",14));
ts_student.add(new Student("zhangsan",13));
comp = ts_student.comparator();
System.out.println(comp);
Iterator it = ts_student.iterator();
while(it.hasNext())
{
Student temp = (Student)it.next();
System.out.println("name:" + temp.GetName() + " ... " + "age:" + temp.GetAge());
}
System.out.println("**********************************");

//方法二:重新写一个比较器,实现Comparator,并在创建TreeSet的时候指明使用什么比较器
TreeSet ts_student1 = new TreeSet(new ComparatorByName());
ts_student1.add(new Student("zhangsan",13));
ts_student1.add(new Student("lisi",14));
ts_student1.add(new Student("wangwu",15));
ts_student1.add(new Student("lisi",14));
ts_student1.add(new Student("zhangsan",13));
comp = ts_student1.comparator();
System.out.println(comp);
Iterator it1 = ts_student1.iterator();
while(it1.hasNext())
{
Student temp = (Student)it1.next();
System.out.println("name:" + temp.GetName() + " ... " + "age:" + temp.GetAge());
}
System.out.println("**********************************");
}
}

class Student implements Comparable
{
private String name;
private int age;

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

public String GetName(){return name;}
public int GetAge(){return age;}

public int compareTo(Object obj)
{
Student student = (Student)obj;
return this.age - student.age;
}

}

class ComparatorByName implements Comparator
{
public int compare(Object obj1,Object obj2)
{
Student student1 = (Student)obj1;
Student student2 = (Student)obj2;

return student1.GetName().compareTo(student2.GetName());
}
}


5.Map接口
Map<K,V>其中K指的是此映射所维护的键的类型,V指的是映射值的类型。Map一次添加一对,Collection则是一次添加一个元素。因此Map也称为双列集合,Collection则称为单列集合。其实Map集合中存储的就是键值对,一个映射中不能包含重复的键,即必须保证键的唯一性。

Map接口常用的方法:

添加:

V put(K key,V value)将指定的值与此映射中指定键关联添加到Map中,并且返回以前与该key关联的值,如果没有关联key的映射关系,则返回null。

删除

void clear()从映射中移除所有映射关系。

value remove(Object key);如果存在指定键key的映射关系,则将其从此映射中移除并返回。

判断

boolean containsKey(Object key)如果此映射包含指定键的映射关系,则返回true,否则返回false

boolean containsValue(value)如果此映射将一个或多个键映射到指定值,则返回true

boolean isEmpty()如果此映射中未包含键-值映射关系,则返回true

获取

V get(Object key)返回指定键值所映射的值,如果此映射不包含在该键的映射关系,则返回null

int size()返回此映射中的键-值映射关系数

举例:

import java.util.*;

public class MapDemo
{
public static void main(String[] args)
{
System.out.println("Hello World!");

Map<String,Integer> map = new HashMap<String,Integer>();

//add
map.put("zhangsan",new Integer(13));//自动装箱
map.put("lisi",new Integer(14));
map.put("wangwu",new Integer(15));
map.put("zhaoliu",new Integer(16));
System.out.println(map);
System.out.println("********************************");

//get
System.out.println(map.get("lisi"));
System.out.println(map.size());
System.out.println("********************************");

//delete
map.remove("lisi");
System.out.println(map);
System.out.println("********************************");

//keySet
Set<String> set_temp = map.keySet();
Iterator it = set_temp.iterator();
while(it.hasNext())
{
String str = (String)it.next();
System.out.println("name:" + str + " ... " +"age" + map.get(str));
}
System.out.println("********************************");

//values
Collection<Integer> col = map.values();
it = col.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
System.out.println("********************************");

//entrySet
Set<Map.Entry<String,Integer>> set_map = map.entrySet();
Iterator<Map.Entry<String,Integer>> entry = set_map.iterator();
while(entry.hasNext())
{
Map.Entry<String,Integer> temp = entry.next();
Integer integer = temp.getValue();
String string = temp.getKey();
System.out.println("name:" + string + " ... " + "age" + integer);
}
System.out.println("********************************");

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: