我是靠谱客的博主 坦率皮带,这篇文章主要介绍java类集框架总结,现在分享给大家,希望可以做个参考。

Collection是一个基础的类集接口,其下有两个主要的接口list和set,list下又有ArrayList和vector

ArrayList

通用方法:

public boolean add()

public boolean remove()

public boolean contains()

public boolean isEmpty()

public void  clear()

public int size()

public Objiect[] toArray()

public Iterator<E>Irerator()

特有方法:

public ListIterator<E>ListIrerator()

public E get(int index)

public E set(int index,E element)

代码演示:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
public class ArrayListAddDemo { public static void main(String args[]) { List<String> allList = new ArrayList<String>();// 为List接口实例化 allList.add("hello");// 增加元素,Collection接口定义 allList.add(0, "MLDN");// 增加元素,List接口定义 allList.add("world");// 增加元素,Collection接口定义 allList.add("world");// 增加元素,Collection接口定义 allList.add("world");// 增加元素,Collection接口定义 allList.add("world");// 增加元素,Collection接口定义 allList.add("world");// 增加元素,Collection接口定义 System.out.println(allList) ; } }


复制代码
1
2
3
4
5
6
7
8
9
10
11
12
public class ArrayListPrintDemo02 { public static void main(String args[]) { Collection<String> allList = new ArrayList<String>();// 为Collection接口实例化 allList.add("hello");// 增加元素,Collection接口定义 allList.add("MLDN");// 增加元素,Collection接口定义 allList.add("world");// 增加元素,Collection接口定义 Object obj[] = allList.toArray() ; for (int i = 0; i < obj.length; i++) {// 循环输出 System.out.println(obj[i]) ; } } }
ArrayList与vector的主要区别是ArrayList采用异步处理,且非线程安全,vector则采用同步处理,线程安全。


Set
Set接口有两个子类分别是HashSet与TreeSet

HashSet通过散列方式无序的存放集合数据。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
public class HashSetPersonAddDemo { public static void main(String[] args) { Set<Person> allSet = new HashSet<Person>(); allSet.add(new Person("张三",30)); // 增加重复元素 allSet.add(new Person("李四",30)); // 增加重复元素 allSet.add(new Person("王五",31)); // 增加重复元素 allSet.add(new Person("赵六",32)); // 增加重复元素 allSet.add(new Person("孙七",32)); // 增加重复元素 allSet.add(new Person("孙七",32)); // 增加重复元素 System.out.println(allSet); } }

此时无序输出所有数据,重复元素并未被插入

TreeSet默认通过类的CompareTo方法来实现顺序排列(类必须先实现Comparble接口)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class Person implements Comparable<Person> { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String toString() { // 覆写toString() return "姓名:" + this.name + ";年龄:" + this.age; } public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Person)) { return false; } Person p = (Person) obj; if (this.name.equals(p.name) && this.age == p.age) { return true; } else { return false; } } public int hashCode() { // 这个方法的返回值都是通过一个公式计算的 // 此时的公式:名字的hashCode * age return this.name.hashCode() * this.age; } public int compareTo(Person o) { if (this.age > o.age) { return 1; } else if (this.age < o.age) { return -1; } else { return this.name.compareTo(o.name); // 比较name } } } public class TreeSetPersonAddDemo { public static void main(String[] args) { Set<Person> allSet = new TreeSet<Person>(); allSet.add(new Person("张三",30)); // 增加重复元素 allSet.add(new Person("李四",30)); // 增加重复元素 allSet.add(new Person("王五",31)); // 增加重复元素 allSet.add(new Person("赵六",32)); // 增加重复元素 allSet.add(new Person("孙七",32)); // 增加重复元素 allSet.add(new Person("孙七",32)); // 增加重复元素 System.out.println(allSet); } }
此时按照年龄顺序输出数据


Iterator(迭代)

学过C++的都很好理解这个接口的用法和含义,不再赘述

常用方法

public boolean hasnext()      判断是否有下一个数据

public E next()                      获取当前数据

public void remove()             移除当前数据

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class IteratorDemo02 { public static void main(String[] args) { List<String> allList = new ArrayList<String>(); allList.add("A"); allList.add("B"); allList.add("C"); allList.add("D"); allList.add("E"); Iterator<String> iter = allList.iterator(); while (iter.hasNext()) { String str = iter.next(); // 取出内容 if ("C".equals(str)) { iter.remove();// 删除元素 } System.out.print(str + "、"); } System.out.println("删除之后的集合:" + allList); } }
ListIterator(双向迭代)
独有方法:

public boolean hasprevios()            判断是否有前一个元素

public E previous()                          取出前一个元素

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class ListIteratorDemo01 { public static void main(String[] args) { List<String> allList = new ArrayList<String>(); allList.add("A"); allList.add("B"); allList.add("C"); allList.add("D"); allList.add("E"); ListIterator<String> iter = allList.listIterator(); System.out.print("从前向后输出:"); while (iter.hasNext()) { System.out.print(iter.next() + "、"); } System.out.print("n从后向前输出:"); while (iter.hasPrevious()) { System.out.print(iter.previous() + "、"); } } }

注意:

迭代后的对象可以和原始对象混用方法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class ListIteratorDemo02 { public static void main(String[] args) { List<String> allList = new ArrayList<String>(); allList.add("A"); allList.add("B"); allList.add("C"); allList.add("D"); allList.add("E"); ListIterator<String> iter = allList.listIterator(); iter.add("X"); // 增加数据 System.out.print("从前向后输出:"); while (iter.hasNext()) { String str = iter.next(); iter.set(str + " - MLDN"); // 修改 System.out.print(str + "、"); } System.out.print("n从后向前输出:"); while (iter.hasPrevious()) { System.out.print(iter.previous() + "、"); } } }

Map

HashMap

public V put(K key,V value)                                 

public V get(Object key)

public Set<K>keySet()                                    将Map中的所有key以Set集合的方式返回

public Set<Map.Entry<K,V>>entrySet()          将Map集合变成Set集合

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class HashMapDemo04 { public static void main(String[] args) { Map<String, Integer> map = new HashMap<String, Integer>(); map.put("zhangsan", 1); map.put("zhangsan", 2); map.put("lisi", 3); map.put("wangwu", 5); Set<String> set = map.keySet(); // 返回全部的key Iterator<String> iter = set.iterator(); while (iter.hasNext()) { String key = iter.next(); System.out.println(key + " --> " + map.get(key)); } } }

使用Iterator输出Map集合

1)通过 entrySet()将Map集合变成Set集合

2)利用iterator()方法取得Iterator的实例化对象

3)迭代找到每一个Map.Entry对象

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class IteartorMapDemo { public static void main(String[] args) { Map<String, Integer> map = new HashMap<String, Integer>(); map.put("zhangsan", 1); map.put("zhangsan", 2); map.put("lisi", 3); map.put("wangwu", 5); Set<Map.Entry<String, Integer>> allSet = null; allSet = map.entrySet(); Iterator<Map.Entry<String, Integer>> iter = allSet.iterator(); while (iter.hasNext()) { Map.Entry<String, Integer> me = iter.next(); System.out.println(me.getKey() + " --> " + me.getValue()); } } }

HashTable已被淘汰,HashMap采用异步,非线程安全,且允许将key或value设置为null。

Map注重查找,而Collection注重输出

最后

以上就是坦率皮带最近收集整理的关于java类集框架总结的全部内容,更多相关java类集框架总结内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部