澳门威斯尼人平台登陆java中具体集合

by admin on 2019年11月1日

ArrayList 一种可以动态增长和缩减的索引序列

1.ArrayList   可以动态增长和缩减的索引序列

原文地址

澳门威斯尼人平台登陆 1

LinkedList 一种可以在任何位置进行高效地插入和删除操作的有序序列

2.LinkedList 可以在任何位置高效插入和删除错左的有序序列

本文内容

  • 并行数组(Parallel Array)
  • 并行向量(Parallel Vector)
  • 并行范围(Parallel Range)
  • 并行哈希表(Parallel Hash Tables)
  • 并行散列 Tries(Parallel Hash Tries)
  • 并行并发 Tries(Parallel Concurrent Tries)
  • 参考资料

 

ArrayDeque  一种用循环数组实现的双端队列

3.HashSet  没有重复元素的无序集合

并行数组(Parallel Array)


一个
ParArray
序列包含线性、连续的元素数组。这意味着,通过修改底层数组,可以高效地访问和修改元素。因此,反序元素也很高效。并行数组跟数组一样也是固定大小的。

scala> val pa = scala.collection.parallel.mutable.ParArray.tabulate(1000)(x =>2*

 x +1)

pa: scala.collection.parallel.mutable.ParArray[Int] = ParArray(1, 3, 5, 7, 9, 11

, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51

, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91

, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 12

5, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 15

7, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 18

9, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 22

1, 223, 225, 227, 229, 231, 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 25

3, 255, 257, 259, 261, 263, 265, 267, 269, 271, 273, 275, 277, 279, 281, 283, 28

5, 287, 289, 291, 293, 295, 297, 299, 301, 303, 305, 307, 309, 311, 313, 315,...

 

scala> pa reduce(_+_)

res0: Int = 1000000

 

scala> pa map(x=>(x-1)/2)

res1: scala.collection.parallel.mutable.ParArray[Int] = ParArray(0, 1, 2, 3, 4,

5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 2

6, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 4

6, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 6

6, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 8

6, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104,

105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,

121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136,

137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152,

153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 16...

 

scala>
Guava:谷歌开发的集合库,通过build path->Add External JARs 把guava.jar包加进去。

版本控制工具:1.CVS 2.SVN 3.git
所以需要下载git客户端。

import com.google.common.collect.ImmutableList;
/**
 * 只读设置
 */
public class Demo01 {
    public static void main(String[] args) {
        List<String> list =new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        //对原有的list进行包装,相等于原有List的一个视图,快照,不够安全
        List<String> readList =Collections.unmodifiableList(list);
        readList.add("d");//对这个视图增操作,错误,抛出java.lang.UnsupportedOperationException(不被支持的异常)。
        list.add("d"); //正确,改变原有List,视图也一起改变,没有达到真正的目的,所以不够安全。

        // guava对只读设置 安全可靠,并且相对简单
        List<String> immutableList =ImmutableList.of("a", "b", "c"); //初始化List
        immutableList.add("d");//java.lang.UnsupportedOperationException
    }
}




import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;

/**
 * 函数式编程 :解耦
 * 1、Predicate 
 * 2、Function
 * 
 * 工具:Collections2.filter() 过滤器
 * Collections2.transfer() 转换
 * Functions.compose()组合式函数编程
 */
public class Demo02 {
    public static void main(String[] args) {
        //组合式函数编程
        //确保容器中的字符串长度不超过5,超过进行截取,后全部大写
        List<String> list =Lists.newArrayList("bjsxt","good","happiness");//静态创建List,
        //确保容器中的字符串长度不超过5,超过进行截取
        Function<String,String> f1 =new Function<String,String>(){
            @Override
            public String apply(String input) {
                return input.length()>5?input.substring(0,5):input;
            }
        };
        //转成大写
        Function<String,String> f2 =new Function<String,String>(){
            @Override
            public String apply(String input) {
                return input.toUpperCase();
            }
        };
        //String =f2(f1(String))
        Function<String,String> f =Functions.compose(f1, f2);
        Collection<String> resultCol =Collections2.transform(list, f);
        for(String temp:resultCol){
            System.out.println(temp);
        }
    }
    /**
     * 转换
     */
    public static void test2(){
        //类型转换
        Set<Long> timeSet =Sets.newHashSet();
        timeSet.add(10000000L);
        timeSet.add(99999999999999999L);
        timeSet.add(2000000000L);
        Collection<String> timeStrCol =Collections2.transform(timeSet, new Function<Long,String>(){
            @Override
            public String apply(Long input) {
                return new SimpleDateFormat("yyyy-MM-dd").format(input);
            }});
        for(String temp:timeStrCol){
            System.out.println(temp);
        }
    }
    /**
     * 过滤器
     */
    public static void test1(){
        //创建List 静态初始化
        List<String> list =Lists.newArrayList("moom","son","dad","bjsxt","refer");
        //找出回文 palindrome  backwords  mirror words
        //匿名内部类对象: 匿名内部类,同时创建类对象
        Collection<String> palindromeList =Collections2.filter(list, new Predicate<String>(){
            @Override
            public boolean apply(String input) {//如果这个类只使用一次,并且这个类的对象也只使用一次,就用匿名内部类对象。
                //业务逻辑
                return new StringBuilder(input).reverse().toString().equals(input);//字符串的反转等于自身。
            }
        });
        for(String temp:palindromeList){
            System.out.println(temp);
        }
    }
}




import com.google.common.base.Preconditions;
import com.google.common.collect.Constraint;
import com.google.common.collect.Constraints;
import com.google.common.collect.Sets;
/**
 * 加入约束条件:非空、长度验证
 * Constraint
 * Preconditions
 * Constraints
 */
public class Demo03 {
    public static void main(String[] args) {
        Set<String> sets =Sets.newHashSet();
        //创建约束
        Constraint<String> constraint =new Constraint<String>(){
            @Override
            public String checkElement(String element) {
                //非空验证
                Preconditions.checkNotNull(element);
                //长度验证 5-20为字符串
                Preconditions.checkArgument(element.length()>=5 && element.length()<=20);
                return element;
            }
        };
        Set<String> cs =Constraints.constrainedSet(sets, constraint);
        //cs.add(null); //java.lang.NullPointerException
        //cs.add("good"); //java.lang.IllegalArgumentException
        cs.add("bjsxt");
        for(String str:cs){
            System.out.println(str);
        }
    }
}



import com.google.common.collect.Sets;//谷歌的jar包
import com.google.common.collect.Sets.SetView;
/**
 * 集合的操作:交集、差集、并集
 * Sets.intersection()
 * Sets.difference()
 * Sets.union();
 */
public class Demo04 {
    public static void main(String[] args) {
        Set<Integer> sets =Sets.newHashSet(1,2,3,4,5,6);
        Set<Integer> sets2 =Sets.newHashSet(3,4,5,6,7,8,9);
        //交集
        System.out.println("交集为:");
        SetView<Integer> intersection =Sets.intersection(sets, sets2);
        for(Integer temp:intersection){
            System.out.println(temp);//3456
        }
        //差集
        System.out.println("差集为:");
        SetView<Integer> diff =Sets.difference(sets, sets2);
        for(Integer temp:diff){
            System.out.println(temp);//12
        }
        //并集
        System.out.println("并集为:");
        SetView<Integer> union =Sets.union(sets, sets2);
        for(Integer temp:union){
            System.out.println(temp);//123456789
        }
    }
}




import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
/**
 * 统计单词出现的次数
 * 1、HashMap 分拣存储+面向对象思维  -->判断
 * 2、Multiset :无序+可重复     .count()  增强了可读性 +操作简单
 * @author Administrator
 *
 */
public class Demo05 {
    public static void main(String[] args) {
        String str ="this is a cat and that is a mice where is the food";
        //分割字符串
        String[] strArray =str.split(" ");
        //存储到Multiset中
        Multiset<String> set =HashMultiset.create();
        for(String strTemp:strArray){
            set.add(strTemp);
        }
        //获取所有的单词 Set
        Set<String> letters =set.elementSet();
        for(String temp:letters){
            System.out.println(temp+"-->"+set.count(temp));//统计人员访问网站的 次数
        }
        /*mice-->1
        that-->1
        cat-->1
        is-->3
        food-->1
        a-->2
        the-->1
        where-->1
        this-->1
        and-->1*/
    }
}



import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
/**
 * 分析查看 教师  教授的每门课程
 * Multimap :key-value  key可以重复
 */
public class Demo06 {
    public static void main(String[] args) {
        Map<String,String> cours =new HashMap<String,String>();
        //加入测试数据
        cours.put("改革开放", "邓爷爷");
        cours.put("三个代表", "江主席");
        cours.put("科学发展观", "胡主席");
        cours.put("和谐社会", "胡主席");
        cours.put("八荣八耻", "胡主席");
        cours.put(".1..", "习主席");
        cours.put("..2.", "习主席");
        cours.put(".3..", "习主席");
        //Multimap
        Multimap<String,String> teachers =ArrayListMultimap.create();
        //迭代器
        Iterator<Map.Entry<String,String>> it =cours.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String,String> entry =it.next();
            String key =entry.getKey(); //课程
            String value =entry.getValue(); //教师
            //教师 -->课程
            teachers.put(value, key);
        }
        //查看Multimap
        Set<String> keyset =teachers.keySet();
        for(String key:keyset){
            Collection<String> col =teachers.get(key);
            System.out.println(key+"-->"+col);
        }
        /*邓爷爷-->[改革开放]
               江主席-->[三个代表]
               习主席-->[.3.., ..2., .1..]
               胡主席-->[科学发展观, 八荣八耻, 和谐社会]*/
    }
}




import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
/**
 * HashMap 键唯一,值可以重复
 * BiMap:双向Map(Bidirectional Map ) 键与值都不能重复(unique -valued map)
 */
public class Demo07 {
    public static void main(String[] args) {
        BiMap<String,String> bimap=HashBiMap.create();
        bimap.put("bjsxt", "bjsxt@sina.com");
        bimap.put("good","good@qq.com");
        //通过邮箱找用户
        String user =bimap.inverse().get("good@qq.com");
        System.out.println(user);//good
        System.out.println(bimap.inverse().inverse()==bimap);//true
    }
}




import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import com.google.common.collect.Table.Cell;
import com.google.common.collect.Tables;
/**
 * 双键的Map -->Table -->rowKey+columnKey+value//类似于一个表格,2个作为key,另外一个作为value.
 * 1、方法
 *  所有的行数据:cellSet()
 *  所有的学生: rowKeySet()//查看其中一列
 *  所有的课程:columnKeySet()//查看另一列
 *  所有的成绩: values()//查看所有的value
 *  学生对应的课程: rowMap() +get(学生)
 *                  row(学生)
 *  课程对应的学生: columnMap +get(课程)
 *                  column(课程)
 */
public class Demo08 {
    public static void main(String[] args) {
        Table<String,String,Integer> tables=HashBasedTable.create();
        //测试数据
        tables.put("a", "javase", 80);
        tables.put("b", "javase", 90);
        tables.put("a", "oracle", 100);
        tables.put("c", "oracle", 95);

        //所有的行数据
        Set<Cell<String,String,Integer>> cells =tables.cellSet();
        for(Cell<String,String,Integer> temp:cells){
            System.out.println(temp.getRowKey()+"-->"+temp.getColumnKey()+"-->"+temp.getValue());
        }

        System.out.println("==========学生查看成绩==============");
        System.out.print("学生\t");
        //所有的课程
        Set<String> cours =tables.columnKeySet();
        for(String t:cours){
            System.out.print(t+"\t");
        }
        System.out.println();
        //所有的学生
        Set<String> stus =tables.rowKeySet();
        for(String stu:stus){
            System.out.print(stu+"\t");
            Map<String,Integer> scores =tables.row(stu);
            for(String c:cours){
                System.out.print(scores.get(c)+"\t");
            }
            System.out.println();
        }

        System.out.println("==========课程查看成绩==============");
        System.out.print("课程\t");
        //所有的学生
        Set<String> stuSet =tables.rowKeySet();
        for(String t:stuSet){
            System.out.print(t+"\t");
        }
        System.out.println();
        //所有的课程
        Set<String> courSet =tables.columnKeySet();
        for(String c:courSet){
            System.out.print(c+"\t");
            Map<String,Integer> scores =tables.column(c);
            for(String s:stuSet){
                System.out.print(scores.get(s)+"\t");
            }
            System.out.println();
        }
        System.out.println("===========转换===========");
        Table<String,String,Integer> tables2 =Tables.transpose(tables);
        //所有的行数据
        Set<Cell<String,String,Integer>> cells2 =tables2.cellSet();
        for(Cell<String,String,Integer> temp:cells2){
            System.out.println(temp.getRowKey()+"-->"+temp.getColumnKey()+"-->"+temp.getValue());
        }

    }

}





import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.PredicateUtils;
import org.apache.commons.collections4.functors.EqualPredicate;
import org.apache.commons.collections4.functors.NotNullPredicate;
import org.apache.commons.collections4.functors.UniquePredicate;
import org.apache.commons.collections4.list.PredicatedList;
/**
     函数式编程 之 Predicate 断言
  封装条件或判别式  if..else替代
  1、 new EqualPredicate<类型>(值) 
     EqualPredicate.equalPredicate(值);
  2、NotNullPredicate.INSTANCE 
  3、UniquePredicate.uniquePredicate()
  4、自定义
     new Predicate() +evaluate  
    PredicateUtils.allPredicate(可以传2个以上的断言器),andPredicate(只能传2个断言器),anyPredicate(多个断言器,只要其中一个 为true即可)
    PredicatedXxx.predicatedXxx(容器,判断)
 * @author Administrator
 *
 */
public class Demo01 {
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        System.out.println("======自定义判断======");
        //自定义的判别式
        Predicate<String> selfPre =new Predicate<String>(){
            @Override
            public boolean evaluate(String object) {
                return object.length()>=5 && object.length()<=20;
            }};
        Predicate notNull=NotNullPredicate.notNullPredicate();
        Predicate all =PredicateUtils.allPredicate(notNull,selfPre);//多个断言器
        List<String> list =PredicatedList.predicatedList(new ArrayList<String>(),all);//用这个断言器来限制容器。
        list.add("bjsxt");
        list.add(null);//报异常
        list.add("bj");//报异常
    }
    /**
     * 判断唯一
     */
    public static void unique(){
        System.out.println("====唯一性判断====");
        Predicate<Long> uniquePre =UniquePredicate.uniquePredicate();
        List<Long> list =PredicatedList.predicatedList(new ArrayList<Long>(), uniquePre);
        list.add(100L);
        list.add(200L);
        list.add(100L); //出现重复值,抛出异常
    }

    /**
     * 判断非空
     */
    public static void notNull(){
        System.out.println("====非空判断====");
        Predicate notNull0 = NotNullPredicate.INSTANCE;
        Predicate notNull = NotNullPredicate.notNullPredicate();
        //String str ="bjs";
        String str = null;
        System.out.println(notNull.evaluate(str)); //如果非空为true ,否则为false
        //添加容器值的判断
        List<Long> list =PredicatedList.predicatedList(new ArrayList<Long>(), notNull);//要求容器list不能添加null值。
        list.add(1000L);
        list.add(null); //验证失败,出现异常
    }

    /**
     * 比较相等判断
     */
    public static void equal(){
        System.out.println("======相等判断======");
        Predicate<String> pre0 =new EqualPredicate<String>("bjsxt");//实例化一个对象
        Predicate<String> pre =EqualPredicate.equalPredicate("bjsxt");//创建对象
        boolean flag =pre.evaluate("bj");//是否相等
        System.out.println(flag);
    }
}





