当前位置: 移动技术网 > IT编程>开发语言>Java > Java常用小笔记

Java常用小笔记

2018年12月07日  | 移动技术网IT编程  | 我要评论

 

1.对list集合进行分页

     //startindex指的是开始的小标 从0开始,pagesize是每页记录数
        int toindex = new integer(startindex)+new integer(pagesize) ;
        list<projectworkitemassignmentvo> listpage = volist.sublist(new integer(startindex),toindex>volist.size() ? volist.size():toindex);

 

2.collections对list集合按照指定属性进行排序

       //时间由新到旧进行排列 就是时间由大到小
            collections.sort(volist, new comparator<projectworkitemassignmentvo>() {
                /**
                 * @param o1
                 * @param o2
                 * @return 返回负数表示:o1 大于o2,
                 * 返回0 表示:o1和o2相等,
                 * 返回正数表示:o1小于o2
                 */
                @override
                public int compare(projectworkitemassignmentvo o1, projectworkitemassignmentvo o2) {
                    if(o1.getstime().compareto(o2.getstime()) == 1){
                        return -1;
                    }

                    if(o1.getstime().compareto(o2.getstime()) == 0){
                        return 0;
                    }
                    return 1;
                }
            });
            system.out.println("排序后volist---" + volist);

      //时间由旧到新进行排列 就是时间由小到大
            collections.sort(volist, new comparator<projectworkitemassignmentvo>() {
                /**
                 * @param o1
                 * @param o2
                 * @return 返回正数表示:o1 大于o2,
                 * 返回0 表示:o1和o2相等,
                 * 返回负数表示:o1小于o2
                 */
                @override
                public int compare(projectworkitemassignmentvo o1, projectworkitemassignmentvo o2) {
                      return o1.getetime().compareto(o2.getetime());
                }
            });


       //完成度由低至高  升序
            collections.sort(volist, new comparator<projectworkitemassignmentvo>() {
                /**
                 * 由低到高排序
                 * @param o1
                 * @param o2
                 * @return 返回负数表示:o1 小于o2,
                 * 返回0 表示:o1和o2相等,
                 * 返回正数表示:o1大于o2
                 */
                @override
                public int compare(projectworkitemassignmentvo o1, projectworkitemassignmentvo o2) {
                    if (o1.getcompletion() > o2.getcompletion()) {
                        return 1;
                    }
                    if (o1.getcompletion() == o2.getcompletion()) {
                        return 0;
                    }
                    return -1;
                }
            });

  对于平常数字的排序,只需要比较大下返回对应的正数或者负数来决定按照什么顺序,对于时间日期,需要调用compareto()方法做比较,具体可以详看对应的api源码

 

3.java获取指定字符串,一个字符串中有多个相同的

//截取0到倒数第三个/位置之间的字符串,黄色标注部分
string actionrecord =  "2018-09-08 14:59:42[start时间]||2018-09-08 15:40:48[pause时间]||";
//获取最后一个|的下标
int index = actionrecord.lastindexof("|");
//在0-index-1范围内,查找最后一个匹配的的字符串的位置
index = actionrecord.lastindexof("|",index-1);
//在0-index-1范围内,查找最后一个匹配的字符串的位置
index = actionrecord.lastindexof("|",index-1);
//截取两者之间的字符串
string temp = actionrecord.substring(0,index+1);

  参考连接: 

 

4.判断一个时间段是否与另一个时间段重合

//判断时间段time1~time2是否与时间段fromtime~totime重合
if(time1 >= fromtime && time1 < totime || fromtime >= time1 && fromtime < time2){
   //重合
}

 

5.使用set对list集合去重

//利用set 对list去重复
set<spcustomer> set = new hashset<spcustomer>(templist);
//使用去重复后的set 重新构建一个list
list<spcustomer> templist2 = new arraylist<spcustomer>(set);

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网