一、Map接口
1、Map概述
(1)public interface Map
将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射一个值。
格式如下:
Map<Key,Value>接口
将键映射到值得对象,一个映射不能包含重复的键。每一个键最多只能映射一个值。
(2)Map集合的分类:
2、 Map集合的共性方法
(添加):
put(K key,V value)
将指定的值与此映射中的指定键相关联。add是返回的Boolean值。Put添加元素若出现添加相同键,后添加的会覆盖原来的值,然后put方法返回的是原来的值。
void putAll(Map t)
从指定映射中将所有映射关系复制到此映射中。
(删除):
void clear()
从此映射中移除所有映射关系。
void remove(Object key)
如果存在此键的映射关系,则将其从映射中移除。
(判断):
boolean containsValue(Object value)
如果此映射为指定值映射一个或多个键,则返回 true。
boolean containsKey(Object key)
如果此映射包含指定键的映射关系,则返回 true。
boolean isEmpty()
如果此映射未包含键-值映射关系,则返回 true。
(获取):
get(Object key)
返回此映射中映射到指定键的值。如果此映射中没有该键的映射关系,则返回 null。
Collection values()
返回此映射中包含的值的 collection 视图。Collection是单列集合,Map为双列集合。
import java.util.*;
class Demo
{
public static void main(String[] args)
{
Map<String,String>map=new HashMap<String,String>();
Sop("/***********************添加***************************/");
map.put("01","zhangsan");
map.put("02","lisi");
map.put("03","wangwu");
Sop(map);
Collection<String>coll=map.values();
Sop(coll);
Sop("/***********************判断***************************/");
Sop(map.containsKey("01"));
Sop(map.containsValue("zhangsan"));
Sop(map.isEmpty());
Sop("/***********************删除***************************/");
Sop(map.remove("01"));
Sop(map);
Sop("/***********************获取***************************/");
Sop(map.get("02"));
map.put(null,"空值");
Sop(map.get(null));
}
public static void Sop(Object obj)
{
System.out.println(obj);
}
}
3、Map的取出方法
- 3.1 方法一:
Set keySet()
返回此映射中包含的键的 set 视图。因为Set具备迭代器,所以可用迭代方法取出所有的键,再根据get方法获取每一个键对应的值。
import java.util.*;
class Demo
{
public static void main(String[] args)
{
Map<String,String>map=new HashMap<String,String>();
map.put("01","zhangsan");
map.put("02","shenjing");
map.put("03","haha");
map.put("04","lisi");
Set<String>keySet=map.keySet();
Iterator<String>it=keySet.iterator();
while (it.hasNext())
{
String key=it.next();
String value=map.get(key);
System.out.println(key+">>>"+value);
}
}
}
- 3.2 方法二:
Set entrySet()
返回此映射中包含的映射关系的 set 视图。返回的 set 中的每个元素都是一个 Map.Entry。
public static interface Map.Entry
映射项(键-值对)。Map.entrySet 方法返回映射的 collection 视图,其中的元素属于此类。获得映射项引用的惟一 方法是通过此 collection 视图的迭代器来实现。
K getKey()
返回与此项对应的键。
V getValue()
返回与此项对应的值。
import java.util.*;
class Demo
{
public static void main(String[] args)
{
Map<String,String>map=new HashMap<String,String>();
map.put("01","zhangsan");
map.put("02","shenjing");
map.put("03","haha");
map.put("04","lisi");
Set<Map.Entry<String,String>>entrySet=map.entrySet();
Iterator<Map.Entry<String,String>>it=entrySet.iterator();
while (it.hasNext())
{
Map.Entry<String,String>me=it.next();
String key=me.getKey();
String value=me.getValue();
System.out.println(key+"....."+value);
}
}
}
- 3.3 练习
(1)
/*
需求:将学生作为对象,将其按照名字和年龄排序,使用Map存储和取出。
*/
import java.util.*;
class Student implements Comparable<Student>
{
private String name;
private int age;
Student(String name,int age)
{
this.name=name;
this.age=age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()//使用它后面才可以直接打印System.out.println(stu+"~~~~"+addr);
{
return name+"····"+age;
}
public int hashCode()
{
return name.hashCode()+age*39;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Student))
throw new ClassCastException("类型转换异常");
Student s=(Student)obj;
return this.name.equals(s.name)&&this.age==s.age;
}
public int compareTo(Student s)
{
int num=new Integer(s.age).compareTo(new Integer(this.age));
if(num==0)
return s.name.compareTo(this.name);
return num;
}
}
class HashMapDemo
{
public static void main(String[] args)
{
HashMap<Student,String> hm=new HashMap<Student,String>();
hm.put(new Student("Lisi7",10),"Beijing");
hm.put(new Student("Lisi5",14),"Hangzhou");
hm.put(new Student("Lisi9",14),"Shanghai");
hm.put(new Student("Lisi2",13),"Nanjing");
/****************************************************///方法一:
Set<Student> keySet=hm.keySet();
Iterator<Student> it=keySet.iterator();
while(it.hasNext())
{
Student stu=it.next();
String addr=hm.get(stu);
System.out.println(stu+"······"+addr);
}
/***************************************************///方法二:
Set<Map.Entry<Student,String>>entrySet=hm.entrySet();
Iterator<Map.Entry<Student,String>>it=entrySet.iterator();
while(it.hasNext())
{
Map.Entry<Student,String>me=it.next();
Student stu=me.getKey();
String addr=me.getValue();
System.out.println(stu+"·····"+addr);
/***************************************************/
}
}
}
(2)
/*****************************************************************************
字母出现的次数,例如“asddgdfdghhcvgr”希望打印结果是a(1)s(1)
思路:
1、将字符串转换为字符数组,要对每个字母操作
2、使用treeMap集合进行排序
3.遍历数组
4、将map集合中的数据变成指定字符串形式返回
******************************************************************************/
import java.util.*;
class TreeMapDemo
{
public static void main(String[] args)
{
String s=charCount("adhfudfddsfjdjf");
System.out.println(s);
}
public static String charCount(String str)
{
char[] chs=str.toCharArray();
TreeMap<Character,Integer>tm=new TreeMap<Character,Integer>();
for(int i=0;i<str.length();i++)
{
Integer value=tm.get(chs[i]);
if(value==null)
{
tm.put(chs[i],1);
}
else
{
value=value+1;
tm.put(chs[i],value);
}
}
StringBuilder sb=new StringBuilder();
Set<Map.Entry<Character,Integer>>entrySet=tm.entrySet();
Iterator <Map.Entry<Character,Integer>> it=entrySet.iterator();
while(it.hasNext())
{
Map.Entry<Character,Integer>me=it.next();
Character ch=me.getKey();
Integer ve=me.getValue();
sb.append(ch+"("+ve+")");
}
return sb.toString();
}
}
二、Collections接口
1、Collections方法
- 1.1 一般方法
public static void sort(List list)
*根据指定比较器产生的顺序对指定列表进行排序。
public static T max(Collection coll)
根据元素的自然顺序,返回给定 collection 的最大元素。collection 中的所有元素都必须实现 Comparable 接口。此外,collection 中的所有元素都必须是可相互比较的(也就是说,对于 collection 中的任何 e1 和 e2 元素,e1.compareTo(e2) 不得抛出 ClassCastException)。
public static void fill(List list,T obj)
使用指定元素替换指定列表中的所有元素。
public static boolean replaceAll(Listlist, T oldVal,T newVal)
使用某一值替换列表中出现的所有某一指定值
public static void reverse(List list)
反转指定列表中元素的顺序。
import java.util.*;
class CollectionsDemo8
{
public static void main(String[] args)
{
List<String>list=new ArrayList<String>();
list.add("absdf");
list.add("fgfg");
list.add("sxxg");
list.add("ggf");
System.out.println(list);
/**************排序*********************/
Collections.sort(list);
System.out.println("sort后:"+list);
/**************取最值*********************/
String max=Collections.max(list);
System.out.println(max);
/**************二分查找*********************/
int index=Collections.binarySearch(list,"fgfg");
System.out.println("fgfg位置:"+index);
/***************替换*****************************/
Collections.replaceAll(list,"fgfg","哈哈");
System.out.println("replaceAll后"+list);
/***************反转*************************/
Collections.reverse(list);
System.out.println("reverse后数组:"+list);
/***************添加*************************/
Collections.fill(list,"pp");
System.out.println("fill后数组:"+list);
}
}
public static void sort(List list, Comparator c)
根据指定比较器产生的顺序对指定列表进行排序。此列表内的所有元素都必须是使用指定比较器可相互比较的(也就是说,对于列表中的任何 e1 和 e2 元素,c.compare(e1, e2) 不得抛出 ClassCastException)。
public static T max(Collection coll,Comparator comp)
根据指定比较器产生的顺序,返回给定 collection 的最大元素。collection 中的所有元素都必须是通过指定比较器可相互比较的(也就是说,对于 collection 中的任何 e1 和 e2 元素,comp.compare(e1, e2) 不得抛出 ClassCastException)。
/*
需求:自定义比较方法,按照字符串的长度进行排序。
*/
import java.util.*;
class CollectionDemo
{
public static void main(String[] args)
{
List <String> list=new ArrayList<String>();
list.add("abcd");
list.add("bcd");
list.add("qq");
list.add("kkk");
Collections.sort(list,new StrLenComparator());
System.out.println(list);
String max=Collections.max(list,new StrLenStringDemo());
System.out.println(max);
}
}
class StrLenComparator implements Comparator<String>
{
public int compare(String s1,String s2)
{
if(s1.length()>s2.length())
return 1;
else if(s1.length()<s2.length())
return -1;
return s1.compareTo(s2);
}
}
- 1.2 查找方式
public static int binarySearch(List list,T key)
使用二进制搜索算法来搜索指定列表,以获得指定对象。
/**********************************************************************
需求:(1)使用binarySearch方法查找指定的元素
(2)使用自定义的查找方式进行指定元素的查找
(3)使用自定义的查找方式进行元素的查找,但是需要按照字符串长度进行排序
**********************************************************************/
import java.util.*;
class StrLenStringDemo implements Comparator<String>
{
public int compare(String s1,String s2)
{
if(s1.length()>s2.length())
return 1;
else if(s1.length()<s2.length())
return -1;
return s1.compareTo(s2);
}
}
class CollectionDemo
{
public static void main(String[] args)
{
List <String> list=new ArrayList<String>();
list.add("abcd");
list.add("bcd");
list.add("qq");
list.add("kkk");
Sop("原数组:"+list);
Collections.sort(list);//要排列有序的序列
Sop("sort后数组:"+list);
int index1=Collections.binarySearch(list,"abcd");
Sop("abcd位置:"+index1);
int index2=halfSearch1(list,"kkk");//查找
Sop("kkk位置:"+index2);
Collections.sort(list,new StrLenStringDemo());
Sop("按照长度排序后数组:"+list);
int index3=halfSearch2(list,"bcd",new StrLenStringDemo());
Sop("bcd位置:"+index3);
}
public static void Sop(Object obj)
{
System.out.println(obj);
}
public static int halfSearch1(List<String>list,String key)
{
int min=0;
int max=list.size()-1;
while(min<=max)
{
int mid=(min+max)>>1;
String str=list.get(mid);
int num=str.compareTo(key);
if(num>0)
max=mid-1;
else if(num<0)
min=mid+1;
else
return mid;
}
return -1;
}
public static int halfSearch2(List<String>list,String key,Comparator<String>cmp)
{
int min=0;
int max=list.size()-1;
while(min<=max)
{
int mid=(min+max)>>1;
String str=list.get(mid);
int num=cmp.compare(str,key);
if(num>0)
max=mid-1;
else if(num<0)
min=mid+1;
else
return mid;
}
return -1;
}
}
1.3