Java 集合总结

来源:互联网 发布:机动车扣分查询软件 编辑:程序博客网 时间:2024/06/06 16:36

集合框架图

Collection

Map


1.集合框架图


其中Iterator为迭代器,用hasNext()判断是否还有元素,用next()返回其中的元素。ListIterator只能出现在list中使用,其使用了线程的同步,也就是当遍历的时候,可以对集合中的元素进行添加删除等。Collection只能装 单个的元素,Map是以键值对的方式存入的。Collection子类接口List中存储的元素是可以重复的,也就是以下标的方式访问。Set中的元素是不能重复的。Map中的元素都是键值对对应的,其中键是不能重复的。

List的子类ArrayList是读取速度较快插入删除较慢,LinkedList是链表插入删除较快,读取很慢。修改多的时候用后者,否则就用前者。Set中的hashset底层数据结构是hash表结构,其存储的对象要根据条件复写自己的hashCode()和equals()方法。TreeSet底层数据结构是二叉树,其存储的对象要实现comparable接口或者传入自定义comparator对其存入的对象比较,其中return0就是其判断是否唯一性的标志。

map中的HashMap和TreeMap其中的key集合就是hashset和treeset。其遍历也是通过遍历这两个集合实现了,或者是用Map.entrySet()返回键值对的关系然后取出。

2.Collection

ListIterator和Iterator

import java.util.*;class Test{public static void main(String[] arg){List<String> l1 = new ArrayList<String>();l1.add("s1");l1.add("s2");l1.add("s3");l1.add("s4");//Iterator<String> it = l1.iterator();/*--------此处代码会出现异常----------*//*原因是因为使用迭代器取出的时候,在往集合中添加元素,造成不能同时完成的操作而抛出runtime异常*//*while(it.hasNext()){String s = it.next();if(s.equals("s3")){l1.add("s5");}System.out.println(s);}*/ListIterator<String> it = l1.listIterator();while(it.hasNext()){String s = it.next();if(s.equals("s3")){it.add("s5");}System.out.println(s);}System.out.println(l1);}}
HashSet实例:

hashset存储的方式就是按照所存储的对象的hash值所存储,如果对象的hash值纯在的话那就用equas方法比较两个对象是否一样,如果一样的话就不在存储。
也就是说,hashset的速度比较快 但是你要提供合理的hashCode方法以及equeas方法。
下面是案例
判断元素是否纯在依赖的是元素的hashCode和equals方法
先判断hashCode
然后判断equals
arraylist 判断是否一样依赖的是equals方法

import java.util.*;class Person{public String name;public int age;Person(String name,int age){this.name = name;this.age = age;}public int hashCode(){return name.hashCode()+age;}public boolean equals(Object o){if(!(o instanceof Person))return false;Person p = (Person)o;return p.name.equals(this.name)&&p.age==this.age;}}class Test{public static void main(String[] arg){Set<Person> s = new HashSet<Person>();s.add(new Person("李斯",14));s.add(new Person("李斯",14));s.add(new Person("王五",16));s.add(new Person("张三",17));Iterator it = s.iterator();while(it.hasNext()){Person p = (Person)it.next();System.out.println(p.name+"......"+p.age);}}}

treeset实例:/*
此段代码实现了 可以存储不同年龄的学生

class Student implements Comparable{public String name;public int age;public Student(String name,int age){this.name = name;this.age = age;}public int compareTo(Object o){if(!(o instanceof Student))throw new RuntimeException("存入的不是该类型");Student s1 = (Student)o;if(s1.age > this.age)return 1;if(s1.age == this.age)return s1.name.compareTo(this.name);return -1;}}class Test{public static  void main(String[] arg){Set<Student>  s = new TreeSet<Student>();s.add(new Student("张三",14));s.add(new Student("张三1",15));s.add(new Student("张三2",16));s.add(new Student("张三3",18));s.add(new Student("张三4",18));Iterator it = s.iterator();while (it.hasNext()){Student s1 = (Student)it.next();System.out.println(s1.name+"*****"+s1.age);}}}


总结:
!-------TreeSet:存储不同的元素并且排序,底层是二叉树结构  其方法是comparable方法。
(当其要按照存入的顺序或者是逆序存取的时候就可以在其 comparable方法中设置其返回值为1或者是-1 如果只要有一个就return0)
其除了实现接口 也可以继承自comparator。 然后再构造treeset的时候传入。
!-------HashSet:存储不同的元素,底层是hash表结构 其方法是先判断是够有相同的hash码 如果有 在判断equals方法
*/

3.Map

map遍历的两种方式 其中键的存储与set一样。

import java.util.*;class Test{public static void main(String[] arg){Map<String,String>  map = new HashMap<String,String>();map.put("key1","001");map.put("key2","002");map.put("key3","003");map.put("key4","004");/*---------第一种遍历----------*/Set<String> keyset = map.keySet();Iterator<String> it = keyset.iterator();while(it.hasNext()){String value = map.get(it.next());System.out.println(value);}/*---------第二种遍历----------*//*这一种遍历使用了entrySet方法,其返回的一组映射,可以利用你内部封装的方法getValue和getKey方法来获取  其对应的值或者是键,而其中Map.Entry是属于map中的内部类,其可以访问map中的数据,并且提供了一组方法用于返回  其一组键值对的中的值 */Set<Map.Entry<String,String>> entryset = map.entrySet();Iterator<Map.Entry<String,String>> it1 = entryset.iterator();while(it1.hasNext()){Map.Entry<String,String> entry = it1.next();System.out.println(entry.getKey()+"-----"+entry.getValue());}}}

总结:
!---HashTable:底层是hash表结构,线程是同步的,不允许使用null值和null键
!---HashMap:  底层是hash表结构,线程是不同步的,允许使用null值和null键,效率高
!---TreeMap:底层是二叉树结构,线程不同步,可以对键进行排序。


!————Map集合中没有迭代器;其取出的原理就是将map集合转成set集合,然后再通过set集合中
迭代器来遍历集合
keySet()
entrySet()

0 0
原创粉丝点击