import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.functors.SwitchTransformer;

/**
 解耦,业务处理与判断进行分类
 函数式编程 Transformer 类型转化
 1、new Transformer() +transform
 2、SwitchTransformer
 CollectionUtils.collect(容器,转换器)
 */
public class Demo02 {
    public static void main(String[] args) {
        System.out.println("===自定义类型转换==");
        //判别式
        Predicate<Employee> isLow=new Predicate<Employee>(){//匿名内部类
            @Override
            public boolean evaluate(Employee emp) {
                return emp.getSalary()<10000;
            }
        };
        Predicate<Employee> isHigh=new Predicate<Employee>(){//匿名内部类
            @Override
            public boolean evaluate(Employee emp) {
                return emp.getSalary()>=10000;
            }
        };
        Predicate[] pres ={isLow,isHigh};

        //转换,将Employee转成Level,
        Transformer<Employee,Level> lowTrans =new Transformer<Employee,Level>(){
            @Override
            public Level transform(Employee input) {
                return new Level(input.getName(),"卖身中");
            }};
        Transformer<Employee,Level> highTrans =new Transformer<Employee,Level>(){
            @Override
            public Level transform(Employee input) {
                return new Level(input.getName(),"养身中");
            }};
        Transformer[] trans ={lowTrans,highTrans};    
        //二者进行了关联
        Transformer switchTrans =new SwitchTransformer(pres, trans, null);
        //容器
        List<Employee> list =new ArrayList<Employee>();
        list.add(new Employee("老马",1000000));
        list.add(new Employee("老裴",999));
        Collection<Level> levelList = CollectionUtils.collect(list,switchTrans);//参数为容器和转换规则
        //遍历容器
        Iterator<Level> levelIt =levelList.iterator();
        while(levelIt.hasNext()){
            System.out.println(levelIt.next());
            /*(码农:老马,水平:养身中)
            (码农:老裴,水平:卖身中)*/
        }
    }
    /**
     * 内置类型的转换
     */
    public static void inner(){
        System.out.println("===内置类型转换  长整形时间日期,转成指定格式的字符串==");
        //类型转换器,将long类型转成String,
        Transformer<Long,String> trans =new Transformer<Long,String>(){
            @Override
            public String transform(Long input) {
                return new SimpleDateFormat("yyyy年MM月dd日").format(input);
            }};
        //容器
        List<Long> list =new ArrayList<Long>();    
        list.add(999999999999L);
        list.add(300000000L);
        //工具类 ,程序猿出钱---开发商---农民工出力
        Collection<String>  result=CollectionUtils.collect(list, trans);//将容器和类型转换器连接起来
        //遍历查看结果
        for(String time:result){
            System.out.println(time);
        }
    }
}

/**
 * 员工类
 * @author Administrator
 *
 */
public class Employee {
    private String name;
    private double salary;
    //alt +/
    public Employee() {
    }
    //alt+shift+s  o
    public Employee(String name, double salary) {
        super();
        this.name = name;
        this.salary = salary;
    }
    //alt+shift+s  +r tab 回车 shift+tab 回车
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }
    @Override
    public String toString() {
        return "(码农:"+this.name+",敲砖钱:"+this.salary+")";
    }

}

/**
 * 等级类
 * @author Administrator
 *
 */
public class Level {
    private String name;
    private String level;
    public Level() {
        // TODO Auto-generated constructor stub
    }
    public Level(String name, String level) {
        super();
        this.name = name;
        this.level = level;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLevel() {
        return level;
    }
    public void setLevel(String level) {
        this.level = level;
    }
    @Override
    public String toString() {
        return "(码农:"+this.name+",水平:"+this.level+")";
    }
}





import org.apache.commons.collections4.Closure;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.functors.ChainedClosure;
import org.apache.commons.collections4.functors.IfClosure;
import org.apache.commons.collections4.functors.WhileClosure;
/**
 函数式编程 Closure 闭包 封装特定的业务功能
 1、Closure
 2、IfClosure  IfClosure.ifClosure(断言,功能1,功能2)
 3、WhileClosure WhileClosure.whileClosure(断言,功能,标识) 
 4、ChainedClosure.chainedClosure(功能列表);
 CollectionUtils.forAllDo(容器,功能类对象);
 */
public class Demo03 {
    public static void main(String[] args) {
        basic();        
        ifClosure();
        whileClosure();
        chainClosure();
    }
    /**
     * 折上减   先打折商品,进行9折,满百再减20
     */
    public static void chainClosure(){
        List<Goods> goodsList =new ArrayList<Goods>();
        goodsList.add(new Goods("javase视频1",120,true));
        goodsList.add(new Goods("javaee视频2",100,false));
        goodsList.add(new Goods("高新技术视频",80,false));

        //满百减20
        Closure<Goods> subtract=new Closure<Goods>(){
            public void execute(Goods goods) {
                if(goods.getPrice()>=100){
                    goods.setPrice(goods.getPrice()-20);
                }
            }};
        //打折
        Closure<Goods> discount=new Closure<Goods>(){
            public void execute(Goods goods) {
                if(goods.isDiscount()){
                    goods.setPrice(goods.getPrice()*0.9);
                }
            }};    



        //链式操作
        Closure<Goods> chainClo=ChainedClosure.chainedClosure(discount,subtract);

        //关联
        CollectionUtils.forAllDo(goodsList,chainClo);

        //查看操作后的数据
        for(Goods temp:goodsList){
            System.out.println(temp);
            /*(商品:javase视频1,价格:88.0,是否打折:是)
            (商品:javaee视频2,价格:80.0,是否打折:否)
            (商品:高新技术视频,价格:80.0,是否打折:否)*/

        }

    }

