接口 Map<K,V>
将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
put
将指定的值与此映射中的指定键关联(可选操作)。如果此映射以前包含一个该键的映射关系,则用指定值替换旧值
1.参数:
key - 与指定值关联的键
value - 与指定键关联的值
public static void main(String[] args) {
Map<String, String> map=new HashMap<>();
map.put("zs", "1");
map.put("ls", "2");
map.put("ww", "3");
map.put("ww", "4");
System.out.println(map);
}
输出的结果为:
2.返回:
以前与 key 关联的值,如果没有针对 key 的映射关系,则返回 null。
以前与 key 关联的值,如果针对 key 的映射关系的话,则返回上一个映射关系对应的值。
public static void main(String[] args) {
Map<String, String> map=new HashMap<>();
map.put("zs", "1");
map.put("ls", "2");
map.put("ww", "3");
map.put("ww", "4");
String put=map.put("zs", "12");
System.out.println(put);
System.out.println(map);
}
输出的结果为:
keySet
返回此映射中包含的键的 Set 视图。该 set 受映射支持,所以对映射的更改可在此 set 中反映出来,反之亦然。如果对该 set 进行迭代的同时修改了映射(通过迭代器自己的 remove 操作除外),则迭代结果是不确定的。set 支持元素移除,通过 Iterator.remove、 Set.remove、 removeAll、 retainAll 和 clear 操作可从映射中移除相应的映射关系。它不支持 add 或 addAll 操作。
Set<String> keySet = map.keySet();
for (String key : keySet) {
System.out.println(key+":"+map.get(key));
}
输出的结果为:
entrySet
返回此映射中包含的映射关系的 Set 视图。该 set 受映射支持,所以对映射的更改可在此 set 中反映出来,反之亦然。如果对该 set 进行迭代的同时修改了映射(通过迭代器自己的 remove 操作,或者通过对迭代器返回的映射项执行 setValue 操作除外),则迭代结果是不确定的。set 支持元素移除,通过 Iterator.remove、 Set.remove、 removeAll、 retainAll 和 clear 操作可从映射中移除相应的映射关系。它不支持 add 或 addAll 操作。
Set<Entry<String, String>> entrySet = map.entrySet();
for (Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
输出的结果为:
TreeMap
TreeMap:底层是二叉树数据结构,线程不同步,可以用于给Map集合中的键进行排序
二叉树排序图:
从左由下,往右往上
public static void main(String[] args) {
TreeSet<Integer> ts=new TreeSet<>();
ts.add(18);
ts.add(21);
ts.add(23);
ts.add(14);
ts.add(26);
Iterator<Integer> iterator = ts.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
输出的结果是从小到大排序:
集合框架Map应用
应用一:
1.将学生作为键,地址作为值进行存储,名字年龄相同则被认定为一个人,最后输出
public class TreeMap {
public static void main(String[] args) {
HashMap<Student, String> hm=new HashMap<>();
hm.put(new Student("咪咪", 17), "shanghai");
hm.put(new Student("明明", 19), "guangdong");
hm.put(new Student("西西", 16), "shenzhen");
hm.put(new Student("拉拉", 17), "beijing");
hm.put(new Student("咪咪", 17), "hangzhou");
System.out.println(hm.size());
}
}
class Student{
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.getName().hashCode()+this.age;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if(obj instanceof Student) {
Student stu=(Student)obj;
return this.getName().equals(stu.getName()) &&
this.getAge()==stu.getAge();
}
return false;
}
}
输出的结果为:
2.最后按照年龄排序
实现Comparable接口,重写compareTo方法
public static void main(String[] args) {
TreeMap<Student, String> hm=new TreeMap<>();
hm.put(new Student("咪咪", 17), "shanghai");
hm.put(new Student("明明", 19), "guangdong");
hm.put(new Student("西西", 16), "shenzhen");
hm.put(new Student("拉拉", 17), "beijing");
hm.put(new Student("咪咪", 17), "hangzhou");
Set<Entry<Student, String>> entrySet = hm.entrySet();
for (Entry<Student, String> entry : entrySet) {
System.out.println(entry.getKey()+"----->"+entry.getValue());
}
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
int sum=this.getAge()-o.getAge();
if(sum==0) {
return this.getName().compareTo(o.getName());
}
return sum;
}
输出结果为:
3.需求改变、按姓名进行排序
class stuDemo implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
int sum=o1.getName().compareTo(o2.getName());
if(sum==0) {
return o1.getAge()-o2.getAge();
}
return sum;
}
}
输出的结果为:
应用2:
统计字符串中字符出现次数
分析:
字符是唯一的,可以将其作为map集合的key,次数就是map集合中的value。
将指定的字符串装到一个容器中进行筛选,将字符串转化成一个字符数组。
当字符第一次出现时,意味着map集合找不到对应的value值,给它赋值为一,
当字符第二次出现时,意味着map集合有对应的value值,则为它对应值加一。
public class HashMapDemo {
public static void main(String[] args) {
String s="wertyuiolkjhghjklgdfjhkmvxcncmzdklsjfsja";
getRepeatTimes(s);//统计次数
}
//统计次数的方法
private static void getRepeatTimes(String s) {
// TODO Auto-generated method stub
//把字符串转换为char数组(字符串本质就是char[])
char[] charArray = s.toCharArray();
//key:存储字符名称 value:存储字符出现的次数
// Map<Character, Integer> map=new HashMap<>();
//使用TreeMap,可以使排序按照自然顺序排列
Map<Character, Integer> map=new TreeMap<>();
for (char c : charArray) {
//当字符第一次出现时,将其次数value赋值为一
if(map.get(c)==null) {
map.put(c, 1);
}else {
map.put(c, map.get(c)+1);
}
}
StringBuffer sb=new StringBuffer();
Set<Entry<Character, Integer>> entrySet = map.entrySet();
for (Entry<Character, Integer> entry : entrySet) {
System.out.print(entry.getKey()+"("+entry.getValue()+")");
}
System.out.println(sb.toString());
}
}
输出的结果为:
集合框架工具类(Collections、Arrays)
Collections将后台打印出来的值进行倒序排序
使用前:
输出的结果为:
使用后:
输出的结果为:
使用Arrays时,定义一个数组时,不用遍历也能将值输出
String[] s=new String[] {"迷鹿","女","17"};
System.out.println(Arrays.toString(s));
输出的结果为: