栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java-集合案例三

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java-集合案例三

1.向TreeSet集合中加入5个员工的对象,根据员工的年龄(升序)进行排序...
import java.util.Set;
import java.util.TreeSet;



class Employee implements Comparable{
	int age;
	int workAge;
	int salary;
	public Employee(int age,int workAge,int salary) {
		this.age = age;
		this.workAge = workAge;
		this.salary = salary;
	}
	
	@Override
	public String toString() {
		return "Employee[年龄:" + age + ", 工龄:" + workAge + ", 薪水:" + salary + "]";
	}

	@Override
	public int compareTo(Employee o) {
		//根据员工的年龄(升序)进行排序,若年龄相同,再根据工龄(降序)来排序,若工龄相同,根据薪水(降序)排序
		if(this.age==o.age) {
			if(this.workAge==o.workAge) {
				return o.salary-this.salary;
			}else {
				return o.workAge-this.workAge;
			}			
		}else{
			return this.age - o.age;
		}
	}
		
}

public class Work01 {
	public static void main(String[] args) {
		Set set = new TreeSet();
		set.add(new Employee(25, 3, 5555));
		set.add(new Employee(27, 4, 7777));
		set.add(new Employee(26, 4, 6666));
		set.add(new Employee(28, 5, 8888));
		set.add(new Employee(28, 6, 9999));
		System.out.println(set);
	}
}
2.Map集合的使用
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;




public class Work02 {
	public static void main(String[] args) {
		//1) 使用一个Map,以老师的名字作为键,以老师教授的课程名作为值
		Map map = new HashMap();
		map.put("tom", "CoreJava");
		map.put("John", "Oracle");
		map.put("SuSan", "Oracle");
		map.put("Jerry", "JDBC");
		map.put("Jim", "Unix");
		map.put("Kevin", "JSP");
		map.put("Lucy", "JSP");
		
		//2) 增加了一位新老师Allen 教JDBC 
		map.put("Allen", "JDBC");
		
		//3) Lucy 改为教CoreJava 
		map.replace("Lucy", "CoreJava");
		
		//4) 遍历Map,输出所有的老师及老师教授的课程(Set>、Set get(key))
		System.out.println("遍历Map,输出所有的老师及老师教授的课程为:");
		Set> set = map.entrySet();
		for(Entry entry:set) {
			System.out.println(entry.getKey()+"--->"+entry.getValue());
		}
		
		//5) 利用Map,输出所有教JSP 的老师
		System.out.println("所有教JSP的老师为:");
		for(Entry entry:set) {
			if(entry.getValue().equals("JSP") ){
				System.out.println(entry.getKey());
			}
		}
	}
}
3. 集合中存储学生对象,属性有,学号,姓名,分数,请选择合适的集合进行存储...
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;



class Student{
	String id;
	String name;
	int score;
	public Student(String id,String name,int score) {
		this.id = id;
		this.name = name;
		this.score = score;
	}
	@Override
	public String toString() {
		return "Student[学号:" + id + ",姓名:" + name + ",分数:" + score + "]";
	}
	
}

public class Work03 {
	public static void main(String[] args) {
		Set set = new TreeSet(new Comparator() {

			@Override
			public int compare(Student o1, Student o2) {
				//(1). 先按分数的降序排列,如果分数相同,则按学号升序排列(给定多个分数相同的学生)
				if(o1.score==o2.score) {
					return o1.id.compareTo(o2.id);
				}else {
					return o2.score-o1.score;
				}
				
			}
		});
		set.add(new Student("001", "zs", 88));
		set.add(new Student("002", "ls", 88));
		set.add(new Student("003", "ww", 88));
		set.add(new Student("004", "zl", 77));
		set.add(new Student("005", "zs", 55));
		System.out.println(set);
		
		//(2). 求出学员的优秀人数,及优秀率; (达到80分及以上, 则为优秀)
		int num=0;
		double rate = 0.0;
		for(Student o:set) {
			if(o.score>=80) {
				num++;
			}
		}
		rate = num/5.0;
		System.out.println("学员的优秀人数为:"+num+"t优秀率为:"+rate);
		
		//(3). 求出学员的几个人数,及及格率;(达到60分及以上, 则为及格)
		int num2=0;
		double rate2 = 0.0;
		for(Student o:set) {
			if(o.score>=60) {
				num2++;
			}
		}
		rate2 = num2/5.0;
		System.out.println("学员的及格人数为:"+num2+"t及格率为:"+rate2);
		
	}
}
4.使用键盘输入省的名称,得到所有的市
import java.util.Arrays;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;



public class Work04 {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		String[] list1 = new String[] {"沈阳","大连","鞍山","抚顺","本溪","丹东","锦州","营口","阜新"};
		String[] list2 = new String[] {"吉林市","四平市","通化市","白山市","辽源市","白城市"};
		String[] list3 = new String[] {"哈尔滨市","齐齐哈尔市","鸡西市","鹤岗市","双鸭山市","大庆市"};
		String[] list4 = new String[] {"广州","深圳","珠海","东莞","佛山","中山","惠州"};
		
		Map map = new TreeMap();
		map.put("辽宁省", list1);
		map.put("吉林省", list2);
		map.put("黑龙江省", list3);
		map.put("广东省", list4);
		
		System.out.println("请输入省的名称:");
		String name = input.nextLine();
		
		System.out.println(Arrays.toString(map.get(name)));
	}
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/591438.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号