集合-HashMap

1)
Map list = new HashMap(); list.put("Isaac", "王远航"); list.put("Jay", "周杰伦"); list.put("Akon", "阿肯"); //Object obj=list.values(); System.out.println("输入你要找的姓名"); String name = input.next(); //Object obj=null; boolean flag=false; String name1=null; for(Object obj:list.values()) { if(obj.equals(name)) { flag=true; name1=(String)obj; break; } } if(flag==true) { System.out.println(name1); }else { System.out.println("没找到"); }

1:list.values()集合也可以通过加强for依次将值赋给Object对象

2)ste keySet()的返回值为set类型  是无序唯一的

collection values()返回值类型为collection类型   是无序不唯一的

3)
public
class Iterator { public static void main(String[] args) { Map list = new HashMap(); list.put("Isaac", "王远航"); list.put("Jay", "周杰伦"); list.put("Akon", "阿肯"); //方法1:依次取出键值对 //Set name2 = list.keySet();可以先拿到key的集合 然后用name2去依次赋值遍历 for(Object obj:list.keySet()) { String name1=(String)obj; System.out.println(name1+" "+list.get(name1)); //因为list.get(name1)的返回值类型为collect类型所以可以强成String类型 } //方法2使用Iterator迭代器 Iterator itt = list.iterator(); while(itt.hasNext()) { String names = (String)itt.next(); String value = (String)list.get(names); System.out.println(names+" "+value); } //方法3先拿到Map中的键值对,然后在每对键值对中分别取出键和值 Set ms = list.entrySet(); for(Object obj : ms) { Map.Entry me =(Map.Entry)obj; Object key = me.getKey(); Object value = me.getValue(); System.out.println((String)key+(String)value); } } }

3:三种方式获得 每个键值对 的对应键和值

4)
public
static void main(String[] args) { Ldemo2 title1 = new Ldemo2("张三",'男'); Ldemo2 title2 = new Ldemo2("李四",'男'); Ldemo2 title3 = new Ldemo2("王五",'女'); //Ldemo2 title = new Ldemo2("",""); Map<String, Ldemo2> list = new HashMap<String, Ldemo2>(); list.put("Isaac",title1); list.put("Jay",title2); list.put("Akon",title3); for(String obj:list.keySet()) { //String name1=(String)obj; System.out.println(obj+" "+list.get(obj).getNames()); }

4:list.get(obj).getNames()可以这样写 得到name的值

5)
Set<Map.Entry<String, String>> ms=list.entrySet(); for(Map.Entry<String, String> obk:ms) { System.out.print(obk.getKey()+""+obk.getValue()); }

5:使用泛型获得键值对

6)
public
class TextStudent { public static void main(String[] args) { Scanner input = new Scanner(System.in); Student title1 = new Student("黎明", '男'); Student title2 = new Student("周杰伦", '男'); Student title3 = new Student("碧昂斯", '女'); Student title4 = new Student("艾薇儿", '女'); Map<String, Student> list = new HashMap<String, Student>(); //也可以直接这么写list.put("1", new Student("艾薇儿", '女')); list.put("1", title1); list.put("2", title2); list.put("3", title3); list.put("4", title4); System.out.println("请输入你要找的学员编号"); String number = input.next(); /*boolean flag = false; // String search; for (String search : list.keySet()) { if (number.equals(search)) { flag = true; break; } } if (flag==true) { Student title = list.get(number); System.out.println("找到学员:" + title.getNames() + " " + title.getSex()); } else { System.out.println("没找到相关学员"); }*/ if(list.containsKey(number)) {//没用泛型 Student stu = (Student)list.get(number); System.out.println("找到学员:" + stu.getNames() + " " + stu.getSex()); }else { System.out.println("没有您要找的学员"); } } }

6:通过键寻找值

posted @ 2019-04-06 00:44  围攻柏林  阅读(105)  评论(0编辑  收藏  举报