赞
踩
目录
①以键值对的形式存放数据:
- package com.zq.map;
-
- import java.util.HashMap;
- import java.util.Map;
-
- public class Demo1 {
- public static void main(String[] args) {
- Map<String,Integer> map=new HashMap<>();
- //增
- // V put(K key, V value);
- map.put("a", 1);
- map.put("b", 2);
- map.put("c", 3);
- map.put("d", 4);
- System.out.println("普通遍历"+map);
- }
- }
结果:
②Map集合增删改:
- package com.zq.map;
-
- import java.util.HashMap;
- import java.util.Map;
-
- public class Demo1 {
- public static void main(String[] args) {
- Map<String,Integer> map=new HashMap<>();
- //增
- // V put(K key, V value);
- map.put("a", 1);
- map.put("b", 2);
- map.put("c", 3);
- map.put("d", 4);
- System.out.println("增加结果:"+map);
-
- //删除
- Object obj = map.remove("b");
- //打印删除的值
- System.out.println(obj);
- System.out.println("删除后结果"+map);
-
- //修改 也是调用put方法
- map.put("c", 32);
- System.out.println("修改后结果"+map);
- }
- }
结果:
①拿到Key,再拿值:
- //1.先拿到map集合中所有key
- Set keyS = map.keySet();
- for (Object key : keyS) {
- System.out.println("键:"+key+";值:"+map.get(key));
- }
结果:
②:拿到映射关系,键值都有了
- //拿到映射关系
- Set<Entry<String, Integer>> entrySet = map.entrySet();
- for (Entry<String, Integer> obj1 : entrySet) {
- System.out.println("键:"+obj1.getKey()+"值:"+obj1.getValue());
- }
结果:
解决思路:
1.做字符串切割,得到一个字符串组
2.接下来遍历,拿到单个字符
3.如果该字符没有出现过,即value值为null,那么设置该字符为KEY(键)初始value值为1
4.如果已经出现过,拿到原来的值+1
代码展示:
- package com.zq.map;
-
- import java.util.HashMap;
- import java.util.Map.Entry;
- import java.util.Set;
-
- public class Demo2 {
- public static void main(String[] args) {
- String s = "sdjsahdjshjdsdjsdiwjksjakjdsa";
-
- char[] arr = s.toCharArray();//分割字符串
- HashMap<Character, Integer> map = new HashMap<>();
- for (char c : arr) {
- Integer value = map.get(c);
- if (value == null) {
- map.put(c, 1);
- } else {
- map.put(c, value + 1);
- }
- }
-
- Set<Entry<Character, Integer>> entrySet = map.entrySet();
- for (Entry<Character, Integer> entry : entrySet) {
- System.out.println("键:" + entry.getKey() + ":值" + entry.getValue());
- }
- }
- }
结果:
Map<Character, Integer> map = new TreeMap<>() ;
代码展示:
- package com.zq.map;
-
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Set;
- import java.util.TreeMap;
-
- public class Demo2 {
- public static void main(String[] args) {
- String s = "sdjsahdjshjdsdjsdiwjksjakjdsa";
-
- char[] arr = s.toCharArray();
- Map<Character, Integer> map = new TreeMap<>() ;
- for (char c : arr) {
- Integer value = map.get(c);
- if (value == null) {
- map.put(c, 1);
- } else {
- map.put(c, value + 1);
- }
- }
-
- Set<Entry<Character, Integer>> entrySet = map.entrySet();
- for (Entry<Character, Integer> entry : entrySet) {
- System.out.println("键:" + entry.getKey() + ":值" + entry.getValue());
- }
- }
- }
结果: 我们成功按照首字母进行了升序排序!
Map<Character, Integer> map = new TreeMap<>((x,y) ->y-x);
结果: 我们成功按照首字母进行了降序排序!
- package com.zq.map;
-
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.TreeMap;
-
-
- public class Demo3 {
- public static void main(String[] args) {
- String s = "sdjsahdjshjdsdjsdiwjksjakjdsa";
-
- char[] arr = s.toCharArray();
- Map<Character, Integer> map = new TreeMap<>((x,y) ->y-x);
- for (char c : arr) {
- Integer value = map.get(c);
- if (value == null) {
- map.put(c, 1);
- } else {
- map.put(c, value + 1);
- }
- }
-
- //map.entrySet() 拿到映射关系
- List<Map.Entry<Character, Integer>> aa=new ArrayList<>(map.entrySet());
- //实现接口sort
- aa.sort(new Comparator<Map.Entry<Character, Integer>>() {
- //实现Comparator
- @Override
- public int compare(Entry<Character, Integer> o1, Entry<Character, Integer> o2) {
- return o1.getValue() - o2.getValue();
- }
- });
-
- //遍历
- for (Entry<Character, Integer> entry : aa) {
- System.out.println(entry);
- }
- }
- }
结果: 我们成功按照字母出现次数进行了升序排序!
泛型:就是一种不确定的数据类型。
比如:ArrayList<E> E就是泛型。 这种不确定的数据类型需要在使用这个类的时候才能够确定出来。
泛型可以省略,如果省略,默认泛型是Object类型。
泛型的好处:
1. 省略了强转的代码。
2. 可以把运行时的问题提前暴露到编译时期。泛型之前:不健状代码,会在运行时才会把错误暴露出来
泛型之后:将潜在的问题暴露出来,早预防早治疗 将运行期的出现的异常转换为编译期的错误
- package com.zq.map;
-
- import java.util.HashSet;
- import java.util.Set;
-
- public class Demo4 {
- public static void main(String[] args) {
- Set set = new HashSet<>();
- set.add(1);
- set.add(2);
- set.add(3);
- set.add(4);
- set.add(5);
- for (Object object : set) {
- if(Integer.valueOf(object.toString()) % 2 == 0) {
- System.out.println(object);
- }
- set.remove(object);
- }
- }
- }
面试题2:分析题目,问是否出现问题?
- String[] arr={"a","b","c","d"};
- List<String> list = Arrays.asList(arr);
- // Object[] array = list.toArray();
- list.add("e");
- // java.lang.UnsupportedOperationException
- System.out.println(list.size());
结果为:
①:基本数据类型排序:
- package com.zq.map;
-
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
-
- public class Demo6 {
- public static void main(String[] args) {
- List<String> list = new ArrayList<String>();
- list.add("b");
- list.add("a");
- list.add("c");
- list.add("d");
- System.out.println("默认无序"+list);
-
-
- //将list集合进行排序 升序
- Collections.sort(list);
- System.out.println("升序"+list);
-
- //降序 这里的x跟y指的是集合里的元素
- Collections.sort(list, (x,y) -> y.compareTo(x));
- System.out.println("降序"+list);
- }
- }
结果为:
②对象等其他数据类型排序:
- package com.zq.map;
-
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.List;
-
- public class Demo6 {
- public static void main(String[] args) {
- List<Preson> list = new ArrayList<Preson>();
- list.add(new Preson("b", 12));
- list.add(new Preson("s", 14));
- list.add(new Preson("k", 19));
- list.add(new Preson("a", 18));
- System.out.println("排序前 :"+list);
-
- //实现接口的做法
- Collections.sort(list);
- System.out.println("实现接口排序后:"+list);
-
- //不实现接口的做法
- Collections.sort(list,(x,y)->x.getName().compareTo(y.getName()));
- System.out.println("不实现接口排序:"+list);
-
- Integer[] arr= {3,2,4,6,8,1};
- Arrays.sort(arr);
- System.out.println("打印地址:"+arr);
-
-
- System.out.println("默认升序: "+Arrays.toString(arr));
-
- Arrays.sort(arr,(x,y)-> y-x);
- System.out.println("降序:"+Arrays.toString(arr));
-
- }
- }
- class Preson implements Comparable<Preson>{
- private String name;
- private int 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;
- }
- public Preson() {
- // TODO Auto-generated constructor stub
- }
- public Preson(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- @Override
- public String toString() {
- return "Preson [name=" + name + ", age=" + age + "]";
- }
- //实现方法
- @Override
- public int compareTo(Preson o) {
- return o.getName().compareTo(this.getName());
- }
-
- }
- String[] str = {"a","b","c","d","e"};
- List<String> list = Arrays.asList(str);
- //数组转为集合
- Object[] array = list.toArray();
②数组转集合:asList
- String[] str = {"a","b","c","d","e"};
- List<String> list = Arrays.asList(str);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。