目录
1、泛型
1、泛型的定义格式:
1.1 泛型的好处:
2、泛型类:
3、泛型方法:
4、泛型接口:
5、类型通配符:
6、可变参数:
7、可变参数的使用:
2、Map集合
1、Map集合的概述和特点
2、Map集合的基本功能
3、Map集合的获取功能
4、Map集合的遍历
4.1遍历方法一
4.2遍历方法二
5、HashMap集合案例
5.1案例一
5.2案例二
6、集合嵌套
6.1集合嵌套之ArrayList嵌套HashMap
6.2集合嵌套之HashMap嵌套ArrayList
7、统计字符串每个字符出现的次数
8、Collections的概述和使用
9、用ArrayList集合存储学生并遍历
//泛型类
//泛型类 public class Generic<T>{ private T t; public T getT(){ return t; } public void setT(T t){ this.t =t; } }
//测试类
//测试类 public class work11{ public static void main(String[] args){ Student s = new Student(); Generic<String> g = new Generic<>(); g.setT("中国"); System.out.println(g.getT()); //中国 System.out.println("----------"); Generic<Integer> g1 = new Generic<>(); g1.setT(100); System.out.println(g1.getT()); //100 System.out.println("----------"); Generic<Boolean> g2 = new Generic<>(); g2.setT(true); System.out.println(g2.getT()); //true } }
//Generic类中定义泛型方法
//Generic类 public class Generic{ public <T> void show(T t){ System.out.println(t); } }
//测试类
public class work11{ public static void main(String[] args){ Generic g = new Generic(); g.show("中国"); //中国 g.show(100); //100 g.show(true); //true g.show(12.34); //12.34 } }
//接口
public interface Generic<T>{ void show(T t); }
//实现接口的类
public class GenericImpl<T> implements Generic<T>{ @Override public void show(T t) { System.out.println(t); } }
//测试类
public class work11{ public static void main(String[] args){ Generic<String> g = new GenericImpl<>(); g.show("中国"); //中国 Generic<Integer> g2 = new GenericImpl<>(); g2.show(100); //100 Generic<Boolean> g3 = new GenericImpl<>(); g3.show(true); //true } }
import java.util.*; public class work11{ public static void main(String[] args){ List<?> list1 = new ArrayList<Object>(); List<?> list2 = new ArrayList<Number>(); List<?> list3 = new ArrayList<Integer>(); System.out.println("-----------"); //List<? extends Number> list4 = new ArrayList<Object>(); 会报错 List<? extends Number> list4 = new ArrayList<Number>(); List<? extends Number> list5 = new ArrayList<Integer>(); System.out.println("-----------"); //List<? super Number> list6 = new ArrayList<Integer>(); 会报错 List<? super Number> list6 = new ArrayList<Object>(); List<? super Number> list7 = new ArrayList<Number>(); } }
public class work11{ public static void main(String[] args){ System.out.println(sum(10,20)); //30 System.out.println(sum(10,20,30)); //60 System.out.println(sum(10,20,30,40)); //100 System.out.println(sum(10,20,30,40,50)); //150 System.out.println(sum(10,20,30,40,50,60)); //210 } /* public static int sum(int b,int...a){ int sum = b; for(int i:a){ sum += i; } return sum; } */ public static int sum(int...a){ int sum = 0; for(int i:a){ sum += i; } return sum; } }
import java.util.*; public class work11{ public static void main(String[] args){ List<String> list1 = Arrays.asList("hello","world","java"); // list1.add("javaee"); //会报错 // list1.remove("hello"); //会报错 list1.set(1,"javaee"); System.out.println(list1); //[hello, javaee, java] System.out.println("------------"); List<String> list2 = List.of("hello","world","java"); // list1.add("javaee"); //会报错 // list1.remove("hello"); //会报错 // list1.set(1,"javaee"); //会报错 System.out.println(list2); //[hello, world, java] System.out.println("------------"); Set<String> set = Set.of("hello","world","java"); // Set<String> set = Set.of("hello","world","java","hello"); //会报错 // list1.add("javaee"); //会报错 // list1.remove("hello"); //会报错 // list1.set(1,"javaee"); //会报错 System.out.println(set); //[world, java, hello] } }
import java.util.*; public class work11{ public static void main(String[] args){ Map<Integer,String> map = new HashMap<>(); //put方法,键不存在时为添加,如果键存在时为修改 map.put(1,"风清扬"); map.put(2,"天山果佬"); map.put(3,"橙留香"); map.put(3,"菠萝吹雪"); System.out.println(map); //{1=风清扬, 2=天山果佬, 3=菠萝吹雪} } }
import java.util.*; public class work11{ public static void main(String[] args){ //创建Map对象 Map<Integer,String> map = new HashMap<>(); //put方法,键值不存在时为添加,如果键值存在时为修改 map.put(1,"风清扬"); map.put(2,"天山果佬"); map.put(3,"菠萝吹雪"); System.out.println(map); //{1=风清扬, 2=天山果佬, 3=菠萝吹雪} System.out.println(map.remove(1)); //风清扬 System.out.println(map.remove(1)); //null System.out.println(map); //{2=天山果佬, 3=菠萝吹雪} System.out.println(map.containsKey(2)); //true System.out.println(map.containsValue("菠萝吹雪")); //true System.out.println(map.size()); //2 map.clear(); //移除Map集合中的所有键值对 System.out.println(map.isEmpty()); //true } }
import java.util.*; public class work11{ public static void main(String[] args){ //创建Map对象 Map<Integer,String> map = new HashMap<>(); //put方法,键值不存在时为添加,如果键值存在时为修改 map.put(1,"风清扬"); map.put(2,"天山果佬"); map.put(3,"菠萝吹雪"); System.out.println(map.get(1)); //风清扬 System.out.println(map.get(4)); //null Set<Integer> set = map.keySet(); for(Integer i:set){ System.out.println(i); //1 2 3 } Collection<String> c = map.values(); for(String s:c){ System.out.println(s); //风清扬 天山果佬 菠萝吹雪 } } }
import java.util.*; public class work11{ public static void main(String[] args){ //创建Map对象 Map<Integer,String> map = new HashMap<>(); //put方法,键值不存在时为添加,如果键值存在时为修改 map.put(1,"风清扬"); map.put(2,"天山果佬"); map.put(3,"菠萝吹雪"); //获取键的集合 Set<Integer> set = map.keySet(); //用加强for循环遍历 for(Integer i:set){ //根据键获取相应的值 String s = map.get(i); System.out.println(i+","+s); } } }
import java.util.*; public class work11{ public static void main(String[] args){ //创建Map对象 Map<Integer,String> map = new HashMap<>(); //put方法,键值不存在时为添加,如果键值存在时为修改 map.put(1,"风清扬"); map.put(2,"天山果佬"); map.put(3,"菠萝吹雪"); //获取键值对的集合 Set<Map.Entry<Integer,String>> entrySet = map.entrySet(); //用加强for循环遍历 for(Map.Entry<Integer,String> me:entrySet){ int i = me.getKey(); String s = me.getValue(); System.out.println(i+","+s); } } }
//测试类 import java.util.*; public class work11{ public static void main(String[] args){ //创建HashMap集合 HashMap<String,Student> hm = new HashMap<>(); //创建学生对象 Student s1 = new Student("风清扬",30); Student s2 = new Student("天山果佬",35); Student s3 = new Student("菠萝吹雪",33); hm.put("itheima001",s1); hm.put("itheima002",s2); hm.put("itheima003",s3); //创建所有键的集合 Set<String> set = hm.keySet(); //用加强for循环遍历 for(String s:set){ Student ss = hm.get(s); System.out.println(s+","+ss.getName()+","+ss.getAge()); } System.out.println("----------"); //创建所有键值对的集合 Set<Map.Entry<String,Student>> entrySet = hm.entrySet(); //用加强for循环遍历 for(Map.Entry<String,Student> me:entrySet){ String s = me.getKey(); Student ss = me.getValue(); System.out.println(s+","+ss.getName()+","+ss.getAge()); } } }
//学生类 public class Student{ private String name; private int age; public Student(){} public Student(String name,int age){ this.name = name; this.age = age; } public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } }
//测试类 import java.util.*; public class work11{ public static void main(String[] args){ //创建HashMap集合 HashMap<Student,String> hm = new HashMap<>(); //创建学生对象 Student s1 = new Student("风清扬",30); Student s2 = new Student("天山果佬",35); Student s3 = new Student("菠萝吹雪",33); hm.put(s1,"itheima001"); hm.put(s2,"itheima002"); hm.put(s3,"itheima003"); //创建所有键的集合 Set<Student> set = hm.keySet(); //用加强for循环遍历 for(Student s:set){ String ss = hm.get(s); System.out.println(ss+","+s.getName()+","+s.getAge()); } System.out.println("----------"); //创建所有键值对的集合 Set<Map.Entry<Student,String>> entrySet = hm.entrySet(); //用加强for循环遍历 for(Map.Entry<Student,String> me:entrySet){ Student s = me.getKey(); String ss = me.getValue(); System.out.println(ss+","+s.getName()+","+s.getAge()); } } }
//学生类 public class Student{ private String name; private int age; public Student(){} public Student(String name,int age){ this.name = name; this.age = age; } public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } }
import java.util.*; public class work11{ public static void main(String[] args){ ArrayList<HashMap<String,String>> array = new ArrayList<>(); HashMap<String,String> hm1 = new HashMap<>(); hm1.put("杨过","小龙女"); hm1.put("郭靖","黄蓉"); array.add(hm1); HashMap<String,String> hm2 = new HashMap<>(); hm2.put("孙策","大乔"); hm2.put("周瑜","小乔"); array.add(hm2); HashMap<String,String> hm3 = new HashMap<>(); hm3.put("李白","王昭君"); hm3.put("干将","莫邪"); array.add(hm3); for(HashMap<String,String> me:array){ Set<String> set = me.keySet(); for(String s:set){ String ss = me.get(s); System.out.println(ss+","+s); } System.out.println("----------"); } System.out.println("遍历的另一种方法:"); for(HashMap<String,String> me:array){ Set<Map.Entry<String,String>> entrySet = me.entrySet(); for(Map.Entry<String,String> mo:entrySet){ String s = mo.getKey(); String ss = mo.getValue(); System.out.println(s+","+ss); } System.out.println("----------"); } } }
结果:
import java.util.*; public class work { public static void main(String[] args){ HashMap<String,ArrayList<String>> map = new HashMap<>(); ArrayList<String> array1 = new ArrayList<>(); array1.add("橙留香"); array1.add("菠萝吹雪"); map.put("果宝特攻",array1); ArrayList<String> array2 = new ArrayList<>(); array2.add("杨过"); array2.add("小龙女"); map.put("神雕侠侣",array2); Set<Map.Entry<String,ArrayList<String>>> entrySet = map.entrySet(); for(Map.Entry<String,ArrayList<String>> me:entrySet){ String s = me.getKey(); ArrayList<String> list = me.getValue(); System.out.println(s); for(String ss:list){ System.out.println("\t" + ss); } } System.out.println("以下是另一种方法:"); Set<String> set = map.keySet(); for(String s:set){ ArrayList<String> list = map.get(s); System.out.println(s); for(String ss:list){ System.out.println("\t" + ss); } } } }
结果:
import java.util.*; public class work { public static void main(String[] args){ //从键盘录入一个字符串 Scanner sc = new Scanner(System.in); System.out.println("请输入字符串"); String line = sc.nextLine(); //创建HashMap集合 HashMap<Character,Integer> hm = new HashMap<>(); //如果要按键的顺序打印在控制面板上,使用TreeMap //TreeMap<Character,Integer> hm = new TreeMap<>(); //遍历字符串,得到没一个字符 for(int i=0;i<line.length();i++){ char c = line.charAt(i); //用字符键来寻找对应的值 Integer x = hm.get(c); //如果返回null,说明该字符在集合中不存在,将该字符作为键,1作为值存储 if(x == null){ hm.put(c,1); //返回的不是null,说明该字符已经存在,将该字符对应的值加1在进行存储 }else { x++; hm.put(c,x); } } //创建一个StringBuilder对象,按要求进行拼接 StringBuilder sb = new StringBuilder(); //遍历HashMap集合 Set<Character> set = hm.keySet(); for(Character y:set){ Integer i = hm.get(y); //按要求拼接 sb.append(y).append("(").append(i).append(")"); } String ss = sb.toString(); System.out.println(ss); } }
结果:
import java.util.*; public class work{ public static void main(String[] args){ ArrayList<String> arrayList = new ArrayList<>(); arrayList.add("风清扬"); arrayList.add("林青霞"); arrayList.add("橙留香"); arrayList.add("菠萝吹雪"); //sort方法排序 Collections.sort(arrayList); System.out.println(arrayList); //reverse方法倒叙 Collections.reverse(arrayList); System.out.println(arrayList); //shuffle方法随机排序 Collections.shuffle(arrayList); System.out.println(arrayList); } }
结果:
//测试类 import java.util.*; public class work{ public static void main(String[] args){ ArrayList<Student> list = new ArrayList<Student>(); list.add(new Student("林青霞",30)); list.add(new Student("风清扬",33)); list.add(new Student("橙留香",10)); Collections.sort(list, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { int num1 = o1.getAge() - o2.getAge(); int num2 = num1 == 0? o1.getName().compareTo(o2.getName()):num1; return num2; } }); for(Student s:list){ System.out.println(s.getName()+","+s.getAge()); } } }
//学生类 public class Student { private String name; private int age; public Student() { } public Student(String name,int age) { 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; } }
结果: