zoukankan      html  css  js  c++  java
  • Java 技术笔记

    vlist提取字段生成新list

    List<int> uidList = urResult.stream().map(p -> p.getUserId()).collect(Collectors.toList());

    vList中根据某个字段条件生成新list

    List<User> temp = uList.stream().filter(item -> item.getUid() == 10000).collect(Collectors.toList());

    v去除List符合条件的元素

    Optional<Ticket> oTicket = topicList.stream().filter(p -> p.getId().equals(to.getId())).findFirst();
                            if (oTopic.isPresent()) {
                                Ticket tempTicket = oTicket.get();
                            }

    vMybatis批量修改

    <update id="updateBatch"  parameterType="java.util.List">  
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            update course
            <set>
                name=${item.name}
            </set>
            where id = ${item.id}
        </foreach>      
    </update>

    vMybatis实现InsertOrUpdate

    <insert id="saveOrUpdate" >
      <selectKey keyProperty="count" resultType="int" order="BEFORE">
        select count(*) from country where id = #{id}
      </selectKey>
      <if test="count > 0">
        update country 
        set countryname = #{countryname},countrycode = #{countrycode} 
        where id = #{id}
      </if>
      <if test="count==0">
        insert into country values(#{id},#{countryname},#{countrycode})
      </if>
    </insert>

    条件限制

    根据不同的判断逻辑,会有所不同,就上面这个例子而言,就要求实体类中包含count属性(可以是别的名字)。否则selectKey的结果没法保存,如果入参是个Map类型,就没有这个限制。

    说明

    从例子来看除了有个限制外,也没别的麻烦。

    通过selectKey做第一次查询,然后根据结果进行判断,所以这里的order="BEFORE"是必须的。

    也是因为BEFORE,所以没法通过<bind>标签来临时存储中间的值,只能在入参中增加属性来存放。

    v异步线程Thread

    new Thread(){
                public void run() {
                    // 方法体
                }
            }.start();

    vJava计算代码执行时间

    long startTime = System.currentTimeMillis();
    long endTime = System.currentTimeMillis();
    float seconds = (endTime - startTime) / 1000F;
    System.out.println("Cost seconds" + Float.toString(seconds));
    Stopwatch stopwatch = Stopwatch.createStarted();
    // Todo 业务逻辑
    stopwatch.stop();
    log.info("cost [%d] second. (﹁.﹁))", stopwatch.elapsed(TimeUnit.SECONDS));

    v随机指定范围内N个不重复的数

    /** 
         * 随机指定范围内N个不重复的数 
         * 最简单最基本的方法 
         * @param min 指定范围最小值 
         * @param max 指定范围最大值 
         * @param n 随机数个数 
         */  
        public static int[] getRandomList(int min, int max, int n){  
            if (n > (max - min + 1) || max < min) {  
                   return null;  
               }  
            int[] result = new int[n];  
            int count = 0;  
            while(count < n) {  
                int num = (int) (Math.random() * (max - min)) + min;  
                boolean flag = true;  
                for (int j = 0; j < n; j++) {  
                    if(num == result[j]){  
                        flag = false;  
                        break;  
                    }  
                }  
                if(flag){  
                    result[count] = num;  
                    count++;  
                }  
            }  
            return result;  
        }

    v逗号分隔字符串转List

    String categorys = "12,34,54,64,6,4,32";
    List<Integer> listIds = Arrays.asList(categorys.split(","))
                        .stream().map(s -> Integer.parseInt(s.trim()))
                        .collect(Collectors.toList());
    listIds.forEach(s ->System.out.print(s+" "));
    //结果:12 34 54 64 6 4 32 

    vidList排序获得的结果集modelList

            // ********  造点数据  ********
            List<User> userList = new ArrayList<>();
            for(int i = 0; i < 5; i++){
                User user = new User();
                user.setId(i);
                user.setName(i+"aaaaa"+i);
                userList.add(user);
            }
    
            List<Integer> idList = new ArrayList<>();
            idList.add(4);
            idList.add(1);
            idList.add(3);
            idList.add(4);
            idList.add(5);
            idList.add(2);
            idList.add(3);
    
            // ********  关键部分  ********
            idList = idList.stream().distinct().collect(Collectors.toList());
            if(idList.size() > 1) {
                Dictionary<Integer, User> dictionary = new Hashtable<>();
                userList.forEach(item ->
                        dictionary.put(item.getId(), item)
                );
    
                userList = new ArrayList<>();
                for(int i = 0; i < idList.size(); i++){
                    userList.add(dictionary.get(idList.get(i)));
                }
            }
  • 相关阅读:
    CentOS7防火墙
    [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
    二叉树系列
    二叉树系列
    [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
    动态规划小结
    [LeetCode] Populating Next Right Pointers in Each Node I, II
    [LeetCode] 递推思想的美妙 Best Time to Buy and Sell Stock I, II, III O(n) 解法
    二叉树系列
    [LeetCode] 数组的最长连续数, O(n)解法
  • 原文地址:https://www.cnblogs.com/toutou/p/6567728.html
Copyright © 2011-2022 走看看