国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java集合框架(下)

2019-11-06 07:27:17
字體:
來源:轉載
供稿:網友

contains原理

List.contains()

-遍歷Lsit,對集合中的每個元素進行equals判斷,只要有一個元素equals返回true,則contains返回true。 這里寫圖片描述

Set.contains()

-遍歷Lsit,對集合中的每個元素進行equals以及hashCode判斷,只要有一個元素equals以及hashCode返回true,則contains返回true。 這里寫圖片描述

測試List.contains() and Set.contains()

-Course類

public class Course { // 課程編號 PRivate String id; // 課程名 private String name; public Course() { } public Course(String id, String name) { this.id = id; this.name = name; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Course)) return false; Course other = (Course) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; }}

-Student

import java.util.Set;public class Student { // 學生編號 private String id; // 學生姓名 private String name; // 課程集合 private Set<Course> courses; public Student(String id, String name, Set<Course> courses) { this.id = id; this.name = name; this.courses = courses; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Course> getCourses() { return courses; } public void setCourses(Set<Course> courses) { this.courses = courses; }}

-main程序

import java.util.ArrayList;import java.util.Arrays;import java.util.HashSet;import java.util.List;import java.util.Scanner;import java.util.Set;public class SetTest { private List<Course> listCourses; private Scanner scanner; private Student student; public SetTest() { } public SetTest(List<Course> listCourses, Scanner scanner) { this.listCourses = listCourses; this.scanner = scanner; } // 用于向課程集合中添加被選課程 public void add() { Course course1 = new Course("1", "數據結構"); listCourses.add(course1); Course temp = listCourses.get(0); System.out.println(temp.getId() + "/" + temp.getName()); Course course2 = new Course("2", "C語言"); /* * 在可以訪問的位置中插入數據,之前相應的數據位置往后移動一位 目前可訪問的位置為0 若試圖訪問其他位置則會發生數組下標越界異常 */ listCourses.add(0, course2); Course temp2 = listCourses.get(0); System.out.println(temp2.getId() + "/" + temp2.getName()); // 一次添加多個元素 Course[] courses = { new Course("3", "離散數學"), new Course("4", "匯編語言") }; // asList將數組轉換為集合 listCourses.addAll(Arrays.asList(courses)); Course temp3 = listCourses.get(2); System.out.println(temp3.getId() + "/" + temp3.getName()); Course temp4 = listCourses.get(3); System.out.println(temp4.getId() + "/" + temp4.getName()); Course[] courses2 = { new Course("5", "高等數學"), new Course("6", "大學英語") }; // 新添加的兩門課程從2處插入 listCourses.addAll(2, Arrays.asList(courses2)); Course temp5 = listCourses.get(2); System.out.println(temp5.getId() + "/" + temp5.getName()); Course temp6 = listCourses.get(3); System.out.println(temp5.getId() + "/" + temp6.getName()); } // 通過foreach遍歷List public void foreach() { System.out.println("調用foreach"); for (Course course : listCourses) { System.out.println(course.getId() + "/" + course.getName()); } } // foreach輸出所選課程 public void StudentForeach(Student student) { // 輸出學生所選課程,輸出順序不固定 for (Course course : student.getCourses()) { System.out.println(course.getId() + "/" + course.getName()); } } // 測試List-contains方法 public void contains() { Course course = listCourses.get(0); System.out.println("取得課程:" + course.getName()); System.out.println("備選課程中是否包含:" + course.getName() + "," + listCourses.contains(course)); // 創建一個新課程對象 System.out.println("輸入課程名"); String name = scanner.next(); Course course2 = new Course(); course2.setName(name); System.out.println("取得課程:" + course2.getName()); // 根據需求重寫Course.equals方法來獲得效果 System.out.println("備選課程中是否包含:" + course2.getName() + "," + listCourses.contains(course2)); } // 創建學生對象并選課 public void createStudentAndSelectCourse() { // 創建一個學生對象 Set<Course> courses = new HashSet<Course>(); student = new Student("1", "小明", courses); // 獲取鍵盤輸入的課程ID Scanner scanner = new Scanner(System.in); for (int i = 0; i < 3; i++) { System.out.println("請輸入課程ID"); String id = scanner.next(); for (Course course : listCourses) { if (course.getId().equals(id)) { student.getCourses().add(course); } } } StudentForeach(student); } // 測試Setcontains方法 public void SetContains() { // 提示輸入課程名稱 System.out.println("請輸入學生已選課程名稱"); String name = scanner.next(); Course course2 = new Course(); course2.setName(name); System.out.println("取得課程:" + course2.getName()); // 根據需求重寫Course.equals方法來獲得效果 System.out.println("備選課程中是否包含:" + course2.getName() + "," + student.getCourses().contains(course2)); } public static void main(String[] args) { List<Course> list = new ArrayList<Course>(); Scanner scanner = new Scanner(System.in); ListTest listCourses = new ListTest(list); SetTest setTest = new SetTest(list, scanner); setTest.add(); setTest.foreach();// setTest.contains(); setTest.createStudentAndSelectCourse(); setTest.SetContains(); }}

1/數據結構 2/C語言 3/離散數學 4/匯編語言 5/高等數學 5/大學英語 調用foreach 2/C語言 1/數據結構 5/高等數學 6/大學英語 3/離散數學 4/匯編語言 請輸入課程ID 1 請輸入課程ID 2 請輸入課程ID 3 3/離散數學 2/C語言 1/數據結構 請輸入學生已選課程名稱 離散數學 取得課程:離散數學 備選課程中是否包含:離散數學,true

獲取List中索引位置

-代碼跟之前的大同小異,此處只顯示略微更改的部分

// 測試List-contains方法 public void contains() { Course course = listCourses.get(0); System.out.println("取得課程:" + course.getName()); System.out.println("備選課程中是否包含:" + course.getName() + "," + listCourses.contains(course)); // 創建一個新課程對象 System.out.println("輸入課程名"); String name = scanner.next(); Course course2 = new Course(); course2.setName(name); System.out.println("取得課程:" + course2.getName()); // 根據需求重寫Course.equals方法來獲得效果 System.out.println("備選課程中是否包含:" + course2.getName() + "," + listCourses.contains(course2)); // 通過indexOf方法來取得元素索引位置 // 如果有多個重復元素,則返回第一次出現時所在索引位置,LastindexOf則是返回最后一次出現的位置 // 若不出現則返回索引位置為-1 if(listCourses.contains(course2)){ System.out.println("課程:"+course2.getName()+"索引位置為:"+listCourses.indexOf(course2)); } }public static void main(String[] args) { List<Course> list = new ArrayList<Course>(); Scanner scanner = new Scanner(System.in); ListTest listCourses = new ListTest(list); SetTest setTest = new SetTest(list, scanner); setTest.add(); setTest.contains(); setTest.foreach();// setTest.createStudentAndSelectCourse();// setTest.SetContains(); }

1/數據結構 2/C語言 3/離散數學 4/匯編語言 5/高等數學 5/大學英語 取得課程:C語言 備選課程中是否包含:C語言,true 輸入課程名 離散數學 取得課程:離散數學 備選課程中是否包含:離散數學,true 課程:離散數學索引位置為:4 調用foreach 2/C語言 1/數據結構 5/高等數學 6/大學英語 3/離散數學 4/匯編語言

判斷Map中是否包含指定的key和value

-對Student中的equals進行重寫

import java.util.Set;public class Student { // 學生編號 private String id; // 學生姓名 private String name; // 課程集合 private Set<Course> courses; public Student(String id, String name, Set<Course> courses) { this.id = id; this.name = name; this.courses = courses; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Student)) return false; Student other = (Student) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Course> getCourses() { return courses; } public void setCourses(Set<Course> courses) { this.courses = courses; }}

-主程序

import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;import java.util.Scanner;import java.util.Set;public class MapTest { // 用了盛裝學生類型 private Map<String, Student> students; private Set<Course> courses; public MapTest(Map<String, Student> students) { this.students = students; } /* * 添加學生:輸入ID,判斷是否有學生對象, 若沒有則輸入姓名,創建學生對象并添加到students中 */ public void put() { Scanner scanner = new Scanner(System.in); int i = 0; while (i < 3) { System.out.println("請輸入學生ID"); String id = scanner.next(); // 判斷id是否被占用 Student student = students.get(id); if (student == null) { System.out.println("請輸入學生姓名:"); String name = scanner.next(); Student newStudent = new Student(id, name, courses); // 向map中添加學生 students.put(id, newStudent); System.out.println("成功添加" + students.get(id).getName()); i++; } else { System.out.println("id被占用"); continue; } } } // 測試keySet public void keySet() { Set<String> key = students.keySet(); System.out.println("容量" + students.size()); for (String id : key) { Student student = students.get(id); // 判斷是否為空非常必要,不然會出現空指針異常 if (student != null) { System.out.println(student.getId() + "/" + student.getName()); } } } // 測試刪除Map中的映射 public void remove() { System.out.println("請輸入要刪除學生的id"); Scanner scanner = new Scanner(System.in); while (true) { String id = scanner.next(); Student student = students.get(id); if (student == null) { System.out.println("輸入的id不存在,請重新輸入"); continue; } else { students.remove(id); System.out.println("刪除:" + student.getName() + "成功"); break; } } } // 通過entrySet遍歷Map public void entrySet() { Set<Entry<String, Student>> entrySet = students.entrySet(); for (Entry<String, Student> entry : entrySet) { System.out.println("取得鍵:" + entry.getKey()); System.out.println("對應值:" + entry.getValue().getName()); } } // 利用put方法修改Map中已有映射 public void testPut() { System.out.println("請輸入要修改學生的id"); Scanner scanner = new Scanner(System.in); while (true) { String id = scanner.next(); Student student = students.get(id); if (student == null) { System.out.println("輸入的id不存在,請重新輸入"); continue; } else { System.out.println("對應學生為:" + student.getName()); System.out.println("請輸入新的學生名"); String name = scanner.next(); Student newStudent = new Student(id, name, courses); students.put(id, newStudent); System.out.println("修改成功"); break; } } } // 測試在Map中是否包含某個key值或者value值 public void containsKeyOrValue() { /* * 在Map中,用containsKey()方法,來判斷是否包含某個key值 * 用containsValue()方法,來判斷是否包含某個value值 作比較時經常需要對相應類的equals() 和 hasnCode() * 重寫 */ System.out.println("請輸入要查詢的學生ID"); Scanner scanner = new Scanner(System.in); String id = scanner.next(); System.out.println("在學生映射表中是否存在:" + students.containsKey(id)); if (students.containsKey(id)) { System.out.println("對應的學生為:" + students.get(id).getName()); } System.out.println("請輸入要查詢的學生姓名"); String name = scanner.next(); if (students.containsValue(new Student(null, name, courses))) { System.out.println("包含學生:" + name); } else { System.out.println("該學生不存在"); } } public static void main(String[] args) { Map<String, Student> students = new HashMap<String, Student>(); MapTest mapTest = new MapTest(students); mapTest.put(); mapTest.keySet(); // mapTest.remove(); // mapTest.testPut(); // mapTest.entrySet(); mapTest.containsKeyOrValue(); } public Map<String, Student> getStudents() { return students; } public void setStudents(Map<String, Student> students) { this.students = students; }}

請輸入學生ID 1 請輸入學生姓名: lc 成功添加lc 請輸入學生ID 2 請輸入學生姓名: shz 成功添加shz 請輸入學生ID 3 請輸入學生姓名: cjl 成功添加cjl 容量3 1/lc 2/shz 3/cjl 請輸入要查詢的學生ID 1 在學生映射表中是否存在:true 對應的學生為:lc 請輸入要查詢的學生姓名 lc 包含學生:lc

Collection.sort()實現List()排序

-對List排序 -對List排序 -對List排序

字符串排序規則

-先數字后字母即【0-9】、【A-Z】、【a-z】 -從高位到低位即進行比較時,字符串“1000”比“200”小

Comparable and Comparator

-對象想要排序則必須是可以比較的 -Comparable即給對象定義默認的排序規則

-實現該接口表示:這個類的實例可以比較大小,進行自然排序 -定義了默認的比較規則 -其實現類需實現compareTo()方法 -compareTo()方法返回正數表示大,負數表示小,0表示相等

-Comparator臨時比較規則

-比較工具的接口,用于定義臨時比較規則,而不是默認比較規則 -其實現類需要實現compare()方法 -Comparable and Comparator都是Java集合框架成員

Student改動部分

public class Student implements Comparable<Student>{//其余代碼與之前相同不贅述@Override public int compareTo(Student o) { // 將兩個對象id的比較結果作為學生對象id的比較結果返回 return this.id.compareTo(o.id); }}

StudentComparator

import java.util.Comparator;public class StudentComparator implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { // 按照名字排序 return o1.getName().compareTo(o2.getName()); }}

CollectionTest

import java.util.ArrayList;import java.util.Collections;import java.util.HashSet;import java.util.List;import java.util.Random;import java.util.Set;import org.omg.CORBA.PRIVATE_MEMBER;/* * 1.通過Collection.sort()方法,對integer泛型的List進行排序; * 2.對String泛型的List進行排序; * 3.對其他類型的List進行排序。 */public class CollectionTest { /* * 通過Collection.sort()方法,對integer泛型的List進行排序; 插入十個100以內的隨機數,調用sort()方法排序。 */ public void integerSort() { System.out.println("調用integerSort"); List<Integer> integers = new ArrayList<Integer>(); // 插入10個100以內的不重復隨機整數 Random random = new Random(); Integer kInteger; for (int i = 0; i < 10; i++) { do { kInteger = random.nextInt(100); } while (integers.contains(kInteger)); integers.add(kInteger); System.out.println("成功添加:" + kInteger); } System.out.println("--------排序前----------"); for (Integer integer : integers) { System.out.println(integer); } System.out.println("--------排序后----------"); // 進行排序 Collections.sort(integers); for (Integer integer : integers) { System.out.println(integer); } } /* * 對String進行排序 創建String泛型的List,添加三個亂序的String元素 */ public void stringSort() { System.out.println("調用stringSort"); List<String> strings = new ArrayList<String>(); strings.add("abc"); strings.add("ggg"); strings.add("hhd"); System.out.println("--------排序前----------"); for (String string : strings) { System.out.println(string); } System.out.println("--------排序后----------"); /* * 排序,因為student本身尚未實現comparable接口,所以需要讓student實現comparable接口 * 并重寫compareTo方法,否則會報錯 */ Collections.sort(strings); for (String string : strings) { System.out.println(string); } } // 對Student進行排序 public void studentSort(){ System.out.println("調用studentSort"); Set<Course> courses = new HashSet<Course>(); List<Student> students = new ArrayList<Student>(); students.add(new Student("1", "lc", courses)); students.add(new Student("3", "shz", courses)); students.add(new Student("2", "cjl", courses)); System.out.println("--------排序前----------"); for (Student student : students) { System.out.println(student.getId()+"/"+student.getName()); } System.out.println("--------按id排序后----------"); // 排序 Collections.sort(students); for (Student student : students) { System.out.println(student.getId()+"/"+student.getName()); } System.out.println("--------按name排序后----------"); Collections.sort(students, new StudentComparator()); for (Student student : students) { System.out.println(student.getId()+"/"+student.getName()); } } public static void main(String[] args) { CollectionTest collectionTest = new CollectionTest(); collectionTest.integerSort(); collectionTest.stringSort(); collectionTest.studentSort(); }}

調用integerSort 成功添加:29 成功添加:75 成功添加:60 成功添加:74 成功添加:39 成功添加:57 成功添加:11 成功添加:5 成功添加:0 成功添加:15 ——–排序前———- 29 75 60 74 39 57 11 5 0 15 ——–排序后———- 0 5 11 15 29 39 57 60 74 75 調用stringSort ——–排序前———- abc ggg hhd ——–排序后———- abc ggg hhd 調用studentSort ——–排序前———- 1/lc 3/shz 2/cjl ——–按id排序后———- 1/lc 2/cjl 3/shz ——–按name排序后———- 2/cjl 1/lc 3/shz

Java集合框架總結

-Collection接口

List -ArrayListSet -HashSetCollections工具類 -Comparator接口 -Comparable接口Map接口
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 保靖县| 开封市| 内乡县| 开平市| 调兵山市| 宝清县| 武邑县| 天气| 梓潼县| 鄱阳县| 溧阳市| 彝良县| 红河县| 大荔县| 黔江区| 咸宁市| 巴彦县| 武胜县| 乌海市| 巴中市| 拜城县| 临安市| 阳新县| 德令哈市| 西藏| 衡阳市| 洞口县| 浦江县| 平谷区| 克拉玛依市| 即墨市| 襄汾县| 中西区| 肥西县| 巧家县| 紫阳县| 拉萨市| 定兴县| 苍山县| 常宁市| 青田县|