JavaSE学习 第十一章 集合与映射

程序人生系列之JavaSE学习 集合与映射

1.集合框架

三个主要内容: Set   List   Map

关系结构:


2. Collection 接口

Set 和 List 的父接口,专门定义了一些公共的方法

3. Set 和 List 接口:两者对父接口 Collection 进行了不同的扩展

Set 是集合,不能包含相同的元素

List 是列表,可以有相同的元素

4. ArrayList :长度可变的数组

示例代码:

import java.util.Date;

import java.util.ArrayList;

public class TestArrayList{

public static void main(String[] args) {

ArrayList h = new ArrayList();

h.add("1st");

h.add("2nd");

h.add(new Integer(3));

h.add(new Double(4.0));

h.add("2nd");      // 重复元素, 加入

h.add(new Integer(3)); // 重复元素,加入

System.out.println(h);

System.out.println("size=" + h.size());

System.out.println("-----替换指定元素-----");

h.set(3,"replace");

System.out.println("-----for循环遍历-----");

for(int i=0;i<h.size();i++){

System.out.println(h.get(i));

}

System.out.println("-----取用特定元素-----");

Integer it = (Integer)h.get(2);

System.out.println(it.intValue());

System.out.println("-----插入元素-----");

h.add(3,new Date());

System.out.println("-----转换为数组-----");

Object[] os = h.toArray();

for(Object o : os){

System.out.println(o);

}

}

}

测试结果:
图片

5.Vector  : 注意它和ArrayList 的区别

示例代码:

import java.util.Date;

import java.util.Vector;

public class TestVector{

public static void main(String[] args) {

Vector v = new Vector();

v.add("1st");

v.add("2nd");

v.add(new Integer(3));

v.add(new Double(4.0));

v.add("2nd");      // 重复元素, 加入

v.add(new Integer(3)); // 重复元素,加入

System.out.println(v);

System.out.println("size=" + v.size());

System.out.println("-----替换指定元素-----");

v.set(3,"replace");

System.out.println("-----for循环遍历-----");

for(int i=0;i<v.size();i++){

System.out.println(v.get(i));

}

System.out.println("-----取用特定元素-----");

Integer it = (Integer)v.get(2);

System.out.println(it.intValue());

System.out.println("-----插入元素-----");

v.add(3,new Date());

System.out.println("-----转换为数组-----");

Object[] os = v.toArray();

for(Object o : os){

System.out.println(o);

}

}

}

测试结果:

图片

6. Stack :栈,很重要的一种数据结构,它继承了 Vector 类

示例代码:

import java.util.Date;

import java.util.Stack;

public class TestStack{

public static void main(String[] args) {

Stack s = new Stack();

s.push("hello");

s.push(new Date());

s.push(400);  //自动封装,等价于s.push(new Integer(400));

s.push(3.14);

System.out.println("弹栈前:size=" + s.size());

System.out.println(s.pop());

System.out.println("弹栈后:size=" + s.size());

System.out.println(s.peek());

System.out.println("peek操作后:size=" + s.size());

while(!s.isEmpty())

System.out.println(s.pop());

}

}

测试结果:
图片

7. Iterator 接口:迭代器,很重要,对于实现了 List接口的类提供了一个统一的遍历方式

示例代码:

import java.util.Date;

import java.util.ArrayList;

import java.util.Vector;

import java.util.Iterator;

public class TestIterator{

public static void main(String[] args) {

ArrayList a = new ArrayList();

a.add("China");

a.add("USA");

a.add("Korea");

Iterator  it = a.iterator();

while(it.hasNext()){

String country = (String)it.next();

System.out.println(country);

}

Vector v = new Vector();

v.addElement(new Date());

v.addElement(new Date(200008755554L));

it = v.iterator();

while(it.hasNext()){

Date time = (Date)it.next();

System.out.println(time);

}

}

}

测试结果:

图片

8.TreeSet :可以实现排序功能的集合,默认是字典序

   HashSet

图片

示例代码:

import java.util.TreeSet;

import java.util.Iterator;

public class TestTreeSet{

public static void main(String[] args) {

TreeSet ts = new TreeSet();

ts.add("orange");

ts.add("banana");

ts.add("apple");

ts.add("grape");

Iterator it = ts.iterator();

while(it.hasNext()){

String fruit = (String)it.next();

System.out.println(fruit);

}

}

}

测试结果:

图片

示例代码:

import java.util.Date;

import java.util.HashSet;

import java.util.Iterator;

public class TestHashSet{

public static void main(String[] args) {

HashSet h = new HashSet();

h.add("1st");

h.add("2nd");

h.add(new Integer(3));

h.add(new Double(4.0));

h.add("2nd");       // 重复元素, 未被加入

h.add(new Integer(3)); // 重复元素,未被加入

h.add(new Date());

System.out.println("开始: size=" + h.size());

Iterator it = h.iterator();

while(it.hasNext()){

Object o = it.next();

System.out.println(o);

}

h.remove("2nd");

System.out.println("移除元素后: size=" + h.size());

System.out.println(h);

}

}

测试结果:

图片

9.Comparable 接口:重写compareTo方法可以定制比较逻辑

示例代码:

  ----------------------------------->   Person.java

