Java common notes

Posted by bloodl on Sat, 07 Dec 2019 14:39:15 +0100

1. Paging the list set

     //startIndex It means that the start of the small label starts from 0, pageSize Is the number of records per page
        int toIndex = new Integer(startIndex)+new Integer(pageSize) ;
        List<ProjectWorkItemAssignmentVO> listPage = voList.subList(new Integer(startIndex),toIndex>voList.size() ? voList.size():toIndex);

 

2.Collections sorts the list collection according to the specified properties

       //The arrangement of time from new to old isTime from big to small
            Collections.sort(voList, new Comparator<ProjectWorkItemAssignmentVO>() {
                /**
                 * @param o1
                 * @param o2
                 * @return A negative number returned indicates that o1 is greater than o2,
                 * Returning 0 means that o1 and o2 are equal,
                 * Returning a positive number indicates that o1 is less than 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("After sorting voList---" + voList);

      //The arrangement of time from old to new is timeFrom small to large
            Collections.sort(voList, new Comparator<ProjectWorkItemAssignmentVO>() {
                /**
                 * @param o1
                 * @param o2
                 * @return Returns a positive number to indicate that o1 is greater than o2,
                 * Returning 0 means that o1 and o2 are equal,
                 * Return a negative number to indicate that o1 is less than o2
                 */
                @Override
                public int compare(ProjectWorkItemAssignmentVO o1, ProjectWorkItemAssignmentVO o2) {
                      return o1.geteTime().compareTo(o2.geteTime());
                }
            });


       //Completion degreeAscending order from low to high
            Collections.sort(voList, new Comparator<ProjectWorkItemAssignmentVO>() {
                /**
                 * Sort from low to high
                 * @param o1
                 * @param o2
                 * @return A negative number returned indicates that o1 is less than o2,
                 * Returning 0 means that o1 and o2 are equal,
                 * Returning a positive number indicates that o1 is greater than 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;
                }
            });

For the sorting of ordinary numbers, you only need to return the corresponding positive or negative numbers to determine the order. For the time and date, you need to call the compareTo() method for comparison. For details, see the corresponding api source code

 

3.java gets the specified string. There are multiple identical strings in a string

//Intercept 0 to the third from the bottom/String between positions, yellow callout section
String actionRecord =  "2018-09-08 14:59:42[Start time]||2018-09-08 15:40:48[Pause time]||";
//Get the last|Subscript
int index = actionRecord.lastIndexOf("|");
//In the 0-index-1 range, find the location of the last matching string
index = actionRecord.lastIndexOf("|",index-1);
//In the 0-index-1 range, find the location of the last matching string
index = actionRecord.lastIndexOf("|",index-1);
//Intercept the string between the two
String temp = actionRecord.substring(0,index+1);

Reference connection: https://www.cnblogs.com/dujinyang/p/4788028.html

 

4. Judge whether one time period coincides with another

//Judge time period time1~time2 Time period or not fromTime~toTime coincidence
if(time1 >= fromTime && time1 < toTime || fromTime >= time1 && fromTime < time2){
   //coincidence
}

 

5. Use set to de duplicate list set

//utilize set Yes list Repeat
Set<SPCustomer> set = new HashSet<SPCustomer>(tempList);
//Use the set Rebuild a list
List<SPCustomer> tempList2 = new ArrayList<SPCustomer>(set);

Topics: Java less