• commons的迭代器和双向Map——高淇JAVA300讲笔记之commons


    案例一:迭代器

      1 package com.bjsxt.others.commons;
      2 
      3 import java.util.ArrayList;
      4 import java.util.Iterator;
      5 import java.util.List;
      6 
      7 import org.apache.commons.collections4.IterableMap;
      8 import org.apache.commons.collections4.MapIterator;
      9 import org.apache.commons.collections4.Predicate;
     10 import org.apache.commons.collections4.iterators.ArrayListIterator;
     11 import org.apache.commons.collections4.iterators.FilterIterator;
     12 import org.apache.commons.collections4.iterators.LoopingIterator;
     13 import org.apache.commons.collections4.iterators.UniqueFilterIterator;
     14 import org.apache.commons.collections4.map.HashedMap;
     15 
     16 
     17 /**
     18  * 迭代器的扩展
     19  * 1、MapIterator 以后不再使用map.keySet.iterator访问
     20  *   IterableMap  HashedMap
     21  * 2、UniqueFilterIterator 去重迭代器
     22  * 3、FilterIterator 自定义过滤 + Predicate
     23  * 4、LoopingIterator 循环迭代器
     24  * 5、ArrayListIterator 数组迭代器
     25  *
     26  */
     27 public class Demo06 {
     28 
     29     public static void main(String[] args) {
     30 //        mapIt();
     31 //        uniqueIt();
     32 //        filterIt();
     33 //        loopIt();
     34         arrayIt();
     35     }
     36     /**
     37      * 数组迭代器
     38      */
     39     public static void arrayIt() {
     40         System.out.println("=======数组迭代器======");
     41         int[] arr = {1,2,3,4,5};
     42         //数组迭代器
     43 //        Iterator<Integer> it = new ArrayListIterator<Integer>(arr);
     44         //指定起始索引和结束索引
     45         Iterator<Integer> it = new ArrayListIterator<Integer>(arr,1,3);
     46         while(it.hasNext()) {
     47             System.out.println(it.next());
     48         }
     49     }
     50     
     51     /**
     52      * 循环迭代器
     53      */
     54     public static void loopIt() {
     55         System.out.println("=======循环迭代器======");
     56         List<String> list = new ArrayList<String>();
     57         list.add("refer");
     58         list.add("dad");
     59         list.add("bjsxt");
     60         list.add("moom");
     61         
     62         Iterator<String> it = new LoopingIterator(list);
     63         for(int i=0;i<5;i++) {
     64             System.out.println(it.next());
     65         }
     66         
     67     }
     68     
     69     
     70     /**
     71      * 自定义迭代器
     72      */
     73     public static void filterIt() {
     74         System.out.println("=======自定义迭代器======");
     75         List<String> list = new ArrayList<String>();
     76         list.add("refer");
     77         list.add("dad");
     78         list.add("bjsxt");
     79         list.add("moom");
     80         //自定义条件判断
     81         Predicate<String> pre = new Predicate<String>() {
     82             @Override
     83             public boolean evaluate(String value) {
     84                 //回文判断
     85                 return new StringBuilder(value).reverse().toString().equals(value);
     86             }
     87         };
     88         
     89         //过滤器
     90         Iterator<String> it = new FilterIterator(list.iterator(),pre);
     91         while(it.hasNext()) {
     92             System.out.println(it.next());
     93         }
     94     }
     95     
     96     /**
     97      * 去重迭代器
     98      */
     99     public static void uniqueIt() {
    100         System.out.println("=======去重迭代器======");
    101         List<String> list = new ArrayList<String>();
    102         list.add("a");
    103         list.add("b");
    104         list.add("a");
    105         //去除重复的过滤器
    106         Iterator<String> it = new UniqueFilterIterator(list.iterator());
    107         while(it.hasNext()) {
    108             System.out.println(it.next());
    109         }
    110     }
    111     
    112     /**
    113      * map迭代器
    114      */
    115     public static void mapIt() {
    116         System.out.println("=======map迭代器======");
    117         IterableMap<String,String> map = new HashedMap<String,String>();
    118         map.put("a","bjsxt");
    119         map.put("b","sxt");
    120         map.put("c","good");
    121         //使用MapIterator
    122         MapIterator<String,String> it = map.mapIterator();
    123         while(it.hasNext()) {
    124             //一定要it.next()
    125             String key = it.next();
    126             String value = it.getValue();
    127             System.out.println(key+"-->"+value);
    128                 
    129         }
    130             
    131     }
    132 
    133 }

    案例二:有序和无序的双向Map

     1 package com.bjsxt.others.commons;
     2 
     3 import org.apache.commons.collections4.BidiMap;
     4 import org.apache.commons.collections4.MapIterator;
     5 import org.apache.commons.collections4.bidimap.DualHashBidiMap;
     6 import org.apache.commons.collections4.bidimap.DualTreeBidiMap;
     7 
     8 /**
     9  * 双向Map 要求键与值都不能重复
    10  * BidiMap  inverseBidiMap()
    11  * 1、DualTreeBidMap: 有序
    12  * 2、DualHashBidMap: 无序
    13  *
    14  */
    15 public class Demo07 {
    16 
    17     public static void main(String[] args) {
    18         hashMap();
    19         treeMap();
    20     }
    21     
    22     /**
    23      * 有序的双向Map
    24      */
    25     public static void treeMap() {
    26         System.out.println("=======有序的双向Map=======");
    27         BidiMap<String,String> map = new DualTreeBidiMap<String,String>();
    28         map.put("bj", "bj@test.com");
    29         map.put("sxt", "sxt@qq.com");
    30     
    31         //遍历查看
    32         MapIterator<String,String> it = map.inverseBidiMap().mapIterator();
    33         while(it.hasNext()) {
    34             String key = it.next();
    35             String value = it.getValue();
    36             System.out.println(key+"-->"+value);
    37         }
    38     }
    39     
    40     /**
    41      * 无序的双向Map
    42      */
    43     public static void hashMap() {
    44         System.out.println("=======无序的双向Map=======");
    45         BidiMap<String,String> map = new DualHashBidiMap<String,String>();
    46         map.put("bj", "bj@test.com");
    47         map.put("sxt", "sxt@qq.com");
    48         //反转
    49         System.out.println(map.inverseBidiMap().get("sxt@qq.com"));
    50         //遍历查看
    51         MapIterator<String,String> it = map.inverseBidiMap().mapIterator();
    52         while(it.hasNext()) {
    53             String key = it.next();
    54             String value = it.getValue();
    55             System.out.println(key+"-->"+value);
    56         }
    57     }
    58 
    59 }

    运行结果:

    =======无序的双向Map=======
    sxt
    bj@test.com-->bj
    sxt@qq.com-->sxt
    =======有序的双向Map=======
    bj@test.com-->bj
    sxt@qq.com-->sxt

    案例三:Bag 包

     1 package com.bjsxt.others.commons;
     2 
     3 import java.util.Iterator;
     4 import java.util.Set;
     5 
     6 import org.apache.commons.collections4.Bag;
     7 import org.apache.commons.collections4.bag.HashBag;
     8 import org.apache.commons.collections4.bag.TreeBag;
     9 
    10 /**
    11  * Bag 包 允许重复
    12  * 1、HashBag 无序
    13  * 2、TreeBag 无序
    14  * 统计单词的出现次数
    15  *
    16  */
    17 public class Demo08 {
    18     public static void main(String[] args) {
    19         hashBag();
    20         treeBag();
    21         String str = "this is a cat and that is a mice where is the food";
    22         //分割字符串
    23         String[] strArray = str.split(" ");
    24         Bag<String> bag = new TreeBag<String>();
    25         for(String temp:strArray) {
    26             bag.add(temp);
    27         }
    28         
    29         System.out.println("========统计次数======");
    30         Set<String> keys = bag.uniqueSet();
    31         for(String letter:keys) {
    32             System.out.println(letter+"-->"+bag.getCount(letter));
    33         }
    34         
    35     }
    36     /**
    37      * 有序
    38      */
    39     public static void treeBag() {
    40         System.out.println("======有序的包=======");
    41         Bag<String> bag = new TreeBag<String>();
    42         bag.add("a");
    43         bag.add("a",5);
    44         bag.remove("a",2);
    45         bag.add("b");
    46         bag.add("c");
    47         
    48         Iterator<String> it = bag.iterator();
    49         while(it.hasNext()) {
    50             System.out.println(it.next());
    51         }
    52     }
    53     /**
    54      * 无序
    55      */
    56     public static void hashBag() {
    57         System.out.println("======无序的包=======");
    58         Bag<String> bag = new HashBag<String>();
    59         bag.add("a");
    60         bag.add("a",5);
    61         bag.remove("a",2);
    62         bag.add("b");
    63         bag.add("c");
    64         
    65         Iterator<String> it = bag.iterator();
    66         while(it.hasNext()) {
    67             System.out.println(it.next());
    68         }
    69     }
    70 }

    运行结果:

    ======无序的包=======
    a
    a
    a
    a
    b
    c
    ======有序的包=======
    a
    a
    a
    a
    b
    c
    ========统计次数======
    a-->2
    and-->1
    cat-->1
    food-->1
    is-->3
    mice-->1
    that-->1
    the-->1
    this-->1
    where-->1
  • 相关阅读:
    谈谈 OC 中的内联函数
    Spring 新手教程(二) 生命周期和作用域
    实时竞价(RTB) 介绍(基础篇)
    oracle数据库性能优化方案精髓整理收集回想
    HNU 13411 Reverse a Road II(最大流+BFS)经典
    CSS3主要知识点复习总结+HTML5新增标签
    修改默认MYSQL数据库data存放位置
    mysql状态查看 QPS/TPS/缓存命中率查看
    Mysql5.7.10新加用户
    很靠谱linux常用命令
  • 原文地址:https://www.cnblogs.com/swimminglover/p/8402180.html
Copyright © 2020-2023  润新知