public class Person implements java.lang.Comparable{

private final int id;

private String name;

private int age;

public Person(int id,String name,int age){

this.id = id;

this.name = name;

this.age = age;

}

public int getId(){

return id;

}

public void setName(String name){

this.name = name;

}

public String getName(){

return name;

}

public void setAge(int age){

this.age = age;

}

public int getAge(){

return age;

}

public String toString(){

return "Id: " + id + "\tName: " + name + "\tAge: " + age;

}

@Override

public int compareTo(Object o){

Person p = (Person)o;

return this.id - p.id;

}

@Override

public boolean equals(Object o){

boolean flag = false;

if(o instanceof Person){

if(this.id == ((Person)o).id)

flag = true;

}

return false;

}

}

------------------------------------------> 测试类

import java.util.TreeSet;

import java.util.Iterator;

public class TestComparable{

public static void main(String[] args) {

TreeSet ts = new TreeSet();

ts.add(new Person(1003,"张三",15));

ts.add(new Person(1008,"李四",25));

ts.add(new Person(1015,"王五",73));

ts.add(new Person(1001,"赵六",49));

Iterator it = ts.iterator();

while(it.hasNext()){

Person employee = (Person)it.next();

System.out.println(employee);

}

}

}

图片

10.Map 接口:以键值对的形式保存数据

11.HashMap:基于哈希表的映射集合结构

----------------->person.java

public class Person implements java.lang.Comparable{

private final int id;

private String name;

private int age;

public Person(int id,String name,int age){

this.id = id;

this.name = name;

this.age = age;

}

public int getId(){

return id;

}

public void setName(String name){

this.name = name;

}

public String getName(){

return name;

}

public void setAge(int age){

this.age = age;

}

public int getAge(){

return age;

}

public String toString(){

return "Id: " + id + "\tName: " + name + "\tAge: " + age;

}

@Override

public int compareTo(Object o){

Person p = (Person)o;

return this.id - p.id;

}

@Override

public boolean equals(Object o){

boolean flag = false;

if(o instanceof Person){

if(this.id == ((Person)o).id)

flag = true;

}

return false;

}

}

-------------------------------------> 测试类

import java.util.Set;

import java.util.HashMap;

import java.util.Collection;

import java.util.Iterator;

public class TestHashMap{

public static void main(String[] args) {

HashMap hm = new HashMap();

hm.put(new Integer(1003),new Person(1003,"张三",15));

hm.put(new Integer(1008),new Person(1008,"李四",25));

hm.put(1015,new Person(1015,"王五",73));   //自动封装

hm.put(1001,new Person(1001,"赵六",49));

System.out.println("----检索单个元素----");

Person p = (Person)hm.get(1008);

System.out.println(p);

System.out.println("----遍历所有\"键\"(元素名)----");

Set names = hm.keySet();

for(Object o : names){

System.out.println(o);

}

System.out.println("----遍历所有\"值\"(元素值)----");

Collection values = hm.values();

for(Object o : values){

System.out.println(o);

}

}

}

图片

HashTable 与 HashMap 基本上相同的,但是也有一些区别

线程安全的一般都是用于多线程的环境中

12. Arrays 类:数组类,里面有很多的静态方法可供数组直接调用

示例代码:

import java.util.List;

import java.util.Arrays;

public class TestArrays{

public static void main(String[] args) {

Integer[] a = {3,25,12,79,48};

System.out.println(a);

System.out.println(Arrays.toString(a));

Arrays.sort(a);

System.out.println(Arrays.toString(a));

int idx = Arrays.binarySearch(a,25);

System.out.println(idx);

List list = Arrays.asList(3,4,5);

System.out.println(list);

list.set(2,66);

System.out.println(Arrays.toString(a));

}

}

测试结果:

图片

13. Collections 类:里面定义了很多的对集合进行操作的方法

示例代码:

import java.util.Vector;

import java.util.ArrayList;

import java.util.Collections;

import java.util.Enumeration;

public class TestCollections{

public static void main(String[] args) {

ArrayList alist = new ArrayList();

alist.add(75);

alist.add(38);

alist.add(21);

alist.add(4);

alist.add(12);

System.out.println("原列表: " + alist);

Collections.sort(alist);

System.out.println("排序后: " + alist);

Collections.reverse(alist);

System.out.println("逆序后: " + alist);

Collections.shuffle(alist);

System.out.println("混排后: " + alist);

Collections.rotate(alist,2);

System.out.println("移位后: " + alist);

ArrayList blist = new ArrayList();

blist.add(55);

blist.add(66);

System.out.println("新列表: " + blist);

Collections.copy(alist,blist);

System.out.println("拷贝后: " + alist);

System.out.println("列表中出现元素66的次数: " + Collections.frequency(alist,66));

System.out.println("列表中元素的最大值: " + Collections.max(alist));

System.out.println("列表中元素的最小值: " + Collections.min(alist));

Enumeration en = createDemoEnumeration();

ArrayList clist = Collections.list(en);

System.out.println("Enumeration->ArrayList: " + alist);

}

public static Enumeration createDemoEnumeration(){

Vector v = new Vector();

v.add("Tom");

v.add("Billy");

v.add("Jenny");

return v.elements();

}

}

测试结果:

图片

你可能感兴趣的:(JavaSE)