Application of Java heap sorting--sorting classes according to target members

Application of Java heap sorting--sorting classes according to target members

foreword

Suddenly want to rearrange the data searched by the SQL statement, that is, sort the class list. For a while, I searched the Internet and said that I need to override the compareTo function. I think it is too annoying to do so, and I want to make a sort by myself and want to be simpler.
So I suddenly remembered heap sorting. The performance of heap sort is similar to the average performance of quick sort, so I plan to use heap to sort classes, and review the heap by the way.

heap sort scheme

  • Use the minimum heap, and specify the comparison rule to compare the size of the expected comparison field ( ->the value on the right determines the size relationship between parameter 1 and parameter 2)
//小堆
Queue<UserDevice> heap =new PriorityQueue<UserDevice>((n1, n2) -> n1.compareDeviceId(n2.getDeviceId()));
  • Specify a comparison method in the class
class UserDevice {
    
    
	private Integer deviceId;
	private String  userName;
	public UserDevice (String  userName,int deviceId){
    
    
		this.userName=userName;
		this.deviceId=deviceId;
	}
	public Integer getDeviceId() {
    
    
		return deviceId;
	}
	public void setDeviceId(Integer deviceId) {
    
    
		this.deviceId = deviceId;
	}
	public String getUserName() {
    
    
		return userName;
	}
	public void setUserName(String userName) {
    
    
		this.userName = userName;
	}
	
	//定义null最小
	public int compareDeviceId(Integer anotherInteger) {
    
    
		if(anotherInteger==null&&deviceId==null) 
			return 0;
		if(deviceId==null)
			return -1;
		if(anotherInteger==null)
			return 1;
		return deviceId.compareTo(anotherInteger);	
	}
}

application

		//小堆
        Queue<UserDevice> heap =new PriorityQueue<UserDevice>((n1, n2) -> n1.compareDeviceId(n2.getDeviceId()));
    	for(UserDevice ud :userDevices) {
    
    
    		heap.offer(ud);
    	}
    	while(heap.size()>0) {
    
    
    		UserDevice ud = heap.poll(); 
    		System.out.println(ud.getUserName()+"----ID:"+ud.getDeviceId());
    	}

Quick schedule

After the heap sorting is completed, if you still have energy, think about the quick sorting solution.
One method is to implement the Comparable interface, and then call the Collections.sort(List) method to sort. The other method is not to implement the Comparable interface, but Use the Collections.sort(List, Comparator) method when sorting, and implement the Comparator interface. The method I take is the latter, because there are few places that need to be sorted, but there are many places where classes are used.

  • Add a comparison function to the class (or separate it to the outside)
public int compareTo(UserDevice ud) {
    
    
		if((ud==null||ud.getDeviceId()==null)&&deviceId==null) 
			return 0;
		if(deviceId==null)
			return -1;
		if(ud==null||ud.getDeviceId()==null)
			return 1;
		return deviceId.compareTo(ud.getDeviceId());
	}

application

  • Use the Collections.sort(List, Comparator) method
		Collections.sort(userDevices,new Comparator<UserDevice>() {
    
    
    		@Override
    		public int compare(UserDevice o1,UserDevice o2) {
    
    
    			if(o1==null&&(o2==null||o2.getDeviceId()==0))return 0;
    			if(o1==null)return -1;
    			return o1.compareTo(o2);
    		}
    	});
 
    	for(UserDevice ud :userDevices) {
    
    
    		System.out.println(ud.getUserName()+"----ID:"+ud.getDeviceId());
    	}

code record

public class testLearn {
    
    
	
	public static int  n = 1;
	
    public static void main(String[] args) {
    
        	
    	try {
    
    
    		test();
		} catch (IOException e) {
    
    
			e.printStackTrace();
		} catch (Exception e) {
    
    
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
    }
    
    
    
    public static void test() throws Exception {
    
    
    	ArrayList <UserDevice> userDevices= new  ArrayList<UserDevice>();
    	userDevices.add(new UserDevice("张三",3));
    	userDevices.add(new UserDevice("李四",2));
    	userDevices.add(new UserDevice("王五",1));
    	
    	for(UserDevice ud :userDevices) {
    
    
    		System.out.println(ud.getUserName()+"----ID:"+ud.getDeviceId());
    	}
    	
    	//小堆
        Queue<UserDevice> heap =new PriorityQueue<UserDevice>((n1, n2) -> n1.compareDeviceId(n2.getDeviceId()));
    	for(UserDevice ud :userDevices) {
    
    
    		heap.offer(ud);
    	}
    	while(heap.size()>0) {
    
    
    		UserDevice ud = heap.poll(); 
    		System.out.println(ud.getUserName()+"----ID:"+ud.getDeviceId());
    	}
    	Collections.sort(userDevices,new Comparator<UserDevice>() {
    
    
    		@Override
    		public int compare(UserDevice o1,UserDevice o2) {
    
    
    			if(o1==null&&(o2==null||o2.getDeviceId()==0))return 0;
    			if(o1==null)return -1;
    			return o1.compareTo(o2);
    		}
    	});
 
    	for(UserDevice ud :userDevices) {
    
    
    		System.out.println(ud.getUserName()+"----ID:"+ud.getDeviceId());
    	}
    	
    }
    
}
class UserDevice {
    
    
	private Integer deviceId;
	private String  userName;
	public UserDevice (String  userName,int deviceId){
    
    
		this.userName=userName;
		this.deviceId=deviceId;
	}
	public Integer getDeviceId() {
    
    
		return deviceId;
	}
	public void setDeviceId(Integer deviceId) {
    
    
		this.deviceId = deviceId;
	}
	public String getUserName() {
    
    
		return userName;
	}
	public void setUserName(String userName) {
    
    
		this.userName = userName;
	}
	
	//定义null最小
	public int compareDeviceId(Integer anotherInteger) {
    
    
		if(anotherInteger==null&&deviceId==null) 
			return 0;
		if(deviceId==null)
			return -1;
		if(anotherInteger==null)
			return 1;
		return deviceId.compareTo(anotherInteger);
		
	}
	public int compareTo(UserDevice ud) {
    
    
		if((ud==null||ud.getDeviceId()==null)&&deviceId==null) 
			return 0;
		if(deviceId==null)
			return -1;
		if(ud==null||ud.getDeviceId()==null)
			return 1;
		return deviceId.compareTo(ud.getDeviceId());
	}
}

epilogue

I finally thought about it, and it is better to use quick sort, or directly hand over the sorting task to SQL.

Guess you like

Origin blog.csdn.net/ex_xyz/article/details/122232451