一、集合概述
- java中集合有3大类:List、Set、Map
- List接口:有序的集合,能通过索引访问列表中的元素,类似数组。
- 实现List接口的常用类:LinkedList,ArrayList,Vector和Stack
- Set接口:不包含重复元素的集合,即set中的任何e1.equals(e2)=false。set中最多有一个null。
- :key-value映射的键值对。
- 实现Map的常用类:HashTable、HashMap
- List接口:有序的集合,能通过索引访问列表中的元素,类似数组。
Map接口
- Map接口常用方法:
- put(key,value);//向map中加入元素
- contiansKey(Object key);//key在map中是否存在
- contiansValue(Object value);//value在map中是否存在
-
Set
视图。 - get(Object key) 返回指定键所映射的值,若对于该键值来说此映射不包含任何映射,则返回NULL
3. HashMap和HashTable的区别<span style="font-size:14px;">import java.util.*; public class HashMapDemo { public static void main(String[] args){ //注册用户 HashMap users = new HashMap(); users.put("luqh1",new User("luqh1","1")); // users.put("luqh2",new User("luqh2","2")); users.put("luqh3",new User("luqh3","3")); users.put("luqh3","zyf"); Set keySet = users.keySet(); System.out.println(keySet); User user1 = new User("luqh1","1"); System.out.println("users.containsValue("+"zyf"+"):"+users.containsValue("zyf")); //登录查找 Scanner s = new Scanner(System.in); while(true){ System.out.print("姓名:"); String name = s.nextLine(); System.out.print("密码:"); String pwd = s.nextLine(); if(!users.containsKey(name)){ System.out.println("用户不存在"); continue; } User user = (User)users.get(name);//通过name获取到value if(user.pwd.equals(pwd)){ System.out.println(name+"登录成功"); continue; } } } } class User { String name; String pwd; public User(String name,String pwd){ this.name=name; this.pwd = pwd; } public String toString(){ return name+":"+pwd; } }</span>
输出结果:
[luqh1, luqh3, luqh2]users.containsValue(zyf):true姓名:luqh1密码:1luqh1登录成功
- HashMap HashTable
非线程安全 线程安全
快 稍微慢点
- 举例子说明HashMap的非线程安全(网上实例)
<span style="font-size:14px;">public class HashMapThread {
public static final HashMap<String,String> firstHashMap = new HashMap<String, String>();
public static void main(String[] args) throws Exception {
//线程1
Thread t1 = new Thread(){
public void run(){
for(int i=0;i<25;i++){
firstHashMap.put(String.valueOf(i), String.valueOf(i));
}
}
};
Thread t2 = new Thread(){
public void run(){
for(int j=25;j<50;j++){
firstHashMap.put(String.valueOf(j), String.valueOf(j));
}
}
};
t1.start();
t2.start();
Thread.currentThread().sleep(1000);//注线程休息1秒,以便于t1,t2链各个线程填充firstHashMap完成
//如果key和value不同,说明两个线程在同步的时候HashMap出现了异常
for(int l = 0;l<50;l++){
if(!String.valueOf(l).equals(firstHashMap.get(String.valueOf(l)))){
System.out.println(String.valueOf(l)+":"+firstHashMap.get(String.valueOf(l)));
}
}
}
}</span>
自己验证之后的结果是:
2:null
5:null
7:null
9:null
Set接口:无序不重复
- 实现类有hashSet
- 常用方法:
- 实例
<span style="font-size:14px;">package MAP;
import java.util.HashSet;
import java.util.Set;
public class HashSetDemo {
public static void main(String[] args) {
Set<Node> foods = new HashSet<Node>();
foods.add(new Node(3,4));
foods.add(new Node(5,6));
foods.add(new Node(6,7));
foods.add(new Node(8,9));
System.out.println(foods.size());
System.out.println(foods);//必须有toStrig方法,才成输出自己想要的[3,4]结果
//吃一颗豆子
foods.remove(new Node(8,9));
System.out.println(foods);//必须有toStrig方法,才成输出自己想要的[3,4]结果
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(foods.contains(new Node(i,j))){
System.out.println("0");
}else
System.out.println("");
}
}
}
}
class Node{
private int i;
private int j;
public Node(int i,int j){
this.i=i;
this.j=j;
}
//必须写equals方法
/*原因:
集合中的contains其实也是用equals来比较的;
默认equals在比较两个对象时,是看他们是否指向同一个地址的,
但有时,我们希望两个对象只要是某些属性相同就认为他们的quals为true。比如:
Node n1 = new Node(3,4);
Node n2 = new Node(3,4);
如果不重写equals的话,他们是不相同的,所以我们要重写equals,判断只要他们的id和名字相同equals就为true
这也正是foods.contains(new Node(3,4)为true的原因。
*/
public boolean equals(Object obj){
if(obj==null){
return false;
}
if(this==obj){
return true;
}
if(obj instanceof Node){
Node o = (Node)obj;
return i==o.i && j ==o.j;
}
return false;
}
//上面讲述了为什么要重新equals,一般重写了equals,也也重写hashCode()
//hashCode的常规规定:两个相同的对象,hashCode也一样;
//当 对象.eauals(新对象)=true时,hashCode重写之后也会一样;
public int hashCode(){
return (i<<16)|j;
}
//必须有toStrig方法,System.out.println(foods);才成输出自己想要的[3,4]结果
public String toString(){
return "["+i+","+j+"]";
}
}</span>
List接口
- 常用子类:ArrayList 、Vector,常用的是ArrayList
- 实例:常用API方法举例:
- list的增加、删除常用方法
<span style="font-size:14px;">public class ArrayListDemo {
public static void main(String[] args) {
//List<String> list = new ArrayList<String>();
//指定操作的泛型为String类型
List<String> list = null;
list = new ArrayList<String>();
list.add("how are you!");
list.add(0, "luqh");
list.add(1,"hello");
System.out.println("删除之前"+list);
//根据索引删除
list.remove(0);
//根据内容删除
list.remove("hello");
System.out.println("删除之后"+list);
}
}
//输出结果:删除之前[luqh, hello, how are you!]
删除之后[how are you!]</span>
4. List的输出
<span style="font-size:14px;">public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("how old are you!");
list.add(0, "luqh");
list.add(1,"hello");
//正序输出
for(int i = 0;i<list.size();i++){
System.out.print(list.get(i)+"、");
}
System.out.println("");
//逆序输出
for(int k=list.size()-1;k >= 0;k--){
System.out.print(list.get(k)+"、");
}
System.out.println("");
//按适当顺序(从第一个到最后一个元素)返回包含此列表中所有元素的数组;返回数组的运行时类型是指定数组的运行时类型
String[] str = list.toArray(new String[]{});
for(int j= 0;j<str.length;j++){
System.out.print(str[j]+"、");
}
}</span>
输出结果
luqh、hello、how old are you!、
how old are you!、hello、luqh、
luqh、hello、how old are you!、