    /**
     * 确保所有的员工工资都大于10000,如果已经超过的不再上涨
     */
    public static void whileClosure(){
        //数据
        List<Employee> empList =new ArrayList<Employee>();
        empList.add(new Employee("bjsxt",20000));
        empList.add(new Employee("is",10000));
        empList.add(new Employee("good",5000));

        //业务功能 每次上涨0.2 
        Closure<Employee> cols=new Closure<Employee>(){
            public void execute(Employee emp) {
                emp.setSalary(emp.getSalary()*1.2);
            }};

        //判断
        Predicate<Employee> empPre=new Predicate<Employee>(){
            @Override
            public boolean evaluate(Employee emp) {
                return emp.getSalary()<10000;
            }            
        };    
        //false 表示 while结构 先判断后执行   true do..while 先执行后判断
        Closure<Employee> whileCols =WhileClosure.whileClosure(empPre, cols, false);//empPre为true则进入cols,

        //工具类
        CollectionUtils.forAllDo(empList, whileCols)    ;

        //操作后的数据
        Iterator<Employee> empIt=empList.iterator();
        while(empIt.hasNext()){
            System.out.println(empIt.next());
            /*(码农:bjsxt,敲砖钱:20000.0)
            (码农:is,敲砖钱:10000.0)
            (码农:good,敲砖钱:10368.0)   涨到超过一万为止。*/    

        }
    }
    /**
     * 二选一  如果是打折商品,进行9折,否则满百减20
     */
    public static void ifClosure(){
        List<Goods> goodsList =new ArrayList<Goods>();
        goodsList.add(new Goods("javase视频1",120,true));
        goodsList.add(new Goods("javaee视频2",100,false));
        goodsList.add(new Goods("高新技术视频",80,false));

        //满百减20
        Closure<Goods> subtract=new Closure<Goods>(){
            public void execute(Goods goods) {
                if(goods.getPrice()>=100){
                    goods.setPrice(goods.getPrice()-20);
                }
            }};
        //打折
        Closure<Goods> discount=new Closure<Goods>(){
            public void execute(Goods goods) {
                if(goods.isDiscount()){
                    goods.setPrice(goods.getPrice()*0.9);
                }
            }};    

        //判断
        Predicate<Goods> pre=new Predicate<Goods>(){
            public boolean evaluate(Goods goods) {
                return goods.isDiscount();
            }}; 

        //二选一
        Closure<Goods> ifClo=IfClosure.ifClosure(pre,subtract,discount);

        //关联
        CollectionUtils.forAllDo(goodsList,ifClo);

        //查看操作后的数据
        for(Goods temp:goodsList){
            System.out.println(temp);
            /*(商品:javase视频,价格:108.0,是否打折:是)
            (商品:javaee视频,价格:80.0,是否打折:否)
            (商品:高新技术视频,价格:80.0,是否打折:否)*/
        }
    }
    /**
     * 基本操作
     */
    public static void basic(){
        //数据
        List<Employee> empList =new ArrayList<Employee>();
        empList.add(new Employee("bjsxt",20000));
        empList.add(new Employee("is",10000));
        empList.add(new Employee("good",5000));

        //业务功能
        Closure<Employee> cols=new Closure<Employee>(){//匿名内部类对象
            public void execute(Employee emp) {
                emp.setSalary(emp.getSalary()*1.2);//工资加倍
            }};

        //工具类
        CollectionUtils.forAllDo(empList, cols)    ;

        //操作后的数据
        Iterator<Employee> empIt=empList.iterator();
        while(empIt.hasNext()){
            System.out.println(empIt.next());
            /*(码农:bjsxt,敲砖钱:24000.0)
            (码农:is,敲砖钱:12000.0)
            (码农:good,敲砖钱:6000.0)*/
        }
    }

}


public class Goods {
    private String name;
    private double price;
    //折扣
    private boolean discount;
    public Goods() {
        // TODO Auto-generated constructor stub
    }
    public Goods(String name, double price, boolean discount) {
        super();
        this.name = name;
        this.price = price;
        this.discount = discount;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public boolean isDiscount() {
        return discount;
    }
    public void setDiscount(boolean discount) {
        this.discount = discount;
    }

    @Override
    public String toString() {
        return "(商品:"+this.name+",价格:"+this.price+",是否打折:"+(discount?"是":"否")+")";
    }
}





import org.apache.commons.collections4.CollectionUtils;
/**
 * 集合操作
 * 1、并集
 * CollectionUtils.union();
 * 2、交集
 * CollectionUtils.intersection();
 * CollectionUtils.retainAll();
 * 3、差集
 *  CollectionUtils.subtract();
 */
public class Demo04 {
    public static void main(String[] args) {
        Set<Integer> set1 =new HashSet<Integer>();
        set1.add(1);
        set1.add(2);
        set1.add(3);

        Set<Integer> set2 =new HashSet<Integer>();
        set2.add(2);
        set2.add(3);
        set2.add(4);

        //并集
        System.out.println("=========并集============");
        Collection<Integer> col =CollectionUtils.union(set1,set2);
        for(Integer temp:col){
            System.out.println(temp);
        }
        //交集
        System.out.println("=========交集============");
        //col =CollectionUtils.intersection(set1, set2);
        col =CollectionUtils.retainAll(set1, set2);
        for(Integer temp:col){
            System.out.println(temp);
        }
        //差集
        System.out.println("=========差集============");
        col =CollectionUtils.subtract(set1, set2);
        for(Integer temp:col){
            System.out.println(temp);
        }        
    }
}




队列:
import java.util.Queue;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.functors.NotNullPredicate;
import org.apache.commons.collections4.queue.CircularFifoQueue;
import org.apache.commons.collections4.queue.PredicatedQueue;
import org.apache.commons.collections4.queue.UnmodifiableQueue;
/**
 Queue队列(先进先出),栈(后进先出)
 1、循环队列:CircularFifoQueue
 2、只读队列:不可改变队列  UnmodifiableQueue
 3、断言队列:PredicatedQueue.predicatedQueue()
 */
public class Demo05 {
    public static void main(String[] args) {
        circular();
        readOnly();
        predicate();
    }
    /**
     * 断言队列
     */
    public static void predicate(){
        //循环队列
        CircularFifoQueue<String> que =new CircularFifoQueue<String>(2);
        que.add("a");
        que.add("b");
        que.add("c");
        Predicate notNull=NotNullPredicate.INSTANCE;
        //包装成对应的队列
        Queue<String> que2=PredicatedQueue.predicatedQueue(que, notNull);
        que2.add(null);
    }
    /**
     * 只读队列
     */
    public static void readOnly(){
        //循环队列
        CircularFifoQueue<String> que =new CircularFifoQueue<String>(2);
        que.add("a");
        que.add("b");
        que.add("c");
        Queue<String> readOnlyQue =UnmodifiableQueue.unmodifiableQueue(que);
        readOnlyQue.add("d");
    }
    /**
     * 循环队列
     */
    public static void circular(){
        //循环队列
        CircularFifoQueue<String> que =new CircularFifoQueue<String>(2);
        que.add("a");
        que.add("b");
        que.add("c");
        //查看
        for(int i=0;i<que.size();i++){
            System.out.println(que.get(i));
        }
    }
}



import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.IterableMap;
import org.apache.commons.collections4.MapIterator;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.apache.commons.collections4.iterators.ArrayListIterator;
import org.apache.commons.collections4.iterators.FilterIterator;
import org.apache.commons.collections4.iterators.LoopingIterator;
import org.apache.commons.collections4.iterators.UniqueFilterIterator;
import org.apache.commons.collections4.map.HashedMap;

/**
 迭代器的扩展
 1、MapIterator 以后不再使用map.keySet.iterator访问
  接口IterableMap,实现类 HashedMap
 2、UniqueFilterIterator 去重迭代器 
 3、FilterIterator 自定义过滤 +Predicate
 4、LoopingIterator 循环迭代器
 5、ArrayListIterator 数组迭代器
 */
public class Demo06 {
    public static void main(String[] args) {
        mapIt();
        uniqueIt();
        filterIt();
        loopIt();
        arrayIt();
    }
    /**
     * 数组迭代器
     */
    public static void arrayIt(){
        System.out.println("===== 数组迭代器  ====");
        int[] arr ={1,2,3,4,5};
        //数组迭代器
        Iterator<Integer> it0 =new ArrayListIterator<Integer>(arr);
        //指定起始索引和结束索引
        Iterator<Integer> it =new ArrayListIterator<Integer>(arr,1,3);
        while(it.hasNext()){
            System.out.println(it.next());//2,3
        }
    }
    /**
     * 循环迭代器
     */
    public static void loopIt(){
        System.out.println("===== 循环迭代器  ====");
        List<String> list =new ArrayList<String>();
        list.add("refer");
        list.add("dad");
        list.add("bjsxt");
        list.add("moom");

        Iterator<String> it =new LoopingIterator(list);
        for(int i=0;i<8;i++){
            System.out.println(it.next());
            /*refer
            dad
            bjsxt
            moom
            refer
            dad
            bjsxt
            moom*/
        }
    }
    /**
     * 自定义迭代器 
     */
    public static void filterIt(){
        System.out.println("=====自定义迭代器  ====");
        List<String> list =new ArrayList<String>();
        list.add("refer");
        list.add("dad");
        list.add("bjsxt");
        list.add("moom");
        //自定义条件判断
        Predicate<String> pre =new Predicate<String>(){
            public boolean evaluate(String value) {
                //回文判断
                return new StringBuilder(value).reverse().toString().equals(value);
            }};
        //去除重复的过滤器
        Iterator<String> it =new FilterIterator(list.iterator(),pre);
        while(it.hasNext()){
            System.out.println(it.next());
                /*refer
                dad
                moom*/
        }
    }
    /**
     * 去重迭代器 
     */
    public static void uniqueIt(){
        System.out.println("=====去重迭代器 ====");
        List<String> list =new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("a");
        //去除重复的过滤器
        Iterator<String> it =new UniqueFilterIterator(list.iterator());
        while(it.hasNext()){
            System.out.println(it.next());
            /*a
            b*/
        }
    }
    /**
     * map迭代器
     */
    public static void mapIt(){
        System.out.println("=====map迭代器====");
        IterableMap<String,String> map =new HashedMap<String,String>();
        map.put("a","bjsxt");
        map.put("b", "sxt");
        map.put("c", "good");
        //使用 MapIterator
        MapIterator<String,String> it =map.mapIterator();
        while(it.hasNext()){
            String key =it.next();
            String value =it.getValue();
            System.out.println(key+"-->"+value);
            /*a-->bjsxt
            c-->good
            b-->sxt*/
        }
    }
}





import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.MapIterator;
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
import org.apache.commons.collections4.bidimap.DualTreeBidiMap;
/**
 双向Map 要求键与值都不能重复
 接口BidiMap  inverseBidiMap()
 实现类1、DualTreeBidiMap :有序
 实现类2、DualHashBidiMap :无序
 */
public class Demo07 {
    public static void main(String[] args) {
        hashMap();
        treeMap();
    }
    /**
     * 有序的双向Map(key会自动排列)
     */
    public static void treeMap(){
        System.out.println("=====有序的双向Map====");
        BidiMap<String,String> map =new DualTreeBidiMap<String,String>();
        map.put("zbj", "bj@test.com");
        map.put("sxt", "sxt@qq.com");//{sxt=sxt@qq.com, zbj=bj@test.com}
        //遍历查看
        MapIterator<String,String> it =map.inverseBidiMap().mapIterator();//{bj@test.com=zbj, sxt@qq.com=sxt}
        while(it.hasNext()){
            String key =it.next();
            String value =it.getValue();
            System.out.println(key+"-->"+value);
            //bj@test.com-->zbj
            //sxt@qq.com-->sxt
        }
    }

    /**
     * 无序的双向Map
     */
    public static void hashMap(){
        System.out.println("=====无序的双向Map====");
        BidiMap<String,String> map =new DualHashBidiMap<String,String>();
        map.put("bj", "bj@test.com");
        map.put("sxt", "sxt@qq.com");//{sxt=sxt@qq.com, bj=bj@test.com}
        //反转
        System.out.println(map.inverseBidiMap().get("sxt@qq.com"));//先要反转,然后通过value找到key,sxt
        //遍历查看
        MapIterator<String,String> it =map.inverseBidiMap().mapIterator();//{sxt@qq.com=sxt, bj@test.com=bj}
        while(it.hasNext()){
            String key =it.next();
            String value =it.getValue();
            System.out.println(key+"-->"+value);
            /*sxt@qq.com-->sxt
            bj@test.com-->bj*/
        }
    }
}





import org.apache.commons.collections4.Bag;
import org.apache.commons.collections4.bag.HashBag;
import org.apache.commons.collections4.bag.TreeBag;

/**
 接口Bag 包 允许重复
 实现类1、HashBag 无序
 实现类2、TreeBag 有序
 统计单词的出现次数
 */
public class Demo08 {

    /**
     * @param args
     */
    public static void main(String[] args) {
        hashBag();
        treeBag();
        String str ="this is a cat and that is a mice where is the food";
        //分割字符串
        String[] strArray =str.split(" ");
        Bag<String> bag =new TreeBag<String>();
        for(String temp:strArray){
            bag.add(temp);//[2:a,1:and,1:cat,1:food,3:is,1:mice,1:that,1:the,1:this,1:where]
        }

        System.out.println("====统计次数===");
        Set<String> keys =bag.uniqueSet();//[a, and, cat, food, is, mice, that, the, this, where]
        for(String letter:keys){
            System.out.println(letter+"-->"+bag.getCount(letter));
            /*a-->2
            and-->1
            cat-->1
            food-->1
            is-->3
            mice-->1
            that-->1
            the-->1
            this-->1
            where-->1*/
        }
    }
    /**
     * 有序
     */
    public static void treeBag(){
        System.out.println("=====有序的包====");
        Bag<String> bag =new TreeBag<String>();
        bag.add("a");
        bag.add("a",5);
        bag.remove("a", 2);
        bag.add("b");
        bag.add("c");//[4:a,1:b,1:c],有序
        Iterator<String> it =bag.iterator();
        while(it.hasNext()){
            System.out.println(it.next());//aaaabc
        }
    }

    /**
     * 无序
     */
    public static void hashBag(){
        System.out.println("=====无序的包====");
        Bag<String> bag =new HashBag<String>();
        bag.add("a");
        bag.add("a",5);//加5次a,[6:a]
        bag.remove("a", 2);//移除2个a,[4:a]
        bag.add("b");
        bag.add("c");//[1:b,1:c,4:a],一个b,一个c,4个a,
        Iterator<String> it =bag.iterator();
        while(it.hasNext()){
            System.out.println(it.next());//bcaaaa
        }
    }

}




总结:
set没有顺序,顺序指的是索引的顺序不是指内容。
1.迭代器:Iterator,
2.比较器:实体类可以排序(实现Comparable重写compareTo),还可以用排序比较器(实现Comparator重写compare),TreeSet、TreeMap.
3.泛型:反泛型类,泛型方法,泛型接口,泛型擦出,通配符?
4.6个接口:Collection,set,List,Map,Iterator,Comparable
5.9个常用类:
1)
查看多余修改时推荐使用ArrayList(add,remove,set(修改),get,foreach,)。
2)
修改多余查看推荐使用LinkList,多了链头与链尾的方法。

3)HashSet:元素不能重复,所以要求元素要重写hashCode和equals方法.
4)Treeset:要求元素可以排序或者提供排序的业务类。
5)HashMap:键不能重复必须重写hashCode和equals方法,值可以重复,put(),remove(),get().
6)Properties:资源配置文件,
7)Hashtable:键与值都不能为null,
8)Stack:栈
9)Collections:工具类

