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. vue2.0实践的一些细节

    最近用vue2.0做了个活动.做完了回头发现,好像并没有太多的技术难点,而自己好像又做了比较久...只能说效率有待提升啊...简单总结了一些比较细节的点. 1.对于一些已知肯定会有数据的模块,先用一个 ...

  2. 使用C#处理基于比特流的数据

    使用C#处理基于比特流的数据 0x00 起因 最近需要处理一些基于比特流的数据,计算机处理数据一般都是以byte(8bit)为单位的,使用BinaryReader读取的数据也是如此,即使读取bool型 ...

  3. 使用UIBezierPath绘制图形

    当需要画图时我们一般创建一个UIView子类, 重写其中的drawRect方法 再drawRect方法中利用UIBezierPath添加画图 UIBezierPath的使用方法: (1)创建一个Bez ...

  4. lua执行字节码的过程介绍

    前面一篇文章中介绍了lua给下面代码生成最终的字节码的整个过程,这次我们来看看lua vm执行这些字节码的过程. foo = "bar" local a, b = "a& ...

  5. CSS样式重置(转)

    body,h1,h2,h3,h4,h5,h6,dl,dt,dd,ul,ol,li,th,td,p,blockquote,pre,form,fieldset,legend,input,button,te ...

  6. UVA, 10336 Rank the Languages

    难点在于:递归函数和输出: #include <iostream> #include <vector> #include <algorithm> #include ...

  7. css知多少之绝对定位小记

    一.position定位常见属性 对于属性position来说,属性值有static/relative/absolute/fixed/inherit以下只对绝对定位position:absolute详 ...

  8. NPM如何更新到最新版

    参考文章--npm更新到最新版本的方法 其实我们可以这样,随便新建一个文件夹例如:F:\test.按着"shift"键,右键该文件夹,选择"在此处打开命令窗口(W)&qu ...

  9. Android之Pull解析XML

    一.Pull解析方法介绍 除了可以使用SAX和DOM解析XML文件,也可以使用Android内置的Pull解析器解析XML文件.Pull解析器的运行方式与SAX解析器相似.它也是事件触发的.Pull解 ...

  10. git快速get

    配置:git config --global user.name 'yangshaoxiang' git config --global user.email '254135495@qq.com' s ...