map集合2.0

 一.特征

通过键-值(key-value)对的形式来存储数据

Map的实现:HashMap

Map中,key可以为任意类型,但这里建议使用String,value也可以是任意类型

Map里面多个value可以是不同类型

Map里面key是可以重复的,当key重复时,后存入的数据会覆盖前面的数据

Map里面,value可以重复.

Map里面的key可以为null,但是只能有一个,多个的时候,后面的会覆盖前面的

Map中value可以是null,多个value可以同时为null

二.HashMap原理

,哈希表存储采用数组+链表+红黑树实现

当链表长度超过阈值(8),先判断table的长度是否大于64,就转换为红黑树,这样大大减少了查询时间

如果小于64,就通过扩容的方式来解决,避免红黑树结构化.

三.HashMap的遍历

1.通过entrySet()方法,得到一个Entry对象的集合,这里用Set来装载.

2.通过Set中的迭代器,迭代出每一个Entry对象. ​

3.针对每一个Entry对象,通过getKey()和getValue()方法,来分别获取到对应的key和value

四.泛型

Map<String,Object> map = new HashMap<String,Object>();

五,Collections 集合的工具类

 public static <T>

boolean addAll(Collection<T> c, T... elements): 往集合中添加一些元素

 public static void shuffle(List<?> list): 打乱集合顺序 

public static <T> void sort(List<T> list): 将集合中元素按照默认规则排序

public static <T> void sort(List<T> list,Comparator<? super T>): 将集合中元素按照指定规则排序

六,Map接口常用方法


1.clear


​ 方法全称为:void clear(),移除map里所有的映射关系,使用方法如下:

public static void main(String[] args) {
    Map map=new HashMap();
    map.put(1,"张三");
    map.put(2,"李四");
    map.put(3,"王麻子");
    map.clear();
}


2.containsKey


​ 方法全称为:boolean containsKey(Object key),containsKey底层调用了equals方法,查询key中是否包含某个元素,使用方法如下:

public static void main(String[] args) {
    Map map=new HashMap();
    map.put(1,"张三");
    map.put(2,"李四");
    map.put(3,"王麻子");
    boolean res = map.containsKey(3);
    System.out.println(res);
}


3.containsValue


​ 方法全称为:boolean containsValue(Object value),查询value中是否包含某个元素,containsValue底层用了equals方法,使用方法如下:

public static void main(String[] args) {
    Map map=new HashMap();
    map.put(1,"张三");
    map.put(2,"李四");
    map.put(3,"王麻子");
    boolean res = map.containsValue("张三");
    System.out.println(res);
}


4.get


​ 方法全称为:Object get(Object key),查询key对应的value值,返回value值,使用方法如下:

public static void main(String[] args) {
    Map map=new HashMap();
    map.put(1,"张三");
    map.put(2,"李四");
    map.put(3,"王麻子");
    Object o = map.get(3);
    System.out.println(o);
}


5.isEmpty


​ 方法全称为:boolean isEmpty(),用来判断map集合中是否为空,若为空则返回true,使用方法如下:

public static void main(String[] args) 
    Map map=new HashMap();
    map.put(1,"张三");
    map.put(2,"李四");
    map.put(3,"王麻子");
    boolean res = map.isEmpty();
    System.out.println(res);
}


6.keySet


​ 方法全称为:Set keySet(),用来获取map集合中所有的key,并将所有的key中存入set集合中,使用方法如下:

public static void main(String[] args) {
    Map map=new HashMap();
    map.put(1,"张三");
    map.put(2,"李四");
    map.put(3,"王麻子");
    Set set = map.keySet();
    for (Object o : set) {
        System.out.println(o);
    }
}


7.put


​ 方法全称为:put(Object key,Object value),给map集合中添加键值对,使用方法如下:

public static void main(String[] args) {
    Map map=new HashMap();
    map.put(1,"张三");
    map.put(2,"李四");
    map.put(3,"王麻子");
}


8.remove


​ 方法全称为:remove(),利用key删除map集合中的元素,使用方法如下:

public static void main(String[] args) {
    Map map=new HashMap();
    map.put(1,"张三");
    map.put(2,"李四");
    map.put(3,"王麻子");
    map.remove(3);
}


