这里写目录标题
map集合的概述和特点
其中,一个key只能对应一个value,如果在集合中申请了不同的key,则后一个value会替换掉前一个value的值。键和值也会用=连接起来。
输出:
Map集合的基本功能
Map集合的遍历
1.获取值或者键的集合
map.get(“xxx”):可以返回键xxx对应的值
keyset:获取所有键的集合
valueset:获取所有值的集合
遍历方式1:
2.获取值和键的集合
HashMap集合练习之student类分别作为key和值:
student t1=new student("xxx",19);
student t2=new student("xxx",19);
student t3=new student("xxx",19);
HashMap<String,student> hm=new HashMap<String,student>;
hm.put("003",s1);
hm.put("002",s2);
hm.put("001",s4);
方式一:
Set<String> keyset=hm.keySet();
for(String key:keSet){
student value =hm.get(key);
sout(key,value.getname,value.getage)
}
方式二:
Set<Map.EntrySet<String,String>> entryset=hm.entrySet();
for(Map.EntrySet<String,String> key:entryset){
String key=me.getKey();
Student value =me.getValue();
sout(key,value.getname,value.getage);
}
当student作为key时:
要在student类中重写hashcode和equal方法,输出是:
否则不能实现覆盖:
Arraylist集合存储HashMap集合并遍历:
方案:
ArrayList <HashMap<String,String>> array=new ArrayList <HashMap<String,String>>();
HashMap <String,String> sc=new HashMap<String,String>;
sc.put(1);
HashMap <String,String> sc=new HashMap<String,String>;
sc.put(2);
HashMap <String,String> sc=new HashMap<String,String>;
sc.put(3);
array.add(1,2,3);
for(HashMap i:array){
Set<String> j=i.setkey();
for(String key:j){
sout(key,key.get(key));
}
}
for(HashMap i:array){
Set<Map.Entry<String,String>> j=i.EntrySet();
for(Map.Entry<String,String> entry:j){
sout(entry.getkey(),entry.)
HashMap存ArrayList:
package hashmap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class hashmap1text {
public static void main(String[] args) {
HashMap<String ,ArrayList<student>> sc=new HashMap<String ,ArrayList<student>>();
student s1=new student("张蔚原",100);
student s2=new student("张蔚原",100);
student s3=new student("张蔚原",100);
student s4=new student("张蔚原",100);
ArrayList<student> a1=new ArrayList<student>();
a1.add(s1);
a1.add(s2);
ArrayList<student> a2=new ArrayList<student>();
a2.add(s3);
a2.add(s4);
sc.put("第一个",a1);
sc.put("第二个",a2);
Set<String> keySet=sc.keySet();
for(String i:keySet){
System.out.println(i);
ArrayList<student> value=sc.get(i);
for(student j:value){
System.out.println(j.getName());
System.out.println(j.getScore());
}
}
}
}
统计字符串每个字符出现的次数
package hashmap;
import com.sun.source.tree.Tree;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class hashmap1 {
public static void main(String[] args) {
String array;
Scanner sc=new Scanner(System.in);
array=sc.next();
TreeMap<Character,Integer> hash=new TreeMap<Character, Integer>();
(将hashmap转换为treemap后按ail+enter即可格式化)
char[] a=array.toCharArray();
for(int i=0;i<array.length();i++){
char key=a[i];
Integer value =hash.get(key);
if(value==null){
hash.put(key,1);
}
else{
value++;
hash.put(key,value);//别忘了加入此元素
}
}
/* Set<Character> keySet=hash.keySet();
for(Character j:keySet){
System.out.println(j+hash.get(j));
}*/
StringBuilder sb=new StringBuilder();
Set<Character> keySet=hash.keySet();
for(Character key:keySet){
Integer value =hash.get(key);
sb.append(key).append("(").append(value).append(")");
}
String result=sb.toString();
System.out.println(result);
}
}
结果:
collection的概述和使用:
升序-逆置-随机排序: