Java的LinkedHashSetMap接口可变参数集合嵌套
Posted 孙崇武
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java的LinkedHashSetMap接口可变参数集合嵌套相关的知识,希望对你有一定的参考价值。
1、LinkedHashSet:
(1)LinkedHashSet集合保证元素的存入和取出的顺序;
package com.oracle.demo01; import java.util.HashSet; import java.util.LinkedHashSet; public class demo01 { public static void main(String[] args) { //不能存重复元素,但是LinkedHashSet是有序的。 LinkedHashSet<String> set = new LinkedHashSet<String>(); set.add("c"); set.add("b"); set.add("z"); for(String s:set){ System.out.println(s); } } }
效果如下:
(2)LinkedHashSet其实是HashSet的子类;
2、Map接口:
(1)元素是成对存在,每个元素由键与值两部分组成,通过键可以找对所对应的值,键值对;
(2)Map中的集合不能包含重复的键,值可以重复;每个键只能对应一个值;
(3)遍历:迭代器:不能直接遍历map,遍历set集合
第一种方法:keyset();第二种方法:entrySet();
用增强for或者是迭代器,不能用普通for循环,因为没有下标概念;
增强for(){}
package com.oracle.demo01; import java.util.HashMap; import java.util.Map; import java.util.Set; public class demo02 { public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("1", "a"); map.put("2","b"); map.put("2", "z"); map.put("3", "c"); //移除 map.remove("3"); //取数据 System.out.println(map.get("1")); System.out.println(map.get("2")); System.out.println(map.get("3")); //1、keySet遍历 //先获取map中的key所对应的set集合 Set<String> set = map.keySet(); //然后遍历set找到每一个key值 for(String s:set){ String key = s; String value = map.get(key); System.out.println(key+".."+value); } //简写 for(String s:map.keySet()){ System.out.println(s+".."+map.get(s)); } //2、entryset()遍历 Set<Map.Entry<String, String>> s1 = map.entrySet(); for(Map.Entry<String, String> e:s1){ System.out.println(e.getKey()+"..."+e.getValue()); } //简写 for(Map.Entry<String, String> e:map.entrySet()){ System.out.println(e.getKey()+"..."+e.getValue()); } } }
迭代器
package com.oracle.demo01; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; public class demo03 { public static void main(String[] args) { Map<String,String> map = new HashMap<String,String>(); map.put("邓超", "孙俪"); map.put("李晨", "范冰冰"); map.put("文章", "马伊琍"); map.put("小沈阳", "沈春阳"); //迭代器:不能直接遍历map,遍历set集合; //1、keyset() Set<String> s1 = map.keySet(); Iterator<String> it = s1.iterator(); while(it.hasNext()){ String key = it.next(); String value = map.get(key); System.out.println(key+">>"+value); } //2、entrySet() Set<Map.Entry<String, String>> s2 = map.entrySet(); Iterator<Map.Entry<String, String>> it2 = s2.iterator(); while(it2.hasNext()){ //获取每一个"结婚证对象" Map.Entry<String, String> e =it2.next(); String key = e.getKey(); String value = e.getValue(); System.out.println(key+".."+value); } } }
(4)HashMap存储自定义类型键值;保证对象唯一,必须复写对象的hashCode和equals方法;
建立一个 人 类
package com.oracle.demo02; public class person { private String name; private Integer age; public person() { super(); } public person(String name, Integer age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((age == null) ? 0 : age.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; person other = (person) obj; if (age == null) { if (other.age != null) return false; } else if (!age.equals(other.age)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } public String toString() { return "姓名为" + name + ", 年龄为" + age ; } }
建立测试类
package com.oracle.demo02; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; public class demo01 { public static void main(String[] args) {
//通过方法对比看出 键不能重复
method01();
method02();
method03(); } public static void method01(){ Map<String, person> map = new HashMap<String, person>(); map.put("java0601", new person("a",21)); map.put("java61", new person("b",21)); map.put("java0806", new person("a",21)); map.put("java86", new person("b",21)); for(String s:map.keySet()){ System.out.println(s+"..."+map.get(s)); } } public static void method02(){ Map<person,String> map = new HashMap<person,String>(); map.put(new person("a",21),"java0601"); map.put(new person("b",21),"java61"); map.put(new person("a",21),"java0806"); map.put(new person("b",21),"java86"); for(person p:map.keySet()){ System.out.println(p+"..."+map.get(p)); } } public static void method03(){ LinkedHashMap<person,String> map = new LinkedHashMap<person,String>(); map.put(new person("a",21),"java0601"); map.put(new person("b",22),"java61"); map.put(new person("a",23),"java0806"); map.put(new person("b",24),"java86"); for(person p:map.keySet()){ System.out.println(p+"..."+map.get(p)); } } }
效果如下:
3、可变参数:
(1)修饰符 返回值类型 方法名(参数类型... 形参名){ };
package com.oracle.demo02; public class demo02 { public static void main(String[] args) { System.out.println(add(1)); System.out.println(add(1,1)); System.out.println(add(1,1,1)); } //想要做一个求和的方法,但是参数个数不一定,但是类型都是一定的Int public static int add(int...a){ //int[] b = a;相当于一个数组 int sum = 0; for(int i = 0;i < a.length;i++){ sum +=a[i]; } return sum; } }
效果如下:
(2)可变参数一定要写在参数列表的末尾位置;
(3)调用的不确定性,只能传一个可变参数;
4、集合嵌套:
用循环嵌套取值;
举个例子:
package com.oracle.demo02; import java.util.HashMap; import java.util.Set; public class demo03 { public static void main(String[] args) { //HashMap<String, HashMap<String,String>> method01(); } public static void method01(){ HashMap<String, HashMap<String,String>> map = new HashMap<String, HashMap<String,String>>(); HashMap<String,String> m1 = new HashMap<String,String>(); m1.put("0601", "温昭"); m1.put("0806", "马荣"); HashMap<String,String> m2 = new HashMap<String,String>(); m2.put("0101", "张三"); m2.put("0202", "李四"); map.put("oracle01", m1); map.put("oracle02", m2); //第一层循环:获取学校 Set<String> s1= map.keySet(); for(String s2:s1){ //学校名称 String schoolname = s2; //获取每一个班级 HashMap<String,String> s3 = map.get(s2); Set<String> s4 = s3.keySet(); for(String s5:s4){ //班级名称 String classname = s5; //学生名称: String studentname = s3.get(s5); System.out.println(schoolname+".."+classname+".."+studentname); } } } }
效果如下:
可以简写为:
//简写 for(String s:map.keySet()){ for(String s1:map.get(s).keySet()){ System.out.println("学校为"+s+",班级为"+s1 +"姓名为"+map.get(s).get(s1)); } }
以上是关于Java的LinkedHashSetMap接口可变参数集合嵌套的主要内容,如果未能解决你的问题,请参考以下文章
Java基础学习191221(可变参数,数组,Array类,稀疏数组,继承重写多态,抽象类,接口)
Java——Map接口,可变参数,collections(集合实现类)