1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap

(1)其中LinkedHashMap是有序的  怎么存怎么取出来

我们讲一下Map的增删改查功能:

        /*
* Map集合的添加
*/
Map<String, String> map = new HashMap<String, String>();
map.put("星期一", "Monday");
map.put("星期六", "Sunday");
System.out.println(map); /*
* 通过建 获取值
*/
String string = map.get("星期六");
System.out.println(string); /*
* 通过建 修改值
*/
map.put("星期一", "Mon");
System.out.println(map); /*
* 通过建 删除值
*/
String remove = map.remove("星期六");
System.out.println(remove);
System.out.println(map);
}

以上就是Map的增删改查方法

2.Map的遍历

1.第一种:通过keySet()方法获取集合的所有建 存储在Set<>集合中

(1)增强for

(2)迭代器

Map<String, Character> map =new LinkedHashMap<String, Character>();
        map.put("一", '1');
        map.put("二", '2');
        map.put("三", '3');
        map.put("四", '4');
        map.put("五", '5');
        Set<String> set = map.keySet();

(1)增强for

    for (String string : set) {
            System.out.println(string+" "+map.get(string));
        }

  (2)迭代器

    Iterator<String> iterator = set.iterator();
            while(iterator.hasNext()){
            String str=iterator.next();
            Integer integer = map.get(str);
            System.out.println(str+"   "+integer);
        }

第二种 通过 Map.Entry<K,V>    在Map类设计时,提供了一个嵌套接口:Entry。  Entry将键值对的对应关系封装成了对象。

getKey()方法:获取Entry对象中的键
          getValue()方法:获取Entry对象中的值
          entrySet()方法:用于返回Map集合中所有的键值对(Entry)对象,以Set集合形式返回。

(1)增强for

      Set<Entry<String, Character>> entries = map.entrySet();
            for (Entry<String, Character> entry : entries) {
                String str = entry.getKey();
                Character character = entry.getValue();
                System.out.println(str+"   "+character);

       }

(2)迭代器

    Set<Map.Entry<String, String>> entries = map.entrySet();
          Iterator<Map.Entry<String, String>> iterator = entries.iterator();
          while(iterator.hasNext()){
                Entry<String, String> entry = iterator.next();
                System.out.println(entry.getKey()+"  "+entry.getValue());
        }

3.Map之嵌套

    我们在嵌套Map中传入引用类型Person  这里记得要重写equlas与HashCode方法

    private static void fun6() {
Map<Person, String> map1 = new LinkedHashMap<Person, String>();
map1.put(new Person("黄晓明", 40), "北京");
map1.put(new Person("AnlayBay", 36), "北京"); Map<Person, String> map2 = new LinkedHashMap<>();
map2.put(new Person("宋江",40), "梁山");
map2.put(new Person("林冲",40), "豹子头"); Map<Map<Person,String>, String> map = new LinkedHashMap<>();
map.put(map1, "第一组");
map.put(map2, "第二组"); for (Entry<Map<Person,String>, String> zhu: map.entrySet()) {
String value = zhu.getValue();
for (Entry<Person, String> entry : zhu.getKey().entrySet()) {
Person key = entry.getKey();
String value2 = entry.getValue();
System.out.println(value+".."+key+".."+value2);
}
} }
/**
* 迭代器遍历
*/
private static void fun5() {
Map<Person, String> map1 = new LinkedHashMap<Person, String>();
map1.put(new Person("黄晓明", 40), "北京");
map1.put(new Person("AnlayBay", 36), "北京"); Map<Person, String> map2 = new LinkedHashMap<>();
map2.put(new Person("宋江",40), "梁山");
map2.put(new Person("林冲",40), "豹子头"); Map<Map<Person,String>, String> map = new LinkedHashMap<>();
map.put(map1, "第一组");
map.put(map2, "第二组"); Set<Map<Person,String>> set = map.keySet();
Iterator<Map<Person, String>> iterator = set.iterator();
while(iterator.hasNext()){
Map<Person, String> next = iterator.next();
String string = map.get(next); Set<Person> persons = next.keySet();
Iterator<Person> iterator2 = persons.iterator();
while(iterator2.hasNext()){
Person next2 = iterator2.next();
String string2 = next.get(next2);
System.out.println(string2+" "+next2+" "+string);
}
}
}

这里我补充一个集合的工具类

Collections

