1.HashMap
定义方式 :HashMap<索引对象类型,对象类型>名称=new HashMap<索引对象名称,对象类型>();
例如 HashMap<Integer,String>mapp = new HashMap<Integer,String>();
尖括号内的值前一个为键,后一个为值,前一个可以看做后一个的下标,尖括号里的类型只能是对象,不能为基本的类型。
常用的操作:设value为类型的变量(数据类型或者对象),容器的名称设为a; index为相应索引
将数据加入到容器的操作:a.put(index,value);
得到容器内的东西:a.get(index);
查看容器内的所有内容: System.out.println(容器的名字);
删除容器的内容:a.remove(index,value);
使得容器的内容成为一个集合: a.keySet();
获得容器内容的数目:a.keySet().size();
判断当前索引对应的值是否在容器中:a.containsKey(index);import java.util.HashMap;
import java.util.Scanner;
public class Hashmap {
private HashMap<Integer,String>coinnames = new HashMap<Integer,String>(); //定义hash表
public void getCoin(Integer key,String value) {
coinnames.put(key,value); //第一个表示键,后一个为值,都只能为对象,不能为基本的类型
}
public String getName(Integer key) {
if(coinnames.containsKey(key)) { //查找这个键对应的值是否存在
return coinnames.get(key); //根据键返回对应的值
}
else return "Not Found";
}
//遍历整个Hash表,keySet的作用是将值变成一个集合,然后根据对应的键输出
public void nameList() {
for(Integer key : coinnames.keySet()) {
String s = getName(key);
System.out.println(s);
}
}
public int nameAmount() {
return coinnames.keySet().size();//返回值的个数
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner in = new Scanner(System.in);
Hashmap map = new Hashmap();
String s = in.next();
if(s.equals("#")) return ;
Integer key = in.nextInt();
do{
map.getCoin(key,s);
s = in.next();
if(!s.equals("#")) key = in.nextInt();
}while(!s.equals("#"));
map.nameList();
System.out.println("容器内数量为"+map.nameAmount());
}
}
2.ArrayList
定义方式: ArrayList<对象的类型>名称=new ArrayList<对象的类型>();
常用的操作:设value为类型的变量(数据类型或者对象),容器的名称设为a; index为相应索引
将数据加入到容器的操作:a.add(value)或者a.add(int,value);//第一种按顺序第二种按索引加入;
得到容器内的东西:a.get(index);
查看容器内的所有内容: System.out.println(容器的名字);
删除容器的内容:a.remove(index);
将容器的内容移到对应的数组:a.toArray(数组的引用名字);
得到容器内存存储的数目: a.size();
import java.util.ArrayList;
import java.util.Scanner;
public class Arraylist {
private ArrayList<String>al = new ArrayList<String>(); //定义容器
public void putArray(int index,String value) {
al.add(index,value); //对应序号的位置放入值
}
public void putArray(String value) {
al.add(value); //按次序放入
}
public String getArray(int index) {
return al.get(index); //得到该索引下位置的值
}
public void dele(int index) {
al.remove(index);
}
//函数返回整个容器的内容
public String[] getList() {
String[] notes = new String[al.size()];
al.toArray(notes);//将容器的内容转化为对应的数组
return notes;
}
// 直接用容器的名字以[,,,]的形式输出容器内的所有值
public void list() {
System.out.println(al);
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner in = new Scanner(System.in);
Arraylist al = new Arraylist();
int a ;
String[] s;
for(int i=0;i<5;i++) {
a = in.nextInt();
al.putArray(i,""+a);
}
s=al.getList();
al.dele(0);
for(String k : s) {
System.out.println(k);
}
al.list();
}
}
3.HashSet
定义方式:HashSet<对象的类型>名称=new HashSet<对象的类型>();
HashSet与ArrayList不同,HashSet是真正的集合,容器内是乱的,没有顺序,且里面的元素不相同。
常用的操作:设value为类型的变量(数据类型或者对象),容器的名称设为a; index为相应索引
将数据加入到容器的操作:a.add(value)
查看容器内的所有内容: System.out.println(容器的名字);
删除容器的内容:a.remove(value);
将容器的内容移到对应的数组:a.toArray(数组的引用名字);
得到容器内存存储的数目: a.size();
import java.util.HashSet;
import java.util.Scanner;
public class Hashset {
private HashSet<String>hs =new HashSet<String>();
public void putHash(String value) {
hs.add(value);
}
public void list() {
System.out.println(hs);
}
public void dele(String value) {
hs.remove(value);
}
public String[] getList() {
String[] s = new String[hs.size()];
hs.toArray(s);
return s;
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
Scanner in = new Scanner(System.in);
Hashset hs = new Hashset();
String s;
String[] st;
do{
s = in.next();
hs.putHash(s);
}while(!s.equals("#"));
st = hs.getList();
for(String k : st) {
System.out.println(k);
}
hs.dele("#");
hs.list();
}
}