Fork me on GitHub

Java类集框架

单对象保存父接口:Collection

定义

1
public interface Collection<E> extends Iterator<E>

核心方法

  • public boolean add(E e) 普通方法 向集合里面保存数据
  • public boolean addAll(Collection<? extends E> c) 普通方法 追加一个集合
  • public void clear() 普通方法 清空集合,跟元素为null
  • public boolean contains(Object o) 普通方法 判断是否含有指定内容,需要equals支持
  • public boolean isEmpty() 普通方法 判断是否是空集合
  • public boolean remove(Object o) 普通方法 删除对象,需要equals支持
  • public int size() 普通方法 取得集合中保存元素的个数
  • public Object[] toArray() 普通方法 将集合变为对象数组
  • public Inerator iterator() 普通方法 为Iterator接口实例化

注意:contains()和remove()方法操作是,需要确保已经覆写了Object类中的equals()方法

Clooection的两个子接口:

  • List(允许数据重复)
  • Set(数据不允许重复)

List接口

List接口的方法扩充

  • public E get(int index) 普通方法 取得索引编号的内容
  • public E set(int index,E element) 普通方法 修改指定索引的内容
  • public ListIterator listIterator() 普通方法 为ListIterator接口实例化

注意:在使用List接口时,可以使用ArrayListVector两个子接口来进行接口对象的实例化

ArrayList

List基本操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.ArrayList;
import java.util.List;
public class testDemo{
public static void main(String args[]){
List<String> all=new ArrayList();
System.out.println("长度:"+all.size()+",是否为空:"+all.isEmpty());
all.add(xxy);
all.add(xt);
System.out.println("长度:"+all.size()+",是否为空:"+all.isEmpty());
for(int x=0;x<all.size();x++){
String str=all.get(x);
System.out.println(str);
}
}
}

通过ArrayList子类实例化List接口对象,这样就可以使用List接口中定义的方法,List接口相对于Collection接口扩充了get()方法,所以可以直接获取保存在集合的数据。
ArrayList是List接口的子类,同时也是Collection的子类,所以,可以直接用ArrayList实例化Collection接口。
直接使用Collection接口对象将不具备get()方法,只有将全部集合利用toArray()方法转化为对象数组后才可以输出。

Vector

JDK1.2之后Vector实现了一个List接口,与ArrayList的最大区别就是Vector类中的部分方法使用了Synchronized关键字声明

Set接口

Set接口有两个常用的子类:

  • HashSet:散列存放数据
  • TreeSet:有序存放数据(使用TreeSet子类必须同时使用比较器的概念)

HashSet子类的操作

1
2
3
4
5
6
7
8
9
10
11
package Test1;
import java.util.HashSet;
import java.util.Set;
public class testDemo6 {
public static void main(String args[]) throws Exception{
Set<String> all=new HashSet<String>();
all.add("xxy");
all.add("xt");
System.out.println(all);
}
}

TreeSet子类的操作

1
2
3
4
5
6
7
8
9
10
11
package Test1;
import java.util.Set;
import java.util.TreeSet;
public class testDemo7 {
public static void main(String args[]) {
Set<String> all=new TreeSet<String>();
all.add("xxy");
all.add("xt");
System.out.println(all);
}
}

使用TreeSet子类会默认实现排序功能,但是如果保存的是任意类的对象,则必须实现java.lang.Comparable接口才可以实现排序。

利用TreeSet保存自定义对象,并实现排序功能

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
package Test1;
import java.util.Set;
import java.util.TreeSet;
class Computer implements Comparable<Computer>{
private int price;
private String name;
public Computer(String name,int price) {
this.price=price;
this.name=name;
}
@Override
public String toString() {
return "电脑的名字:"+this.name+",电脑的价格:"+this.price+"\n";
}
@Override
public int compareTo(Computer c) {
if(this.price>c.price) {
return 1;
}else if(this.price<c.price) {
return -1;
}else {
return this.name.compareTo(c.name);
}
}
}
public class testDemo8 {
public static void main(String args[]) {
Set<Computer> all=new TreeSet<Computer>();
all.add(new Computer("lenove",3999));
all.add(new Computer("apple",19999));
all.add(new Computer("xiaomi",4999));
System.out.println(all);
}
}

可以看出,TreeSet数据的排序和消除重复元素都是依靠Compara接口

利用HashSet保存自定义对象,并实现消除重复元素功能
HashSet如果要消除重复元素,则需要依靠Object类中提供的两个方法:

  • 取得哈希码:public int hashCode()
  • 对象比较:public boolean equals(Object obj)
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
package Test1;
import java.util.Set;
import java.util.HashSet;
class Glasses{
private String material;
private int price;
public Glasses(String material,int price) {
this.material=material;
this.price=price;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((material == null) ? 0 : material.hashCode());
result = prime * result + price;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Glasses other = (Glasses) obj;
if (material == null) {
if (other.material != null)
return false;
} else if (!material.equals(other.material))
return false;
if (price != other.price)
return false;
return true;
}
@Override
public String toString() {
return "眼镜的材料:"+this.material+",眼镜的价格:"+this.price+"\n";
}
}
public class testDemo9 {
public static void main(String args[]) {
Set<Glasses> all=new HashSet<Glasses>();
all.add(new Glasses("塑料",399));
all.add(new Glasses("金属",299));
all.add(new Glasses("塑料",399));
System.out.println(all);
}
}

