C~K is looking for a girlfriend! ! ! (java Set sort)

C~K is looking for a girlfriend! ! !

Time Limit: 1000 ms  Memory Limit: 131072 KiB

Problem Description

As 11.11 is approaching, C~K sees that all the friends around her have taken off their orders or are about to do so. C~K also wants to find a girlfriend (I heard that the country will assign them?). MeiK heard about this and said that
C~K was finally enlightened, so he compiled a list of candidates for C~K. But C~K has the height range and age limit of the girls he is attracted to, so he wants
to filter out the information of the eligible girls, but this is difficult for C~K, it is about C~K's happiness, you can help him?
ps: Because MeiK is stupid, there may be duplicate information about girls in the list. If the information is duplicated, the first input is valid information.

Input

Multiple sets of inputs.
There are N people (N<100000) in the candidate list entered into MeiK in the first line.
Enter the four integers a,b,c,d on the second line. Represent the minimum and maximum height and the minimum and maximum age of C~K heart girls, respectively. (The title guarantees a<=b, c<=d)
Next, enter N lines, each line represents the information of a girl (name, height, age, contact information)

ps: Contact information no more than 11 characters.

Output


For each set of inputs, the first line outputs an n, representing the number of eligible girls.
For the next n lines, each line outputs the information of a qualified girl.
The output order is sorted by height from low to high. If the height is the same, it is sorted by age from high to bottom. If the age is also the same, it is output in the input order.

Sample Input

4
160 170 20 22
Goddess 1 161 19 11111
Goddess 2 167 20 22222
Goddess 2 167 20 22222
Goddess 3 163 21 33333

Sample Output

2
Goddess 3 163 21 33333
Goddess 2 167 20 22222
此处在排序的时候要用到Collection.sort方法,但是这个方法只支持List,所以要把Set中的内容存放到List中,因为排序还有多个条件,所以要
重写匿名内部类Comparator声明排序规则。
 
  
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
class Girl{
	String name;
	int height, age;
	String phone;
	public Girl(String name, int height, int age, String phone) {
		super();
		this.name = name;
		this.height = height;
		this.age = age;
		this.phone = phone;
	}
	public Girl() {
		
	}
	@Override
	public String toString() {
		return name + " " + height + " " + age + " " + phone;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + height;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((phone == null) ? 0 : phone.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Girl other = (Girl) obj;
		if (age != other.age)
			return false;
		if (height != other.height)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (phone == null) {
			if (other.phone != null)
				return false;
		} else if (!phone.equals(other.phone))
			return false;
		return true;
	}
	
}
public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int n;
		int l_height, h_height, l_age, h_age;
		Girl gi = new Girl();
		n = cin.nextInt();
		l_height = cin.nextInt();
		h_height = cin.nextInt();
		l_age  = cin.nextInt();
		h_age  = cin.nextInt();
		Set<Girl> girl = new HashSet<Girl>();
		while(n-- > 0) {
			gi = new Girl(cin.next(), cin.nextInt(), cin.nextInt(), cin.next());
			if(gi.height>=l_height && gi.height<=h_height && gi.age>=l_age && gi.age<= h_age) {
				girl.add(gi);
			}
		}
		List<Girl> girl1 = new ArrayList<Girl>();
		Iterator<Girl> it = girl.iterator();
		while(it.hasNext()) {
			girl1.add(it.next());
		}
		Collections.sort(girl1, new Comparator<Girl>() {
			@Override
			public int compare(Girl o1, Girl o2) {
				if(o1.height == o2.height) {
					return o2.age-o1.age;
				}
				return o1.height-o2.height;
			}
		});
		Iterator<Girl> it1 = girl1.iterator();
		System.out.println(girl1.size());
		while(it1.hasNext()) {
			System.out.println(it1.next());
		}
	}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325735271&siteId=291194637