9.size


​ 方法全称为:int size(),获取map集合中键值对的个数,使用方法如下:

public static void main(String[] args) {
    Map map=new HashMap();
    map.put(1,"张三");
    map.put(2,"李四");
    map.put(3,"王麻子");
    int i = map.size();
    System.out.println(i);
}



10.values


​ 方法全称为:Collection values(),获取map集合中所有的value值,并将value值存入Collection集合中返回,使用方法如下:

public static void main(String[] args) {
    Map map=new HashMap();
    map.put(1,"张三");
    map.put(2,"李四");
    map.put(3,"王麻子");
    Collection values = map.values();
    //Collection遍历也可用迭代和获取下标的方法
    for (Object value : values) {
        System.out.println(value);
    }
}



11.entrySet


​ 方法全称为:Set(Map.Entry<K,V>) entrySet(),将map集合转换为set集合,set集合中元素的类型是Map.entry<K,V>,Map.Entry和String一样,都是一种类型的名字,只不过Map.Entry是静态内部类,是Map中的静态内部类,使用方法如下:

public static void main(String[] args) {
    Map map=new HashMap();
    map.put(1,"张三");
    map.put(2,"李四");
    map.put(3,"王麻子");
    Set set = map.entrySet();
    for (Object o : set) {
        System.out.println(o);
    }
}


12.map集合的遍历


​ 第一种方式:

map集合不能迭代,也不能使用foreach,所有只能先获取去所有的key,遍历key然后通过key获取每一个key和value,代码和结果如下:

public static void main(String[] args) {
    Map<Integer,String> map=new HashMap<>();
    map.put(1,"张三");
    map.put(2,"李四");
    map.put(3,"王麻子");
    Set<Integer> keys = map.keySet();
    for (Integer key : keys) {
        String value = map.get(key);
        System.out.println("key="+key+",value="+value);
    }
}


​ 第二种方式:

​ 把map集合全部转换成set集合,set集合中元素的类型是Map.Entry,取出Map.entry对象之后,可以获取到key和value,代码和结果如下:

public static void main(String[] args) {
    Map<Integer,String> map=new HashMap<>();
    map.put(1,"张三");
    map.put(2,"李四");
    map.put(3,"王麻子");
    Set<Map.Entry<Integer, String>> entries = map.entrySet();
    for (Map.Entry<Integer, String> entry : entries) {
        Integer key = entry.getKey();
        String value = entry.getValue();
    }
}


 

Swagger2默认不支持Map集合的返回,需要自定义配置来解决这个问题。 可以在Swagger2的配置文件中添加一个自定义的Docket Bean,并在该Bean中添加一个自定义的ModelBuilderPlugin,用于处理Map集合的返回值。具体代码如下: ``` @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build() .pathMapping("/") .directModelSubstitute(LocalDate.class, String.class) .genericModelSubstitutes(ResponseEntity.class) .apiInfo(apiInfo()) .extensions(Collections.singletonList(new MapModelBuilderPlugin())); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("API") .description("API reference for developers") .termsOfServiceUrl("http://localhost:8080/") .contact(new Contact("Developers", "http://localhost:8080/", "")) .license("Apache License Version 2.0") .licenseUrl("https://www.apache.org/licenses/LICENSE-2.0") .version("2.0") .build(); } private static class MapModelBuilderPlugin implements ModelBuilderPlugin { @Override public boolean supports(DocumentationType delimiter) { return true; } @Override public void apply(ModelContext modelContext) { ResolvedType type = modelContext.getTypeResolver().resolve(Map.class, WildcardType.class); if (type != null) { ModelReference ref = modelContext.getResolver().resolve(Map.class, WildcardType.class, modelContext.getAlternateTypeProvider()); ModelImpl model = new ModelImpl().type(type); modelContext.getDocumentationType().getModelNames().add(ref.getType().getErasedType().getSimpleName()); modelContext.getAdditionalModels().add(ref.getType().getErasedType(), model); } } } } ``` 其中,MapModelBuilderPlugin用于处理Map集合的返回值,将Map类型转换成ModelImpl类型。在Docket Bean中添加该插件即可解决Map集合返回的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值