Java映射和集合集合和映射
列表(List)
集(Set)
映射(Map)
其他的API
集合:若干用途、性质相同或相近的“数据”组合成一个整体
集合类型从体系上归纳为:
列表(List)
列表
区分元素的顺序,且允许包含重复元素
集(Set)
集
不区分元素的顺序,不允许包含重复元素
映射(Map)
映射中保存成对的“键-值”(Key-Value)信息,映射中不能包含重复的键,每个键最多只能映射一个值
Collection接口
描述Set和List集合类型的跟接口
TreeSet接口
自然排序
Comparable接口
ArrayList
Vector
Stack
Iterator
HashSet
HashMap
HashTable
Eg:
public class Person implements 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.Collection;
import java.util.HashMap;
import java.util.Set;
public class TestHashMap {
public static void main(String[] args) {
HashMap h = new HashMap();
h.put(new Integer(1003), new Person(1003,"zhangsan",21));
h.put(1021, new Person(1021,"wangliu",22));
h.put(1013, new Person(1013,"wangliu",25));
h.put(1010, new Person(1010,"wangliu",23));
h.put(1001, new Person(1001,"wangliu",25));
h.put(1002, new Person(1002,"wangliu",29));
h.put(1005, new Person(1005,"wangliu",27));
h.put(1004, new Person(1004,"wangliu",21));
h.put(1009, new Person(1009,"wangliu",22));
System.out.println("检查单个元素");
Person p = (Person) h.get(1009);
Person p1 = (Person) h.get(1000);
System.out.println(p);
System.out.println(p1);
System.out.println("遍历所有的键");
Set names = h.keySet();
for (Object o : names) {
System.out.println(o);
}
System.out.println("遍历所有的值");
Collection values = h.values();
for (Object o : values) {
System.out.println(o);
}
}
}
结果:
检查单个元素
Id: 1009 Name: wangliu Age: 22
null
遍历所有的键
1001
1003
1002
1005
1004
1021
1009
1010
1013
遍历所有的值
Id: 1001 Name: wangliu Age: 25
Id: 1003 Name: zhangsan Age: 21
Id: 1002 Name: wangliu Age: 29
Id: 1005 Name: wangliu Age: 27
Id: 1004 Name: wangliu Age: 21
Id: 1021 Name: wangliu Age: 22
Id: 1009 Name: wangliu Age: 22
Id: 1010 Name: wangliu Age: 23
Id: 1013 Name:wangliu Age: 25
public class Person implements 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.Collection;
import java.util.Hashtable;
import java.util.Set;
public class TestHashTable {
public static void main(String[] args) {
Hashtable h1 = new Hashtable();
h1.put(new Integer(1003), new Person(1003,"zhangsan",21));
h1.put(1021, new Person(1021,"wangliu",22));
h1.put(1013, new Person(1013,"wangliu",25));
h1.put(1010, new Person(1010,"wangliu",23));
h1.put(1001, new Person(1001,"wangliu",25));
h1.put(1002, new Person(1002,"wangliu",29));
h1.put(1005, new Person(1005,"wangliu",27));
h1.put(1004, new Person(1004,"wangliu",21));
h1.put(1009, new Person(1009,"wangliu",22));
System.out.println("检查单个元素");
Person p = (Person) h1.get(1009);
Person p1 = (Person) h1.get(1000);
System.out.println(p);
System.out.println(p1);
System.out.println("遍历所有的键");
Set names = h1.keySet();
for (Object o : names) {
System.out.println(o);
}
System.out.println("遍历所有的值");
Collection values = h1.values();
for (Object o : values) {
System.out.println(o);
}
}
}
结果:
检查单个元素
Id: 1009 Name: wangliu Age: 22
null
遍历所有的键
1010
1009
1005
1004
1003
1002
1001
1021
1013
遍历所有的值
Id: 1010 Name: wangliu Age: 23
Id: 1009 Name: wangliu Age: 22
Id: 1005 Name: wangliu Age: 27
Id: 1004 Name: wangliu Age: 21
Id: 1003 Name: zhangsan Age: 21
Id: 1002 Name: wangliu Age: 29
Id: 1001 Name: wangliu Age: 25
Id: 1021 Name: wangliu Age: 22
Id: 1013 Name: wangliu Age: 25
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class TestArray {
public static void main(String[] args) {
Integer [] a = {21,12,24,8};
// System.out.println(a);
System.out.println(Arrays.toString(a));
Arrays.sort(a);
System.out.println(Arrays.toString(a));
int b = Arrays.binarySearch(a, 12);//指定的二进制搜索
System.out.println(b);
List al = Arrays.asList(31,20,18);
System.out.println(al);
al.set(1, 7);
System.out.println(al);
System.out.println(Arrays.toString(a));
}
}
结果:[21, 12, 24, 8]
[8, 12, 21, 24]
1
[31, 20, 18]
[31, 7, 18]
[8, 12, 21, 24]