Java集合类
来源:互联网 发布:win 串口编程 编辑:程序博客网 时间:2024/04/29 18:19
Java中的集合框架类全都继承java.util.Collection接口,这个接口没有直接的实现类,只能在由子接口的实现类去实现里面的方法.具体的结构如图:
nCollection:集合层次中的根接口,JDK没有提供这个接口直接的实现类。
List:是一个有序的集合,可以包含重复的元素。提供了按索引访问的方式。
nSet:不能包含重复的元素。SortedSet是一个按照升序排列元素的Set。
n
Map:包含了key-value对。Map不能包含重复的key。SortedMap是一个按照升序排列key的Map。
n
实现set接口的类有:HashSet,LinkedHashSet; 实现List接口的类有:ArrayLink,LinkedList,实现Map接口的类有:HashMap;实现SortSet接口的类有:TreeSet,实现 SortMap接口的类有:TreeMap.
1、ArrayList:相当于能自动增长容量的数组。使用add()方法增加元素。get(int index)获取元素。remove删除元素。其中,toArray()方法是将一个元素列表转换为数组的方法。在Arrays类中的asArrayList() 方法能够将数组转换成列表。这两个方法实现了列表与数组的互为转换。
2、Iterator 迭代器
由于具些集合类没有相应读取元素的get()方法,但是都有iterator()方法。所以可以用Iterator类实现所有集合类的公共读取,而不需要为每个类编写读取方法,十分方法好用。
public static void printElement(Collection c)
...{
Iterator it = c.iterator();
while(it.hasNext())
...{
System.out.println(it.next());
}
}
...{
Iterator it = c.iterator();
while(it.hasNext())
...{
System.out.println(it.next());
}
}
这是一段读取集合类元素的方法,只要调用这个方法,避免了重复编写读取方法。
3、LinkedList 由双向循环列表实现。采用的数据结构可以为队列,栈。其中,队列的数据结构为先进先出的方式,栈的为先进后出的方式。LinkedList则是以一般的双向链表(double-linked list)完成,其内每个对象除了数据本身外,还有两个 引用,分别指向前一个元素和后一个元素。与ArrayList 相比,采用LinkedList在数据的插入、删除方面更加灵活。ArrayList底层由数组实现,在数据的读取方面方便,在具体的应用上,要根据这两个方面去考虑。
4、HashSet 实现Set接口的hash table(哈希表),依靠HashMap来实现的。
我们应该为要存放到散列表的各个对象定义hashCode()和equals().
import java.util.*;
public class HashSetTest
...{
public static void main(String[] args)
...{
HashSet hs = new HashSet();
hs.add(new Student(1,"zhngsan")); //HashSet 中要定义 hashCode( )和 equals( ) 的作用。
hs.add(new Student(2,"zhngsan2"));
hs.add(new Student(3,"zhngsan3"));
hs.add(new Student(1,"zhngsan")); // 相同的 num 和 name
Iterator it = hs.iterator();
while(it.hasNext())
...{
System.out.println(it.next());
}
}
}
class Student
...{
int num;
String name;
Student(int num,String name)
...{
this.num = num;
this.name = name;
}
public int hashCode() //重写hashCode( )方法,重新定义散列码
public class HashSetTest
...{
public static void main(String[] args)
...{
HashSet hs = new HashSet();
hs.add(new Student(1,"zhngsan")); //HashSet 中要定义 hashCode( )和 equals( ) 的作用。
hs.add(new Student(2,"zhngsan2"));
hs.add(new Student(3,"zhngsan3"));
hs.add(new Student(1,"zhngsan")); // 相同的 num 和 name
Iterator it = hs.iterator();
while(it.hasNext())
...{
System.out.println(it.next());
}
}
}
class Student
...{
int num;
String name;
Student(int num,String name)
...{
this.num = num;
this.name = name;
}
public int hashCode() //重写hashCode( )方法,重新定义散列码
...{
return num*name.hashCode();
}
public boolean equals(Object o) //定义散列码。
...{
Student s = (Student)o;
return num == s.num && name.equals(s.name);
}
public String toString()
...{
return num + ":" + name;
}
}
return num*name.hashCode();
}
public boolean equals(Object o) //定义散列码。
...{
Student s = (Student)o;
return num == s.num && name.equals(s.name);
}
public String toString()
...{
return num + ":" + name;
}
}
HashSet中不容许有重复元素,但在上个例子中如果不重写hashCode和equals ,将会有两个相同num和name的对象。
5、TreeSet 与HashSet用法类似,但TreeSet具有排序功能,所以要实现Comparable接口:
import java.util.*;
public class TreeSetTest
...{
public static void main(String args[])
...{
TreeSet ts = new TreeSet(new Student.StudentComparator());
ts.add(new Student(1,"ha"));
ts.add(new Student(3,"myboel"));
ts.add(new Student(2,"kjn"));
ts.add(new Student(3,"skh"));
Iterator it = ts.iterator();
while(it.hasNext())
...{
System.out.println(it.next());
}
}
}
class Student implements Comparable
...{
int num;
String name;
Student(int num,String name)
...{
this.num = num;
this.name = name;
}
static class StudentComparator implements Comparator
...{
public int compare(Object o1,Object o2) //覆盖Comparator接口的compare(T,T)方法和equals()方法。
...{
Student s1 = (Student)o1;
Student s2 = (Student)o2;
int result = s1.num > s2.num? 1 : (s1.num == s2.num? 0 : -1);
if(s1.num == s2.num)
...{
result = s1.name.compareTo(s2.name);
}
return result;
}
}
public int compareTo(Object o) //覆盖Comparable中的compareTo()方法
...{
Student s = (Student)o;
return num > s.num? 1 : (num == s.num? 0 : -1);
}
public String toString()
...{
return num + ":" + name;
}
}
public class TreeSetTest
...{
public static void main(String args[])
...{
TreeSet ts = new TreeSet(new Student.StudentComparator());
ts.add(new Student(1,"ha"));
ts.add(new Student(3,"myboel"));
ts.add(new Student(2,"kjn"));
ts.add(new Student(3,"skh"));
Iterator it = ts.iterator();
while(it.hasNext())
...{
System.out.println(it.next());
}
}
}
class Student implements Comparable
...{
int num;
String name;
Student(int num,String name)
...{
this.num = num;
this.name = name;
}
static class StudentComparator implements Comparator
...{
public int compare(Object o1,Object o2) //覆盖Comparator接口的compare(T,T)方法和equals()方法。
...{
Student s1 = (Student)o1;
Student s2 = (Student)o2;
int result = s1.num > s2.num? 1 : (s1.num == s2.num? 0 : -1);
if(s1.num == s2.num)
...{
result = s1.name.compareTo(s2.name);
}
return result;
}
}
public int compareTo(Object o) //覆盖Comparable中的compareTo()方法
...{
Student s = (Student)o;
return num > s.num? 1 : (num == s.num? 0 : -1);
}
public String toString()
...{
return num + ":" + name;
}
}
只有在要实现排序功能时才使用TreeSet,否则我们用HashSet,更快速。
6、HashMap 是对Key进行散列。 HashMap 实现了Map接口,Map接口与Collection 接口没有任何关系。所以HashMap中没有add( )方法。对键值插入用的是put(key,value)方法。读取时用get(int key). 使用KeySet( ) 可以返回类型是Set 的键视图。使用values( )可以返回类型为Collection的值视图。对于视图读取可以用 Iterator 迭代器进行迭代。
entrySet( ) 方法返回的是类型为Set,包含映射关系的 collection 视图 。在返回的集合中,每个元素都是一个 Map.Entry。
import java.util.*;
public class HashMapTest
...{
public static void printElements(Collection c) //迭代器进行迭代
...{
Iterator it = c.iterator();
while(it.hasNext())
...{
System.out.println(it.next());
}
}
public static void main(String[] arg)
...{
HashMap<Object,Object> hs = new HashMap<Object,Object>();
hs.put(1,"abc"); //使用put( )方法添加元素
hs.put(3,"bc");
hs.put(2,"fdfdd");
/**//*System.out.println(hs.get(1)); //具有get( 键 )方法。
System.out.println(hs.get(2));
System.out.println(hs.get(3));*/
Set keys = hs.keySet(); //返回键的视图
System.out.println("Key:");
printElements(keys);
Collection cl = hs.values(); //返回值的视图
System.out.println("Values:");
printElements(cl);
Set entry = hs.entrySet(); //键值映射关系的Collection视图
Iterator it = entry.iterator();
//printElements(entry);
while(it.hasNext())
...{
Map.Entry me = (Map.Entry)it.next(); //分开读取键和值
System.out.println(me.getKey()+":"+me.getValue());
}
}
}
public class HashMapTest
...{
public static void printElements(Collection c) //迭代器进行迭代
...{
Iterator it = c.iterator();
while(it.hasNext())
...{
System.out.println(it.next());
}
}
public static void main(String[] arg)
...{
HashMap<Object,Object> hs = new HashMap<Object,Object>();
hs.put(1,"abc"); //使用put( )方法添加元素
hs.put(3,"bc");
hs.put(2,"fdfdd");
/**//*System.out.println(hs.get(1)); //具有get( 键 )方法。
System.out.println(hs.get(2));
System.out.println(hs.get(3));*/
Set keys = hs.keySet(); //返回键的视图
System.out.println("Key:");
printElements(keys);
Collection cl = hs.values(); //返回值的视图
System.out.println("Values:");
printElements(cl);
Set entry = hs.entrySet(); //键值映射关系的Collection视图
Iterator it = entry.iterator();
//printElements(entry);
while(it.hasNext())
...{
Map.Entry me = (Map.Entry)it.next(); //分开读取键和值
System.out.println(me.getKey()+":"+me.getValue());
}
}
}
7、TreeMap 具有对Key排序功能。与HashMap 相比,和Set类似,HashMap的速度通常都比TreeMap快,只有在需要排序的功能的时候,才使用TreeMap。
- Java集合:集合类详解
- Java集合-常用集合类
- JAVA【集合一】集合类
- JAVA中的集合类
- JAVA中的集合类
- JAVA中的集合类
- Java集合类(整理)
- java集合类
- JAVA中的集合类
- Java集合类笔记
- JAVA中的集合类
- java集合类
- java集合类总结
- JAVA中的集合类 - -
- JAVA中的集合类
- java集合类
- java 集合类
- java中的集合类
- 通用SQL数据库查询语句精华使用简介
- 如何利用SQL Server发邮件?
- 关于上一篇文章的补充
- 在北大听讲座
- 北京?是好是坏?
- Java集合类
- 如何才能比较精确测试模块运行的时间?
- 自动远程刷屏或跳转
- 文字直播亚洲杯中国对伊朗
- 安静的等你。。。
- 陋室铭
- ajax与servlet的一个简单分页
- 想要学习的东西
- const的用法