HashSet 一种没有重复元素的无序集合

4.TreeSet   有序集

并行向量(Parallel Vector)


一个
ParVector
是一个不可变序列,具有低常量因子对数的访问(low-constant factor
logarithmic access )和更新时间。

scala> val pv = scala.collection.parallel.immutable.ParVector.tabulate(1000)(x =

> x)

pv: scala.collection.parallel.immutable.ParVector[Int] = ParVector(0, 1, 2, 3, 4

, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,

 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,

 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,

 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,

 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104

, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120

, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136

, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152

, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, ...

 

scala> pv filter (_ %2==0)

res2: scala.collection.parallel.immutable.ParVector[Int] = ParVector(0, 2, 4, 6,

 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46,

48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86,

88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 1

22, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 1

54, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 1

86, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 2

18, 220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 2

50, 252, 254, 256, 258, 260, 262, 264, 266, 268, 270, 272, 274, 276, 278, 280, 2

82, 284, 286, 288, 290, 292, 294, 296, 298, 300, 302, 304, 306, 308, 310, 312...

 

scala>

 

TreeSet 一种有序集

5.HashMap  键值对关联的数据结构

并行范围(Parallel Range)


一个
ParRange
是一个有序的等差整数数列(an ordered sequence of elements equally spaced
apart)。并行范围(parallel range)的创建与顺序范围类似(sequential
Range)。

scala> 1 to 3 par

warning: there were 1 feature warning(s); re-run with -feature for details

res3: scala.collection.parallel.immutable.ParRange = ParRange(1, 2, 3)

 

scala> 15 to 5 by -2 par

warning: there were 1 feature warning(s); re-run with -feature for details

res4: scala.collection.parallel.immutable.ParRange = ParRange(15, 13, 11, 9, 7,

5)

 

scala>

LinkHashSet 一种可以记住元素插入次序的集合

6.TreeMap  键值有序排列的映射表

并行哈希表(Parallel Hash Tables)


并行哈希表(Parallel hash
tables)存储底层数组的元素,并将它们放置在由各自元素哈希码的位置。并行可变哈希集(mutable.ParHashSet)和并行可变哈希映射(mutable.ParHashMap)都是基于哈希表。

scala> val phs = scala.collection.parallel.mutable.ParHashSet(1 until 2000: _*)

phs: scala.collection.parallel.mutable.ParHashSet[Int] = ParHashSet(307, 1705, 3

67, 1007, 1954, 1067, 1316, 1033, 707, 1980, 335, 1093, 395, 1342, 1016, 644, 12

65, 35, 704, 95, 1042, 344, 1291, 1044, 404, 1991, 1351, 653, 1353, 1602, 1662,

1379, 1053, 41, 1628, 1302, 1688, 1362, 1079, 381, 1328, 441, 1388, 690, 1637, 9

39, 1390, 1697, 52, 999, 18, 1699, 639, 78, 1665, 1339, 327, 1, 1725, 1399, 1648

, 1365, 27, 1708, 1425, 727, 1674, 976, 1734, 89, 1036, 1983, 676, 115, 736, 38,

 985, 287, 1045, 1685, 347, 64, 1745, 313, 1711, 1385, 373, 1013, 1771, 1073, 13

22, 1382, 773, 75, 1022, 1969, 324, 1082, 384, 1657, 1331, 633, 350, 24, 693, 41

0, 84, 1357, 659, 333, 50, 1731, 719, 393, 110, 359, 1059, 419, 361, 668, 1119,

421, 1368, 728, 670, 1617, 730, 1677, 1394, 979, 696, 370, 1643, 1317, 756, 4...

 

scala> phs map (x => x * x)

res5: scala.collection.parallel.mutable.ParHashSet[Int] = ParHashSet(11236, 2563

201, 1957201, 143641, 227529, 3556996, 1214404, 1946025, 670761, 2181529, 219024

, 1648656, 2062096, 2152089, 343396, 2418025, 1582564, 440896, 925444, 312481, 3

526884, 2775556, 3359889, 175561, 35721, 84681, 2244004, 20164, 2102500, 576081,

 1557504, 338724, 952576, 300304, 1030225, 3139984, 687241, 1227664, 2627641, 35

75881, 51984, 851929, 94249, 2505889, 3972049, 788544, 1459264, 2937796, 2920681

, 2446096, 413449, 59536, 690561, 306916, 441, 2647129, 5929, 1054729, 746496, 3

392964, 3207681, 2989441, 2547216, 180625, 1868689, 166464, 33124, 237169, 25856

64, 1, 3625216, 57600, 99225, 315844, 251001, 238144, 32761, 595984, 1194649, 12

18816, 676, 1464100, 797449, 2131600, 1527696, 2383936, 786769, 3697929, 3222...

 

scala>

PriorityQueue 一种可以高效删除最小元素的集合

7.  EunmMap  枚举类型的映射表

并行散列 Tries(Parallel Hash Tries)


并行哈希 tries(Parallel hash tries )是不可变哈希 tries(immutable hash
tries)的并行版本,它用来高效地表示不可变集(immutable
sets)和映射(immutable maps)。他们由
immutable.ParHashSet

immutable.ParHashMap
支持。

scala> val phs = scala.collection.parallel.immutable.ParHashSet(1 until 1000: _

)

phs: scala.collection.parallel.immutable.ParHashSet[Int] = ParSet(645, 892, 69,

809, 629, 365, 138, 760, 101, 479, 347, 846, 909, 333, 628, 249, 893, 518, 962,

468, 234, 941, 777, 555, 666, 88, 481, 352, 408, 977, 170, 523, 582, 762, 115,

83, 730, 217, 276, 994, 308, 741, 5, 873, 449, 120, 247, 379, 878, 440, 655, 51

, 614, 269, 677, 202, 597, 861, 10, 385, 384, 56, 533, 550, 142, 500, 797, 715,

472, 814, 698, 747, 913, 945, 340, 538, 153, 930, 670, 829, 174, 404, 898, 185,

42, 782, 709, 841, 417, 24, 973, 885, 288, 301, 320, 565, 436, 37, 25, 651, 257

 389, 52, 724, 14, 570, 184, 719, 785, 372, 504, 110, 587, 619, 838, 917, 702,

51, 802, 125, 344, 934, 357, 196, 949, 542, 460, 157, 817, 902, 559, 638, 853,

89, 20, 421, 870, 46, 969, 93, 606, 284, 770, 881, 416, 325, 152, 228, 289, 4..

 

scala> phs map { x => x * x } sum

warning: there were 1 feature warning(s); re-run with -feature for details

res6: Int = 332833500

 

scala>

HashMap 一种存储key:value关联的映射

8.EnumSet   包含枚举类型值的值

并行并发 tries(Parallel Concurrent Tries)


concurrent.TrieMap
是一个并发线程安全的映射(map),而mutable.ParTrieMap
是它的并行版本。若果数据结构在遍历期间被修改,那么大多数并发数据结构不能保证一致性,Ctries
保证在下一次迭代中更新是可见的。这意味着,当你遍历是,可以改变并发
trie,如下例子所示,输出1到99的平方根。

scala> val numbers = scala.collection.parallel.mutable.ParTrieMap((1 until 100)

zip (1 until 100): _*) map {case(k, v)=>(k.toDouble, v.toDouble)}

numbers: scala.collection.parallel.mutable.ParTrieMap[Double,Double] = ParTrieMa

p(15.0 -> 15.0, 51.0 -> 51.0, 33.0 -> 33.0, 48.0 -> 48.0, 84.0 -> 84.0, 30.0 ->

30.0, 66.0 -> 66.0, 12.0 -> 12.0, 27.0 -> 27.0, 9.0 -> 9.0, 99.0 -> 99.0, 81.0 -

> 81.0, 63.0 -> 63.0, 45.0 -> 45.0, 78.0 -> 78.0, 60.0 -> 60.0, 96.0 -> 96.0, 19

.0 -> 19.0, 1.0 -> 1.0, 37.0 -> 37.0, 52.0 -> 52.0, 88.0 -> 88.0, 34.0 -> 34.0,

70.0 -> 70.0, 16.0 -> 16.0, 67.0 -> 67.0, 13.0 -> 13.0, 49.0 -> 49.0, 85.0 -> 85

.0, 31.0 -> 31.0, 82.0 -> 82.0, 64.0 -> 64.0, 46.0 -> 46.0, 5.0 -> 5.0, 56.0 ->

56.0, 2.0 -> 2.0, 38.0 -> 38.0, 74.0 -> 74.0, 20.0 -> 20.0, 89.0 -> 89.0, 71.0 -

> 71.0, 17.0 -> 17.0, 53.0 -> 53.0, 35.0 -> 35.0, 86.0 -> 86.0, 68.0 -> 68.0, 32

.0 -> 32.0, 50.0 -> 50.0, 83.0 -> 83.0, 6.0 -> 6.0, 42.0 -> 42.0, 24.0 -> 24....

 

scala> while(numbers.nonEmpty){

     | numbers foreach{case(num, sqrt)=>

     | val nsqrt =0.5*(sqrt + num / sqrt)

     | numbers(num)= nsqrt

     | if(math.abs(nsqrt - sqrt)<0.01){

     | println(num, nsqrt)

     | numbers.remove(num)

     | }

     | }

     | }

(1.0,1.0)

(2.0,1.4142156862745097)

(5.0,2.2360688956433634)

(6.0,2.4494943716069653)

(3.0,1.7320508100147274)

(7.0,2.64576704419029)

(4.0,2.0000000929222947)

(15.0,3.872983698008724)

(12.0,3.4641016533502986)

(9.0,3.000000001396984)

(19.0,4.358901750853372)

(16.0,4.000000636692939)

(13.0,3.6055513629176015)

(20.0,4.4721402170657)

……

 

scala>

TreeMap 一种key有序的映射

9.ArrayQueue  循环数组实现的双端队列

性能特征


顺序类型(sequence types)的性能特点:

  head tail apply update prepend append insert
ParArray C L C C L L L
ParVector eC eC eC eC eC eC
ParRange C C C

集(set)和映射(map)类型的性能特点:

  lookup add remove
immutable      
ParHashSet/ParHashMap eC eC eC
mutable      
ParHashSet/ParHashMap C C C
ParTieMap eC eC eC

LinkedHashMap 一种可以记住插入次序的映射

10.PriorityQueue  允许高效删除最小元素的集合

键(Key)

上面两个表的条目,说明如下:

该操作花费常量时间(快)
eC 该操作有效地花费常量时间,但可能依赖于某些假设,如向量的最大长度或哈希键的离散性
aC 该操作花费分期常量时间。Some invocations of the operation might take longer, but if many operations are performed on average only constant time per operation is taken.
Log 该操作花费时间与集合大小的对数成比例
L 该操作是线性的,花费的时间与集合大小成比例
该操作不被支持

下表处理序列类型——可变和不可变——具备如下操作:

head 选择序列的第一个元素
tail 产生一个由除了第一个元素的所有元素组成的新序列
apply 索引
update 对于不可变序列(immutable sequence)的函数式更新,对于可变序列(mutable sequences)的副作用(side effect)更新
prepend 添加一个元素到序列前面。针对不可变序列,这将产生一个新序列。针对可变序列,这将修改已经存在的序列
append 添加一个元素到序列尾部。针对不可变的序列,这将产生一个新序列,针对可变序列,这将修改已经存在的序列。
insert 在序列中的任意位置插入一个元素。只支持可变序列(mutable sequence)

下表处理可变和不可变集(set)和映射(map)具有如下操作:

lookup 测试一个元素是否包含在集(set)中,或选择与键有关的值
add 添加一个元素到集(set),或添加键/值对到映射(map)
remove 从集(set)或删除一个元素,或从映射(map)删除一个键
min 集(set)中最小的元素,或映射(map)中最小的键

11.LinkedHashMap  可以记住键值添加次序的映射表

参考资料


  • Scala 官网

12.WeakHashMap  可以被垃圾回收期回收的映射表

13.IdentityHashMap   用==而不是equals比较键值的映射表

14.LinkedHashSet  可以记住插入次序的集合

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图