---------------------- android培训、java培训、期待与您交流! ----------------------
Java平台提供了一个全新的集合框架。“集合框架”主要由一组用来操作对象的接口组成。不同接口描述一组不同数据类型。
· 集合接口:6个接口(短虚线表示),表示不同集合类型,是集合框架的基础。
· 抽象类:5个抽象类(长虚线表示),对集合接口的部分实现。可扩展为自定义集合类。
· 实现类:8个实现类(实线表示),对接口的具体实现。
在很大程度上,一旦您理解了接口,您就理解了框架。虽然您总要创建接口特定的实现,但访问实际集合的方法应该限制在接口方法的使用上;因此,允许您更改基本的数据结构而不必改变其它代码。
· Collection 接口是一组允许重复的对象。
· Set 接口继承 Collection,但不允许重复,使用自己内部的一个排列机制。
· List 接口继承 Collection,允许重复,以元素安插的次序来放置元素,不会重新排列。
· Map接口是一组成对的键-值对象,即所持有的是key-value pairs。Map中不能有重复的key。拥有自己的内部排列机制。
· 容器中的元素类型都为Object。从容器取得元素时,必须把它转换成原来的类型。
一 List接口
在“集合框架”中有两种常规的 List 实现:ArrayList 和 LinkedList。使用两种 List 实现的哪一种取决于您特定的需要。如果要支持随机访问,而不必在除尾部的任何位置插入或除去元素,那么,ArrayList 提供了可选的集合。但如果,您要频繁的从列表的中间位置添加和除去元素,而只要顺序的访问列表元素,那么,LinkedList 实现更好。
代码如下:
package diary;
import java.util.*;
public class ListDemo {
public static void main(String[] args) {
//ArrayListDemo();
LinkedListDemo();
}
// 演示ArrayList 操作
public static void ArrayListDemo()
{
ArrayList al = new ArrayList();
al.add("java01");
al.add("java02");
al.add("java01");
al.add("java02");
al.add("java01");
System.out.println("remove:"+al.remove(1)); // 删除第一个位置上的元素
Iterator it = al.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
// 演示LinkedList 操作
public static void LinkedListDemo()
{
LinkedList link = new LinkedList();
link.add("java01");
link.add("java02");
link.add("java03");
link.add("java01"); // 添加重复元素,添加成功
System.out.println(link.removeFirst()); // 删除第一个元素
ListIterator li = link.listIterator();
while(li.hasNext())
{
System.out.println(li.next());
}
}
}
二 Set 接口
Set 接口继承 Collection 接口,而且它不允许集合中存在重复项,每个具体的 Set 实现类依赖添加的对象的 equals()方法来检查独一性。Set接口没有引入新方法,所以Set就是一个Collection,只不过其行为不同。
“集合框架”支持Set接口两种普通的实现:HashSet和TreeSet(TreeSet实现SortedSet接口)。在更多情况下,您会使用 HashSet 存储重复自由的集合。考虑到效率,添加到 HashSet 的对象需要采用恰当分配哈希码的方式来实现hashCode()方法。虽然大多数系统类覆盖了 Object中缺省的hashCode()和equals()实现,但创建您自己的要添加到HashSet的类时,别忘了覆盖 hashCode()和equals()。
当您要从集合中以有序的方式插入和抽取元素时,TreeSet实现会有用处。为了能顺利进行,添加到TreeSet的元素必须是可排序的。
package diary;
import java.util.*;
public class SetDemo {
public static void main(String[] args) {
HashSetDemo();
TreeSetDemo();
}
// 演示HashSet 操作
public static void HashSetDemo()
{
HashSet hs = new HashSet();
hs.add(new Person("a1",11));
hs.add(new Person("a2",12));
hs.add(new Person("a3",13));
System.out.println(hs.add(new Person("a2",12))); //false 不能添加相同的元素
Iterator it = hs.iterator();
while(it.hasNext())
{
Person p = (Person)it.next();
System.out.println((p.getName()+"::"+p.getAge()));
}
}
public static void TreeSetDemo()
{
TreeSet ts = new TreeSet();
ts.add("a1");
ts.add("me");
ts.add("ball");
ts.add("qq");
System.out.println(ts.add("me")); // false 不能添加相同的元素
Iterator it = ts.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class Person
{
private String name;
private int age;
Person(String name,int age)
{
this.name = name;
this.age = age;
}
public int hashCode()
{
System.out.println(this.name+"....hashCode");
return name.hashCode()+age*37;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Person))
return false;
Person p = (Person)obj;
System.out.println(this.name+"...equals.."+p.name);
return this.name.equals(p.name) && this.age == p.age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}
三 Map接口
Map接口不是Collection接口的继承。Map接口用于维护键/值对(key/value pairs)。该接口描述了从不重复的键到值的映射。
“集合框架”提供两种常规的Map实现:HashMap和TreeMap (TreeMap实现SortedMap接口)。在Map 中插入、删除和定位元素,HashMap 是最好的选择。但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好。使用HashMap要求添加的键类明确定义了hashCode()和equals()的实现。
package diary;
import java.util.*;
class MapDemo
{
public static void main(String[] args)
{
HashMapDemo();
}
// 演示Map操作
public static void HashMapDemo()
{
Map<String,String> map = new HashMap<String,String>();
//添加元素,添加元素,如果出现添加时,相同的键。那么后添加的值会覆盖原有键对应值。
//并put方法会返回被覆盖的值。
System.out.println("put:"+map.put("01","zhangsan1"));
System.out.println("put:"+map.put("01","wnagwu"));
map.put("02","zhangsan2");
map.put("03","zhangsan3");
System.out.println("containsKey:"+map.containsKey("022"));
//System.out.println("remove:"+map.remove("02"));
System.out.println("get:"+map.get("023"));
map.put("04",null);
System.out.println("get:"+map.get("04"));
//可以通过get方法的返回值来判断一个键是否存在。通过返回null来判断。
}
// 两种方式遍历Map集合中的元素
public static void listMap()
{
Map<String,String> map = new HashMap<String,String>();
map.put("02","zhangsan2");
map.put("03","zhangsan3");
map.put("01","zhangsan1");
map.put("04","zhangsan4");
// 第二种方法:使用entrySet
//将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);
}
// 第一种方法:使用ketSet
/*
//先获取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);
}
*/
}
}
---------------------- android培训、java培训、期待与您交流! ----------------------