public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("a");
arrayList.add("s");
arrayList.add("g");
arrayList.add("d"); //排序
Collections.sort(arrayList);
System.out.println(arrayList); //翻转
Collections.reverse(arrayList);
System.out.println(arrayList); //打乱顺序
Collections.shuffle(arrayList);
System.out.println(arrayList); //二分查找
List<Integer> integers = new ArrayList<>();
Collections.addAll(integers, 1,2,3,4,5,7,8);
System.out.println(integers);
int binarySearch = Collections.binarySearch(integers, 6);
System.out.println(binarySearch); //打乱shuffle
Collections.shuffle(integers);
System.out.println(integers);
}

今天大部分用代码展示了,不多做介绍,集合这一模块  无非就是增删改查   还有遍历,我在学习的过程中  基本每一个集合的方法我都会敲十遍加深印象

Java基础Map接口+Collections工具类的更多相关文章

  1. 双列集合Map接口 & Collections工具类

    HashMap 常用方法 遍历方式 iterator迭代器  ITIT HashTable 继承字典 Hashtable--Properties 文件读写 总结 Collections工具类

  2. Java基础Map接口+Collections

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  3. JAVA基础知识之Collections工具类

    排序操作 Collections提供以下方法对List进行排序操作 void reverse(List list):反转 void shuffle(List list),随机排序 void sort( ...

  4. Java基础 - Map接口的实现类 : HashedMap / LinkedHashMap /TreeMap 的构造/修改/遍历/ 集合视图方法/双向迭代输出

    Map笔记: import java.util.*; /**一:Collection接口的 * Map接口: HashMap(主要实现类) : HashedMap / LinkedHashMap /T ...

  5. Java容器类Collection,List,Set,Map.,Iterator,Collections工具类,Arrays工具类,Comparable

    Java容器类Collection,List,Set,Map.,Iterator,Collections工具类,Arrays工具类,Comparable接口,泛型 Collection,List,Se ...

  6. Java:集合,Collections工具类用法

    Collections工具类提供了大量针对Collection/Map的操作,总体可分为四类,都为静态(static)方法: 1. 排序操作(主要针对List接口相关) reverse(List li ...

  7. Java集合框架:Collections工具类

    java.util.Collections工具类提供非常多实用的方法.使得程序员操作集合类的时候更加的方便easy,这些方法都是静态的. 整个Collections工具类源代码几乎相同有4000行.我 ...

  8. Java集合(1):Collections工具类中的static方法

    与Arrays一样,Collections类中也有一些实用的static方法. (1) 排序操作 reverse(List list):反转指定List集合中元素的顺序 shuffle(List li ...

  9. Java可变参数与Collections工具类使用了解

    今天发现jdk1.5后增加了个可变参数,以前还一直不晓得 public static void main(String[] args) { System.out.println(getNum(1,2, ...

随机推荐

  1. 微信小程序体验(2):驴妈妈景区门票即买即游

    驴妈妈因为出色的运营能力,被腾讯选为首批小程序内测单位.驴妈妈的技术开发团队在很短的时间内完成了开发任务,并积极参与到张小龙团队的内测问题反馈.驴妈妈认为,移动互联网时代,微信是巨大的流量入口,也是旅 ...

  2. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

  3. 【Java大系】Java快速教程

    感谢原作者:Vamei 出处:http://www.cnblogs.com/vamei Java是面向对象语言.这门语言其实相当年轻,于1995年才出现,由Sun公司出品.James Gosling领 ...

  4. Flyweight(享元模式)

    import java.util.Hashtable; /** * 享元模式 * @author TMAC-J * 享元模式一般和工厂模式一起使用,但此处为了更好说明,只用享元模式 * 定义:享元模式 ...

  5. C#事件-使用事件需要的步骤

    事件是C#中另一高级概念,使用方法和委托相关.奥运会参加百米的田径运动员听到枪声,比赛立即进行.其中枪声是事件,而运动员比赛就是这个事件发生后的动作.不参加该项比赛的人对枪声没有反应. 从程序员的角度 ...

  6. gulp 自动添加版本号

    本文介绍利用 gulp-rev 和 gulp-rev-collector 进行版本管理 npm官网介绍使用后的效果如下: "/css/style.css" => " ...

  7. js月份,日期加一天

    js没有直接可以用的函数,所以只能自己写,其中需要涉及到每个月天数的判断,如果是2月份的话,还要涉及到闰年的判断 var addDate = { //日期,在原有日期基础上,增加days天数,默认增加 ...

  8. lucene 基础知识点

    部分知识点的梳理,参考<lucene实战>及网络资料 1.基本概念 lucence 可以认为分为两大组件: 1)索引组件 a.内容获取:即将原始的内容材料,可以是数据库.网站(爬虫).文本 ...

  9. opengl 笔记(2)

    /*- * Opengl Demo Test * * Fredric : 2016-7-10 */ #include <GLUT/GLUT.h> #include <stdlib.h ...

  10. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...