集合的输出

4种形式:

  • Iterator
  • ListIterator
  • foreach
  • Enumeration

迭代输出:Iterator

Collection接口中直接为Iterator接口实例化的方法iterator(),任何集合都可以转换为Iterator接口输出

Iterator接口定义的方法

  • public boolean hasNext() 普通方法 判断是否还有内容
  • public E next() 普通方法 取出当前内容

使用Iterator输出集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package Test1;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
public class testDemo10 {
public static void main(String args[]) {
List<String> all=new ArrayList<String>();
all.add("xxy");
all.add("xt");
all.add("zju");
Iterator<String> iter=all.iterator();
while(iter.hasNext()) {
String str=iter.next();
System.out.println(str);
}
}
}

双向迭代输出:ListIterator

ListIterator属于Iterator的子接口

ListIterator接口定义的方法

  • public boolean hasPrevious() 普通方法 判断是否有前一个元素
  • public E previous() 普通方法 取出前一个元素
  • public void add(E e) 普通方法 向集合中追加元素
  • public void set(E e) 普通方法 修改集合数据

同时可以继续使用hasNext()方法和next()方法

foreach输出

Enumeration输出

接口定义:
如下

1
2
3
4
public interface Enumeration<E>{
public boolean hasMoreElements(); // 判断是否有下一个元素 相当于 hasNext()
public E nextElement(); //取出当前元素
}

利用Enumeration接口输出数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package Test1;
import java.util.Enumeration;
import java.util.Vector;
public class testDemo11 {
public static void main(String args[]) {
Vector<String> all=new Vector<String>();
all.add("zju");
all.add("xxy");
Enumeration<String> enu=all.elements();
while(enu.hasMoreElements()) {
String str=enu.nextElement();
System.out.println(str);
}
}
}

如果要利用集合类为Enumeration实例化,必须使用Vector子类完成

偶对象保存:Map接口

Map常用方法

  • public V put(K key,V value) 普通方法 向集合中保存数据
  • public v get(Object key) 普通方法 根据key值查找Value
  • public Set<Map.Entry<K,V>> entrySet() 普通方法 将Map集合转化为Set集合
  • public Set keySet() 普通方法 取出全部的key

Map接口中常用的两个子类:

  • HashTble
  • HashMap

HashMap子类的使用

1
2
3
4
5
6
7
8
9
10
11
12
package Test1;
import java.util.Map;
import java.util.HashMap;
public class testDemo12 {
public static void main(String args[]) {
Map<String,Integer> map=new HashMap<String,Integer>();
map.put("壹",1);
map.put("空",null);
map.put(null,0);
System.out.println(map);
}
}

可以看出:

  • HashMap定义的Map集合是无序存放的
  • 前面如果与重复的key,会使用新的内容替换旧的内容
  • HashMap保存数据时,key或value可以保存为null

Hashtable子类的使用
Hashtable保存的key或value值不能设置为null

1
2
3
4
5
6
7
8
9
10
package Test1;
import java.util.Map;
import java.util.Hashtable;
public class testDemo13 {
public static void main(String args[]) {
Map<String,Integer> map=new Hashtable<String,Integer>();
map.put("壹",1);
System.out.println(map.get("壹"));
}
}

利用Iterator输出Map集合

集合的输出要利用Iterator接口
Collection接口可以直接实例化Iterator接口,但是Map接口并没有直接实例化的方法。
Map保存的对象是将key和value自动包装成一个Map.Entry接口对象
Map.Entry常用方法:

  • public K getKey() 取得数据中的key
  • public VgetValue() 取得对象中的value
  • public V setValue(V value) 修改数据中的value

虽然Map接口没有直接实例化Iterator的方法,但是有将Map接口数据转化为Set接口的方法:public Set<Map.Entry<K,V>> entrySet(),
然后可以利用Set接口实例化Iterator接口

利用Iterator输出Map集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package Test1;
import java.util.Map;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
public class testDemo14 {
public static void main(String args[]) {
Map<String,Integer> map=new Hashtable<String,Integer>();
map.put("壹",1);
Set<Map.Entry<String, Integer>> set=map.entrySet();
Iterator<Map.Entry<String, Integer>> iter=set.iterator();
while(iter.hasNext()) {
Map.Entry<String, Integer> me=iter.next();
System.out.println(me.getKey()+"="+me.getValue());
}
}
}

Collections工具类

常用操作

  • public static boolean addAll(Collection<? super T> c,T elements) 实现集合数据追加
  • public static int binarySearch(List<? extends Comparable<? supoer T>> list, T key) 二分法查找集合数据
  • public static void copy(List<?super T> dest,List<? extends T> src) 集合复制
  • public stativ void reverse(List<?> list) 集合翻转

追加数据

1
2
3
4
5
6
7
8
9
10
11
12
13
package Test1;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
public class testDemo15 {
public static void main(String args[]) {
List<String> all=new ArrayList<String>();
Collections.addAll(all, "xxy","zju","zjg","yq");
System.out.println(all);
Collections.reverse(all);
System.out.println(all);
}
}

-------------本文结束感谢您的阅读-------------
Donate comment here