集合排序练习

1.在一个列表中存储以下元素:red、blue、orange、yellow。
(1)要求打印集合中最大和最小的元素。
public class ListTest1 {
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("red");
		list.add("blue");
		list.add("orange");
		list.add("yellow");
		Collections.sort(list);
		System.out.println("集合中最小的元素:" + list.get(0));
		System.out.println("集合中最大的元素:" + list.get(list.size() - 1));
	}
(2)要求找出集合中长度最大和长度最小的元素
public class ListTest {
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("red");
		list.add("blue");
		list.add("orange");
		list.add("yellow");
		Collections.sort(list, new SortRule());
		System.out.println("集合中长度最小的元素为:" + list.get(0));
		System.out.println("集合中长度最大的元素为:" + list.get(list.size() - 1));
	}
}


public class SortRule implements Comparator<String> {

	@Override
	public int compare(String o1, String o2) {
		int result = o1.length() - o2.length();
		return result;
	}

}
2.如何去除list(存储元素如下)集合中重复的元素并排序
List<String> list = new ArrayList<String>();
list.add("1");
list.add("1");
list.add("22");
list.add("2");
list.add("3");
list.add("31");
list.add("3 1");
list.add("");
list.add("0");
list.add("\t");

public class Demo {
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("1");
		list.add("1");
		list.add("22");
		list.add("2");
		list.add("3");
		list.add("31");
		list.add("3 1");
		list.add("");
		list.add("0");
		list.add("\t");
		Demo.getDistinctList(list);
		System.out.println(list);
	}
public static void getDistinctList(List<String> list) {
		Set<String> set = new HashSet<String>();
		set.addAll(list);
		list.clear();
		list.addAll(set);
		Collections.sort(list);
	}
}
3.写一个Student类, 包含属性code[1-30), grade[1-6], score[0-100], 所有属性都是随机生成(Math.random()方法,返回一个范围[0,1)的随机数)
要求保存这二十位同学的信息,学号(code)不得相同, 并找出分数最高的同学和分数最低的同学, 最后打印输出最高分和最低分同学信息.
public class Student implements Comparable<Student> {
	private int code;
	private int grade;
	private int score;
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public int getGrade() {
		return grade;
	}
	public void setGrade(int grade) {
		this.grade = grade;
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	public Student(int code, int grade, int score) {
		super();
		this.code = code;
		this.grade = grade;
		this.score = score;
	}
	public Student() {
		super();
	}
	@Override
	public String toString() {
		return "Student [学号=" + code + ", 年级=" + grade + ", 分数=" + score + "]";
	}
	@Override
	public int compareTo(Student arg0) {
		int result = this.score - arg0.getScore();
		return result;
	}
	
}
public class ListTest {
	public static void main(String[] args) {
		List<Student> list = new ArrayList<Student>();
		Set<Integer> set = new HashSet<Integer>();
		//将学号添加进set集合中,并保证set集合大小不超过20
		while(set.size() < 20) {
			//获取学号的随机数[1,30)
			//加(int)是因为:范围大的数据强制给范围小的数据赋值时,会出现精度的缺失。
			int code = (int) (Math.random() * 29 + 1);//[0,1)
			set.add(code);
		}
		for(int code : set) {
			int grade = (int) (Math.random() * 6 + 1);
			int score = (int) (Math.random() * 100 + 1);
			list.add(new Student(code,grade,score));
		}
		for(Student stu : list) {
			System.out.println(stu);
		}
		Collections.sort(list);
		System.out.println("分数最低同学:" + list.get(0));
		System.out.println("分数最高同学:" + list.get(list.size() - 1));
	}
}
4.双色球规则:双色球每注投注号码由6个红色球号码和1个蓝色球号码组成。红色球号码从1—33中选择;蓝色球号码从1—16中选择;请随机生成一注双色球号码。(要求同色号码不重复)
public class ListTest {
	public static void main(String[] args) {
		List<DoubleChromosphere> list = new ArrayList<DoubleChromosphere>();
		Set<Integer> set = new HashSet<Integer>();
		while(set.size() < 6) {
			int red = (int) (Math.random() * 32 + 1);//[0,1)
			set.add(red);
		}
		int blue = (int) (Math.random() * 15 + 1);//[0,1)
		set.add(blue);
			System.out.println(set);
	}
}

你可能感兴趣的:(集合排序练习)