HashSet集合存储自定义对象并遍历。
如果对象的成员变量值相同即为同一个对象
//创建集合对象 HashSetset = new HashSet<>(); //创建学生对象 Student1 s1 = new Student1("张飞",20); Student1 s2 = new Student1("马超",19); Student1 s3 = new Student1("司马懿",28); Student1 s4 = new Student1("刘备",12); Student1 s5 = new Student1("关羽",26); Student1 s6 = new Student1("张飞",20); //添加到集合中 set.add(s1); set.add(s2); set.add(s3); set.add(s4); set.add(s5); set.add(s6); //循环 for(Student1 s : set){ System.out.println(s.getName()+"----"+s.getAge()); }
TreeSet集合存储自定义对象并遍历
如果对象的成员变量值相同即为同一个对象
按照年龄进行从大到小进行排序
分析:
1、创建集合对象
2、创建学生对象
3、添加到集合中
4、选择自然排序还是比较器排序
//如果这报出黄色,说明目标类中没有实现Comparable接口 //创建集合对象 TreeSetset = new TreeSet<>(); Student2 s1 = new Student2("张飞",20); Student2 s2 = new Student2("马超",19); Student2 s3 = new Student2("司马懿",28); Student2 s4 = new Student2("刘备",12); Student2 s5 = new Student2("关羽",26); Student2 s6 = new Student2("宋江",20); //添加到集合中 set.add(s1); set.add(s2); set.add(s3); set.add(s4); set.add(s5); set.add(s6); //循环 for(Student2 s : set){ System.out.println(s.getName()+"---"+s.getAge()); }
键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低输出到控制台
1、创建学生类
2、创建集合对象TreeSet
3、创建学生对象
4、将学生对象添加到集合中
5、遍历
Scanner sc = new Scanner(System.in); //创建集合对象TreeSet TreeSetMap集合总结set = new TreeSet<>(new Comparator () { @Override public int compare(Student3 o1, Student3 o2) { //比较总分成绩 int i = o2.getSumScore() - o1.getSumScore(); //总分一样,语文成绩不一样 int i1 = i == 0 ? o2.getChinese() - o1.getChinese() : i; //总分一样,语文成绩一样 但是数学成绩不一样 int i2 = i == 0 ? o2.getMath() - o1.getMath() : i1; int i3 = i == 0 ? o2.getName().compareTo(o1.getName()) : i2; return i3; } }); //创建学生对象 for (int i = 1; i <= 5; i++) { System.out.println("请录入第" + i + "个学生的成绩信息:"); System.out.println("请输入名字:"); String name = sc.next(); System.out.println("请输入学生语文成绩:"); int chinese = sc.nextInt(); System.out.println("请输入学生数学成绩:"); int math = sc.nextInt(); System.out.println("请输入学生英语成绩:"); int english = sc.nextInt(); Student3 student3 = new Student3(); //添加信息 student3.setName(name); student3.setChinese(chinese); student3.setMath(math); student3.setEnglish(english); set.add(student3); } //循环遍历 for (Student3 s : set) { System.out.println(s.getName() + "--总分--" + s.getSumScore()); }
Map的成员方法Map接口概述
将键映射到值的对象
一个映射不能包含重复的键
每个键最多只能映射到一个值
Map接口和Collection接口的不同
Map是双列的,Collection是单列的
Map的键唯一,Collection的子体系Set是唯一的
Map集合的数据结构值针对键有效,跟值无关
Collection集合的数据结构是针对元素有效
1、添加功能
put(K key, V value) 将指定的值与该映射中的指定键相关联(可选操作)。
举例:map.put(1001,“王宇”);2、删除功能:
void clear() 从该Map中删除所有的映射(可选操作)。
V remove(Object key) 如果存在(从可选的操作),从该Map中删除一个键的映射。3、判断功能:
boolean containsKey(Object key) 如果此映射包含指定键的映射,则返回 true 。
boolean containsValue(Object value) 如果此地图将一个或多个键映射到指定的值,则返回 true 。
boolean isEmpty() 如果此地图不包含键值映射,则返回 true 。4、获取功能:
V get(Object key) 返回到指定键所映射的值,或 null如果此映射包含该键的映射。
Set keySet() 返回此Map中包含的键的Set视图。
Collection values() 返回此Map中包含的值的Collection视图
Set> entrySet() 获取Map中所有元素,元素的类组成是由一个键和一个值组成 5、长度功能:
int size() 返回此Map中键值映射的数量。
public class MapDemo1 {
public static void main(String[] args) {
//创建Map集合对象
//由于Map是一个接口,所以接口不能直接实例化,要使用一个具体的子类进行实例化
//这里借助HashMap
Map map = new HashMap<>();
//添加元素
//V put(K key, V value) 将指定的值与该映射中的指定键相关联(可选操作)。
//这里的返回值是将来再次插入同一个键的时候,被覆盖的那个值
System.out.println(map.put("黄晓明","杨颖"));
System.out.println(map.put("邓超","孙俪"));
System.out.println(map.put("小虎","冯提莫"));
System.out.println(map.put("小虎","IU"));
map.put("黄晓明","杨颖");
map.put("邓超","孙俪");
map.put("小虎","冯提莫");
map.put("小虎","IU");
// void clear() 从该Map中删除所有的映射(可选操作)
// map.clear();
// V remove(Object key) 如果存在(从可选的操作),从该Map中删除一个键的映射。
// map.remove("小虎");
System.out.println("==========================================");
System.out.println(map.remove("小虎"));
System.out.println("==========================================");
// boolean containsKey(Object key) 如果此映射包含指定键的映射,则返回 true 。
System.out.println(map.containsKey("小虎"));
//boolean containsValue(Object value) 如果此地图将一个或多个键映射到指定的值,则返回 true 。
System.out.println(map.containsValue("冯提莫"));
System.out.println("==========================================");
// boolean isEmpty() 如果此地图不包含键值映射,则返回 true 。
System.out.println(map.isEmpty());
System.out.println("==========================================");
System.out.println(map);
}
}
//创建Map集合对象
Map map = new HashMap<>();
//向集合中添加元素
map.put("黄晓明","杨颖");
map.put("邓超","孙俪");
map.put("小虎","冯提莫");
//V get(Object key) 返回到指定键所映射的值,或 null如果此映射包含该键的映射。
//根据key找值
//如果map中不包含此key,返回null
System.out.println(map.get("邓超"));
System.out.println("===============================================");
//Set keySet() 返回此Map中包含的键的Set视图。
Set set = map.keySet();
System.out.println(set);
for(String s : set){
String value = map.get(s);
System.out.println(s+"--"+value);
}
//Collection values() 返回此Map中包含的值的Collection视图
Collection values = map.values();
for(String s : values){
System.out.println(s);
}
集合遍历的方法一:根据键找值
1、通过调用keySet()方法获取Map集合中所有的key
2、结合get(key)这个方法,可以获取到每一个key对应的value
3、输出
HashMapMap集合遍历的第二种方式:map = new HashMap<>(); //向集合中添加元素 map.put("霞", "洛"); map.put("蛮王", "寒冰"); map.put("卢锡安", "塞纳"); map.put("盖伦", "卡特"); //通过调用keySet()方法获取Map集合中所有的key //set集合无序的,且唯一 Set keySet = map.keySet(); for (String s : keySet) { String values = map.get(s); System.out.println(s + "--" + values); }
一次先把所有的键值对获取到,然后在依次获取每一个键值对的key和value
Set> entrySet() 获取Map中所有元素,元素的类组成是由一个键和一个值组成
//1、创建集合对象
HashMap map = new HashMap<>();
//2、向集合中添加元素
map.put("王子1", "白雪公主");
map.put("王子2", "灰姑凉");
map.put("王子3", "美人鱼");
// Set> entrySet() 获取Map中所有元素,元素的类组成是由一个键和一个值组成
//获取所有的键值对
Set> set = map.entrySet();
for (Map.Entry k : set) {
String key = k.getKey();
String value = k.getValue();
System.out.println(key + "--" + value);
}
HashMap的键值对形式:
(1)HashMap//创建集合对象 HashMap(2)HashMapmap = new HashMap<>(); //向集合中添加元素 map.put(1001, "明旺"); map.put(1002, "王宇"); map.put(1003, "周家祥"); map.put(1003, "张保桂"); System.out.println(map); //遍历集合 System.out.println("===========方式1:根据keySet键找值==================="); Set set = map.keySet(); for (Integer i : set) { String s = map.get(i); System.out.println(i + "--" + s); } System.out.println("===========方式2:根据values找值==================="); Collection values = map.values(); for(String s : values){ System.out.println(s); } System.out.println("==========方式3:根据键值对找键和值==============="); Set > set1 = map.entrySet(); for (Map.Entry KeyValue : set1) { Integer key = KeyValue.getKey(); String value = KeyValue.getValue(); System.out.println(key + "--" + value); }
//创建集合对象 HashMap(3)HashMapmap = new HashMap<>(); //创建学生对象 Student4 s1 = new Student4("明旺", 18); Student4 s2 = new Student4("王宇", 17); Student4 s3 = new Student4("周家祥", 19); Student4 s4 = new Student4("张保桂", 19); Student4 s5 = new Student4("明旺", 18); map.put("帅气男孩", s1); map.put("亿表人财", s2); map.put("油腻大叔", s3); map.put("忧郁男孩", s4); map.put("经典正太", s5); //循环遍历 Set > set = map.entrySet(); for (Map.Entry KeyValue : set) { String key = KeyValue.getKey(); Student4 value = KeyValue.getValue(); System.out.println(key + "---" + value); }
因为put方法的底层,添加元素的标准就是根据hashCode()方法和equals()的值来判断元素是否重复
//创建集合对象 HashMaplinkedHashMapDemo 继承于HashMapmap = new HashMap<>(); //创建学生对象 Student5 s1 = new Student5("王昭君", 17); Student5 s2 = new Student5("貂蝉", 16); Student5 s3 = new Student5("杨玉环", 18); Student5 s4 = new Student5("西施", 19); Student5 s5 = new Student5("王昭君", 17); //将元素添加到集合中 map.put(s1, "1111"); map.put(s2, "2222"); map.put(s3, "3333"); map.put(s4, "4444"); map.put(s5, "5555"); //遍历 Set > entries = map.entrySet(); for (Map.Entry keyValue : entries) { Student5 key = keyValue.getKey(); String value = keyValue.getValue(); System.out.println(key + ":" + value); }
linkedHashMap:
class linkedHashMap extends HashMap implements Map{}
哈希表和链表实现的Map接口,具有可预测的迭代次序。
哈希保证的是元素的唯一,保证key是唯一
链表保证是有序的(存储和取出的顺序一致)
linkedHashMapTreeMap相关知识点:map = new linkedHashMap<>(); //向集合中添加元素 map.put("1234", "hello"); map.put("4567", "world"); map.put("3456", "java"); map.put("7890", "hadoop"); map.put("1234", "hive"); Set > set = map.entrySet(); for (Map.Entry KeyValue : set) { String key = KeyValue.getKey(); String value = KeyValue.getValue(); System.out.println(key + "---" + value); }
TreeMap:键是基于红黑树结构存储的
键:String
值:String 存储的顺序会按照键的大小存储
//1、创建集合对象 TreeMapmap = new TreeMap<>(); //创建元素并添加到集合中 map.put("hello", "你好"); map.put("world", "世界"); map.put("java", "面向对象的编程语言"); map.put("mysql", "结构化数据库"); //遍历: Set > entries = map.entrySet(); for (Map.Entry keyValue : entries) { String key = keyValue.getKey(); String value = keyValue.getValue(); System.out.println(key + ":" + value); }
TreeMap
键:Student 键的存储是红黑树结构存储,可以保证键的排序和唯一
值:String
//创建集合对象 TreeMapTreeMap的小练习:map = new TreeMap<>(new Comparator () { @Override public int compare(Student6 o1, Student6 o2) { //按照年龄从小到大的顺序排序 int i = o1.getAge() - o2.getAge(); //如果年龄一样 ,名字不一样的时候 int i1 = i == 0 ? o1.getName().compareTo(o2.getName()) : i; return i1; } }); //2、创建学生对象 Student6 s1 = new Student6("明旺", 18); Student6 s2 = new Student6("王宇", 19); Student6 s3 = new Student6("周家祥", 20); Student6 s4 = new Student6("张保桂", 17); Student6 s5 = new Student6("张保桂", 18); Student6 s6 = new Student6("明旺", 18); //3、向集合中添加元素 map.put(s1, "男枪"); map.put(s2, "女警"); map.put(s3, "卡特"); map.put(s4, "霞"); map.put(s5, "男枪"); map.put(s6, "石头人"); //4、遍历 Set > entries = map.entrySet(); for (Map.Entry keyValue : entries) { Student6 key = keyValue.getKey(); String value = keyValue.getValue(); System.out.println(key + ":" + value); }
“aababcabcdabcde”,获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)
//创建字符串,后期可以用键盘录入进行修改
String str = "aadbabcabcabcde";
//创建集合去存储字符串每个字符出现的次数
TreeMap map = new TreeMap<>();
//将字符串转换成字符数组
char[] chars = str.toCharArray();
//循环遍历字符串数组
for (Character c : chars) {
//将字符数组的每个字符放进集合中查找,是否能匹配到相同的键
Integer num = map.get(c);
//先判断集合中是否存在该字符,如果不存在的话,设置初始值为1 如果存在,则使num++
if (num == null) {
map.put(c, 1);
} else {
num++;
map.put(c, num);
}
}
//可以一定 一个StringBuffer来存储输出的数据
StringBuffer stringBuffer = new StringBuffer();
Set> set = map.entrySet();
for (Map.Entry KeyValue : set) {
Character key = KeyValue.getKey();
Integer value = KeyValue.getValue();
stringBuffer.append(key + "(" + value + ")");
// System.out.print(key + "(" + value + ")");
}
//将StringBuffer转成字符串
String result = stringBuffer.toString();
System.out.println("统计结果为:" + result);
集合的嵌套遍历:
集合的嵌套遍历
HashMap嵌套HashMap
HashMap嵌套ArrayList
ArrayList嵌套HashMap
//HashMap嵌套HashMap // HashMap面试题, String> map = new HashMap<>(); // // //创建两个HashMap对象 // HashMap map1 = new HashMap<>(); // map1.put("王子1", "白雪公主"); // map1.put("王子2", "灰姑凉"); // map1.put("王子3", "美人鱼"); // // HashMap map2 = new HashMap<>(); // map2.put("邓超", "孙俪"); // map2.put("张杰", "谢娜"); // map2.put("小虎", "冯提莫"); // // map.put(map1, "童话故事"); // map.put(map2, "现实故事"); // // //输出map集合中的元素 // System.out.println(map); // //循环遍历map集合中的元素 // Set , String>> set = map.entrySet(); // for (Map.Entry , String> m : set) { // HashMap key = m.getKey(); // String value = m.getValue(); // System.out.println(key + "---" + value); // } // HashMap嵌套ArrayList //定义一个ArrayList对象 ArrayList > list = new ArrayList<>(); //创建2个HashMap对象 HashMap map1 = new HashMap<>(); map1.put("霞", "洛"); map1.put("卢锡安", "塞纳"); map1.put("蛮王", "寒冰"); map1.put("盖伦", "卡特"); HashMap map2 = new HashMap<>(); map2.put("邓超", "孙俪"); map2.put("张杰", "谢娜"); map2.put("小虎", "冯提莫"); list.add(map1); list.add(map2); //遍历 for (HashMap maps : list) { Set > entries = maps.entrySet(); for (Map.Entry keyValue : entries) { String key = keyValue.getKey(); String value = keyValue.getValue(); System.out.println(key + ":" + value); } }
面试题:HashMap和Hashtable的区别
1、HashMap与Hashtable它们存储的元素都是一个一个的键值对
2、HashMap中的key和value允许为null值,而Hashtable不允许
3、Hashtable是线程安全的,而HashMap是线程不安全的
//创建HashMap集合对象
HashMap map = new HashMap<>();
//创建Hashtable的集合对象
Hashtable hashtable = new Hashtable<>();
//HashMap存储数据的时候用的是键值对的形式,然而当中的key和value都允许为null值
map.put(null,null);
System.out.println(map);
//HashMap存储数据的时候用的是键值对的形式,然而当中的key和value不允许为null值
//NullPointerException 会报错空指针的错误
hashtable.put(null,null);
System.out.println(hashtable);
Collections工具类的概述
Collections工具类的概述:
针对集合操作的工具类Collections与Collection的区别:
1、Collection是单列集合的顶层接口,有两大子接口List/Set
2、Collections是一个针对于集合操作的工具类,可以对集合进行排序,还有查找(二分查找) public static void sort(List list)
public static int binarySearch(List> list,T key)
public static T max(Collection> coll)
public static void reverse(List> list)
public static void shuffle(List> list) static List synchronizedList(List list) 返回由指定列表支持的同步(线程安全)列表。
//创建List集合对象
ArrayList list = new ArrayList<>();
//将ArrayList转换成线程安全的集合,返回的集合和原本的是一个存储空间
List list1 = Collections.synchronizedList(list);
// System.out.println(list.hashCode());
// System.out.println(list1.hashCode());
//添加元素
list1.add(10);
list1.add(30);
list1.add(20);
list1.add(70);
list1.add(40);
list1.add(50);
System.out.println(list);
System.out.println(list1);
// System.out.println("========================================");
// System.out.println("排序前的集合:"+list);
// //public static void sort(List list)
// Collections.sort(list);
// System.out.println("排序后的集合:"+list);
// System.out.println("========================================");
//
// //排序后的集合:[10, 20, 30, 40, 50, 70]
// //public static binarySearch(List> list,T key):二分查找前提是有序
// //二分查找,如果元素存在,返回该元素对应索引
// System.out.println(Collections.binarySearch(list,70));
// System.out.println(Collections.binarySearch(list,700));
//
// //public static T max(Collection> coll):查找最大值
// System.out.println(Collections.max(list));
//
// //public static void reverse(List> list) 反转
// Collections.reverse(list);
// System.out.println(list);
//
// //public static void shuffle(List> list):随机置换
// Collections.shuffle(list);
// System.out.println(list);



