collection工具类
package day11; //工具类:提供一些静态的方法 import java.util.ArrayList; import java.util.Collections; public class CollectionsDemo { public static void main(String[] args) { ArrayList<Integer> list =new ArrayList<Integer>(); list.add(100); list.add(78); list.add(87); list.add(1); for(Integer n:list){ System.out.println(n); } System.out.println("********************"); System.out.println("asc sorted"); //升序排列 Collections.sort(list); for(Integer n1:list){ System.out.println(n1); } System.out.println("*******************"); //翻转排序(想要排序必须先升序) System.out.println("desc sorted"); Collections.reverse(list); for(Integer n2:list){ System.out.println(n2); } System.out.println("********************"); ArrayList<String> stringSet = new ArrayList<String>(); stringSet.add("Zeeb"); stringSet.add("Tcp"); stringSet.add("Http"); stringSet.add("要吃饭了..."); stringSet.add("七夕到了"); for(String s:stringSet){ System.out.println(s); } System.out.println("*******************"); Collections.sort(stringSet); for(String s1:stringSet){ System.out.println(s1); } } }
公用News类
package day11; public class News implements Comparable<News> { private String pubDate; private String title; public String getPubDate() { return pubDate; } public void setPubDate(String pubDate) { this.pubDate = pubDate; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public News(String pubDate, String title) { super(); this.pubDate = pubDate; this.title = title; } public News() { super(); } @Override public int compareTo(News o) { return this.pubDate.compareTo(o.pubDate); } }
练习1(ArrayList)
package day11; import java.util.ArrayList; import java.util.Collections; public class ListDemo { //(box)装箱:把基本数据类型转换为object类型(子类转换为父类) //(unBox)拆箱:把object类型转换为基本数据类型(父类转换为子类:强转) public static void main(String[] args) { ArrayList<News> newsList = new ArrayList<News>(); News news1 = new News("2017-8-28","又到一年七夕日"); News news2 = new News("2017-8-29","星期一,又要上课了"); News news3 = new News("2017-8-27","开学了"); newsList.add(news1); newsList.add(news2); newsList.add(news3); //newsList.add(news3);//可以重复,重复会显示 Collections.sort(newsList); for(News news:newsList){ System.out.println(news.getPubDate()+" "+news.getTitle()); } System.out.println("delete first"); newsList.remove(0);//索引值不能为最后一个,会报错 //newsList.clear();//全部都删除 for(News news:newsList){ System.out.println(news.getPubDate()+" "+news.getTitle()); } } }
结果图:
练习2(HashSet)
package day11; import java.util.HashSet; import java.util.Iterator; public class SetDemo { public static void main(String[] args) { HashSet<Topic>topics = new HashSet<Topic>(); Topic topic1 = new Topic(1,"七夕你快乐吗?"); Topic topic2 = new Topic(2,"上课你快乐吗?"); Topic topic3 = new Topic(3,"写代码你快乐吗?"); topics.add(topic1); topics.add(topic2); topics.add(topic3); topics.add(topic1);//不能重复,重复不会显示 //topic1 = new Topic(1,"七夕我不快乐"); //topics.add(topic1);//新建了一个内存空间 topic1.setTitle("七夕我很快乐"); topic1.setTopicId(6); topics.add(topic1);//不是根据内容来判定是否能重复,根据对象的内存地址 Iterator<Topic> it = topics.iterator(); while(it.hasNext()){ System.out.println(it.next().getTitle()); } } }
结果图:
公用Book类
package day11; public class Book { private String bookId; private String bookName; private String author; private String pubDate; public Book() { super(); } public Book(String bookId, String bookName, String author, String pubDate) { super(); this.bookId = bookId; this.bookName = bookName; this.author = author; this.pubDate = pubDate; } public String getBookId() { return bookId; } public void setBookId(String bookId) { this.bookId = bookId; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getPubDate() { return pubDate; } public void setPubDate(String pubDate) { this.pubDate = pubDate; } @Override public boolean equals(Object obj) { if(!(obj instanceof Book)){ return false; } Book book = (Book)obj; return this.bookName.equals(book.bookName)&& this.author.equals(book.author)&& this.pubDate.equals(book.pubDate); //两个@Override用来判断不能重复 } @Override public int hashCode() { return this.bookName.hashCode();//返回当前对象的哈希码 } }
package day11; import java.util.HashSet; import java.util.Set; public class bookTest { private Set<Book> books; public bookTest(){ books = new HashSet<Book>(); Book book1 = new Book("b011","哈利波特","外国人","1996-10-18"); Book book2 = new Book("b015","孔乙己","鲁迅","1937-7-7"); Book book3 = new Book("b008","时间简史","霍金","1737-7-7"); Book book4 = new Book("b002","死亡笔记","日本人","2007-1-7"); books.add(book1); books.add(book2); books.add(book3); books.add(book4); } public void showBooks() { for(Book book:books){ System.out.println(book.getBookId()+" "+book.getBookName()+" "+book.getAuthor()+" "+book.getPubDate()); } } public void addBook(Book book) { books.add(book); } public boolean contains(Book book){ return books.contains(book); } public static void main(String[] args) { bookTest bTest = new bookTest(); bTest.showBooks(); System.out.println("-------------添加图书以后------------"); Book book = new Book(); book.setBookId("b007"); book.setBookName("西游记"); book.setAuthor("吴承恩"); book.setPubDate("1592-3-3"); bTest.addBook(book); Book book1 = new Book(); book1.setBookId("b007"); book1.setBookName("西游记"); book1.setAuthor("吴承恩"); book1.setPubDate("1592-3-3"); // bTest.addBook(book1); boolean isContains = bTest.contains(book1); System.out.println(isContains); bTest.showBooks(); } }
结果图:
Job类
package day12homework; public class Job implements Comparable<Job> { private int jobId; private String jobName; private String jobCatgory; private String jobPub_date; public Job() { super(); } public Job(int jobId, String jobName, String jobCatgory, String jobPub_data) { super(); this.jobId = jobId; this.jobName = jobName; this.jobCatgory = jobCatgory; this.jobPub_date = jobPub_data; } public int getJobId() { return jobId; } public void setJobId(int jobId) { this.jobId = jobId; } public String getJobName() { return jobName; } public void setJobName(String jobName) { this.jobName = jobName; } public String getJobCatgory() { return jobCatgory; } public void setJobCatgory(String jobCatgory) { this.jobCatgory = jobCatgory; } public String getJobPub_date() { return jobPub_date; } public void setJobPub_date(String jobPub_date) { this.jobPub_date = jobPub_date; } @Override public boolean equals(Object obj) { if (!(obj instanceof Job)) { return false; } Job job = (Job)obj; return this.jobName.equals(job.jobName) &&this.jobCatgory.equals(job.jobCatgory) &&this.jobPub_date.equals(job.jobPub_date); } public int hashCode() { return this.jobName.hashCode(); } @Override public int compareTo(Job o) { if (this.jobId > o.jobId) { return 1; }else if (this.jobId == o.jobId) { return 0; }else { return -1; } } }
Nmae选择器
package day12homework; import java.text.Collator; import java.util.Comparator; import java.util.Locale; public class JobComparatorByName implements Comparator<Job> { @Override public int compare(Job o1, Job o2) { Collator instance = Collator.getInstance(Locale.CHINA); return instance.compare(o1.getJobName(), o2.getJobName()); //return o1.getJobName().compareTo(o2.getJobName()); } }
Pub_date选择器
package day12homework; import java.util.Comparator; public class JobComparatorByPub_date implements Comparator<Job> { @Override public int compare(Job o1, Job o2) { return o1.getJobPub_date().compareTo(o2.getJobPub_date()); } }
实现测试类
package day12homework; import java.util.HashSet; public class Test1 { private HashSet<Job> jobs; public Test1(){ jobs=new HashSet<Job>(); Job job1=new Job(5,"程序员","技术开发","2017-8-29"); Job job2=new Job(15,"架构师","技术开发","2017-6-29"); Job job3=new Job(55,"鼓励师","后勤","2017-2-25"); Job job4=new Job(2,"保洁","后勤","2017-12-29"); Job job5=new Job(9,"会计","财会","2016-8-29"); jobs.add(job1); jobs.add(job2); jobs.add(job3); jobs.add(job4); jobs.add(job5); } public void showJobs(){ for(Job job:jobs){ System.out.println(job.getJobId()+" " +job.getJobName()+" " +job.getJobCatgory()+" " +job.getJobPub_date()+" "); } } public void addJob(Job job){ this.jobs.add(job); } public void removeJob(Job job){ this.jobs.remove(job); } public static void main(String[] args) { Test1 test1=new Test1(); test1.showJobs(); System.out.println("==========added==========="); Job job1=new Job(19,"审计","审计","2016-8-19"); test1.addJob(job1); test1.showJobs(); System.out.println("=========deleted============"); Job job2=new Job(19,"审计","审计","2016-8-19"); test1.removeJob(job2); test1.showJobs(); } }
结果图:
package day12homework; import java.util.HashMap; public class Test3 { private HashMap<Job,Integer> jobs; public Test3(){ jobs=new HashMap<Job,Integer>(); Job job1=new Job(5,"程序员","技术开发","2017-8-29"); Job job2=new Job(15,"架构师","技术开发","2017-6-29"); Job job3=new Job(55,"鼓励师","后勤","2017-2-25"); Job job4=new Job(2,"保洁","后勤","2017-12-29"); Job job5=new Job(9,"会计","财会","2016-8-29"); jobs.put(job1, 5); jobs.put(job2, 2); jobs.put(job3, 15); jobs.put(job4, 3); jobs.put(job5, 1); } public void updateNumber(Job job,int num){ if(jobs.keySet().contains(job)){ jobs.put(job, num); } }
结果图:
package day12homework; import java.util.HashMap; public class Test3 { private HashMap<Job,Integer> jobs; public Test3(){ jobs=new HashMap<Job,Integer>(); Job job1=new Job(5,"程序员","技术开发","2017-8-29"); Job job2=new Job(15,"架构师","技术开发","2017-6-29"); Job job3=new Job(55,"鼓励师","后勤","2017-2-25"); Job job4=new Job(2,"保洁","后勤","2017-12-29"); Job job5=new Job(9,"会计","财会","2016-8-29"); jobs.put(job1, 5); jobs.put(job2, 2); jobs.put(job3, 15); jobs.put(job4, 3); jobs.put(job5, 1); } public void updateNumber(Job job,int num){ if(jobs.keySet().contains(job)){ jobs.put(job, num); } } public void showJobs(){ for(Job job:jobs.keySet()){ System.out.println(job.getJobId()+" " +job.getJobName()+" " +job.getJobCatgory()+" " +job.getJobPub_date()+" " +jobs.get(job).intValue()); } } public static void main(String[] args) { Test3 test3=new Test3(); test3.showJobs(); System.out.println("=========修改数量以后============"); //如果原集合并不存在此工作,则不修改 Job job=new Job(55,"鼓励师1","后勤","2017-2-25"); test3.updateNumber(job, 50); test3.showJobs(); } }
结果图: