Sorting operation of ArrayList in Java (2)-custom sorting rules

A previously written blog post directly used Collections.sort()to ArrayListsort one . This blog plus a little bit of advanced operations 自定义排序规则.

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class T_00 {
    
    
	public static void main(String[] args) {
    
    
		ArrayList<Integer> a = new ArrayList<Integer>();
		a.add(1);
		a.add(3);
		a.add(5);
		a.add(2);
		a.add(4);
		
		for (int i = 0; i < a.size(); i++) {
    
    
			System.out.println(a.get(i));
		}
		System.out.println("====================");
		// 自定义排序规则,使其降序排列
		Collections.sort(a, new Comparator<Integer>() {
    
    
			@Override
			public int compare(Integer o1, Integer o2) {
    
    
				if(o1>o2)
					return -1;
				else if(o1<o2)
					return 1;
				return 0;
			};
		});
		
		for (int i = 0; i < a.size(); i++) {
    
    
			System.out.println(a.get(i));
		}
	}
}

Insert picture description here

in conclusion

Use Collections.sort(List,Comparator), you can customize the sorting rules. First we need to implement the Comparatorinterface and rewrite the public int compare(Object o1, Object o2) method. Write a custom sorting rule in this method.

数据中有负数时,要注意数据的处理方式

Another point is that you can only 集合类型sort them, you int数组can't do this directly, you have to convert them first 集合类型.

if(o1>o2)
	return -1;
else if(o1<o2)
	return 1;
return 0;

Make a question and consolidate the smallest sequence generated by splicing strings

Reference solution

import java.util.*;

public class Test {
    
    
    public String minString (String[] strs) {
    
    
    	// 这里如果直接使用数组进行冒泡排序,会超时
    	// 所以这里转换成ArrayList,并使用Collections.sort(list,Comparator);排序
        ArrayList<String> list = new ArrayList<>();
        for(String s:strs) list.add(s);    
        Collections.sort(list,new Comparator<String>(){
    
    
            public int compare(String o1,String o2){
    
    
                return (o1+o2).compareTo(o2+o1);
            }
        });
        
        StringBuilder sb = new StringBuilder();
        for(String s:list)
            sb.append(s);
        return sb.toString();
    }
}

One more question-merging of arrays

Reference solution

/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
import java.util.Collections;
import java.util.Comparator;
import java.util.ArrayList;
public class Solution {
    
    
    public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
    
    
        if (intervals.size() <= 1)
			return intervals;
		// 排序规则,升序排列,若左边界相等,则比较右边界
		Collections.sort(intervals, new Comparator<Interval>() {
    
    
			@Override
			public int compare(Interval o1, Interval o2) {
    
    
				if (o1.start == o2.start) {
    
    
					return o1.end - o2.end;
				} else {
    
    
					return o1.start - o2.start;
				}
			}
		});

		ArrayList<Interval> res = new ArrayList<Interval>();
		res.add(intervals.get(0));
		for (int i = 1; i < intervals.size(); i++) {
    
    
			if(res.get(res.size()-1).start==intervals.get(i).start) {
    
    
				res.get(res.size()-1).end = Math.max(res.get(res.size()-1).end, intervals.get(i).end);
				continue;
			}
					
			if(res.get(res.size()-1).end<intervals.get(i).start)
				res.add(intervals.get(i));
			else {
    
    
				res.get(res.size()-1).end = Math.max(res.get(res.size()-1).end, intervals.get(i).end);
			}
		}
		return res;
    }
}

Guess you like

Origin blog.csdn.net/Awt_FuDongLai/article/details/109742265