You can pay attention to the author's account and the Java notebook from scratch. You can also go to the author's blog Garden to learn from the catalog. This film will be based on the black horse programmer job class video for learning and data sharing, and take notes and their own views. Welcome to study and discuss together.
[learn Java notes from scratch] directory
Class Set
Set set features:
Out of order (the order of storage and read may be different)
Duplicate not allowed (element unique required)
No index
Example: using HashSet to store custom objects and traverse
import java.util.HashSet; public class HashSetDemo2 { public static void main(String[] args) { Student s1 = new Student("Big Joe", "18"); Student s2 = new Student("Little Joe", "17"); Student s3 = new Student("Little Joe", "17"); HashSet<Student> hs = new HashSet<Student>(); hs.add(s1); hs.add(s2); hs.add(s3); for (Student student : hs) { System.out.println(student); } } } //Output result Student [name=Little Joe, age=17] Student [name=Big Joe, age=18] Student [name=Little Joe, age=17]
Why can't HashSet be repeated, but s2 and s3 are the same, but they are all saved in?
By looking at the source code of the add() method, we found that:
The add() method of the HashSet first uses each element in the current set to compare the hash value with the newly added element. If the hash value is different, add the new element directly. If the hash value is - sample, compare the address value or use the equals method to compare
Compared with the results of sample, it is considered that it is heavy summer without adding. If the comparison results of all elements in set are different, add.
But this is not what we want. We just like let him have the features of hashset, which can not be repeated. What should we do?
Re equal and hashcode
Right click - > resource - > generate hashcode and equal
Generate the following code
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((age == null) ? 0 : age.hashCode()); 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 (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (age == null) { if (other.age != null) return false; } else if (!age.equals(other.age)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; }
Run the main function again, and the result is as follows
Student [name = Big Joe, age=18] Student [name = Joe, age=17]
Collections class
What's the difference between Collection and Collections?
Collection is the top level of collection system, which contains the commonness of collection system
Collection s is a tool class. Methods are used to operate Collections
common method
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { } private static void mothed7() { //static void swap(List list, int i, int j): swap two indexes in the specified list List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(4); Collections.swap(list, 0, 1); System.out.println(list); } private static void method6() { //Static void sort (list < T > list): sort according to the natural order of the elements in the list List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(4); list.add(3); list.add(2); Collections.sort(list); System.out.println(list); } private static void method5() { //static void shuffle(List list): random permutation List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(4); Collections.shuffle(list); System.out.println(list); } private static void method4() { //static void reverse(List list): reverse List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(4); Collections.reverse(list); System.out.println(list); } private static void method3() { //static void fill(List list, Object obj): fills all elements of the specified list with the specified object List<String> list = new ArrayList<String>(); list.add("hello"); list.add("world"); list.add("java"); System.out.println(list); Collections.fill(list, "android"); System.out.println(list); } private static void method2() { //static void copy(List dest, List src): to overwrite the data in the source list to the target list //Note: the length of the target list is at least equal to the length of the source list //Create source list List<String> src = new ArrayList<String>(); src.add("hello"); src.add("world"); src.add("java"); //Create target list List<String> dest = new ArrayList<String>(); dest.add("java"); dest.add("java"); dest.add("java"); dest.add("java"); Collections.copy(dest, src); System.out.println(dest); } private static void method() { //Static int binary search (list, object key) uses binary search to find the index position of the specified element in the specified list List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(4); int index = Collections.binarySearch(list, 4); System.out.println(index); } }
Doulandlord licensing system
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Landlords { public static void main(String[] args) { //Generate a deck of playing cards ArrayList<String> c = new ArrayList<String>(); String[] arr = {"Spade","Heart","Square sheet","Grass flower"}; String[] arr2 = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; for (int i = 0; i <4; i++) { for (int k = 0; k < 13.; k++) { c.add(arr[i]+arr2[k]); } } c.add("king"); c.add("Xiao Wang"); // System.out.println(c); // System.out.println(c.size()); //Upset Collections.shuffle(c); // System.out.println(c); //Divided into three groups. List<String> c1 = new ArrayList<String>(); List<String> c2 = new ArrayList<String>(); List<String> c3 = new ArrayList<String>(); c1 = c.subList(0, 17); c2 = c.subList(17, 34); c3 = c.subList(34, 51); //sort Collections.sort(c1); Collections.sort(c2); Collections.sort(c3); //output System.out.println("Game player 1:"+c1); System.out.println("Game player 2:"+c2); System.out.println("Game player 3:"+c3); //A hand List<String> c4 = new ArrayList<String>(); c4 = c.subList(51, 54); System.out.println("A hand"+c4); } }
Class Map
Requirement: realize data storage with corresponding relationship between student number and name (one-to-one: one student number corresponds to one name)
In order to reflect this kind of data with corresponding relationship, we can use what we have learned before, but it is a little unchanged, so java also provides me with a special set for storing corresponding relationship Map
Map: the object that maps the key to the value. A map cannot contain duplicate keys; each key can be mapped to at most one value
What's the difference between Map and Collection?
Map: it's a two column set. Common expressions process data with corresponding relationships. key cannot be repeated. We also call it a couple set
Collection: it is a single column collection. Collection has different subsystems. Some of them are allowed to be duplicate and have index order. Some of them are not allowed to be duplicate and disordered. Then we also call it bachelor collection
Map method
import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Set; /* * Map Common functions of: * Mapping function: * V put(K key, V value) * Get function: * V get(Object key) * int size() * Judgment function: * boolean containsKey(Object key) boolean containsValue(Object value) boolean isEmpty() * Delete function: * void clear() * V remove(Object key) * * Traversal function: * Set<Map.Entry<K,V>> entrySet() * * * Set<K> keySet() * Collection<V> values() */ public class MapDemo2 { public static void main(String[] args) { // Create Map object Map<String, String> map = new HashMap<String, String>(); map.put("001", "Zhang San"); map.put("002", "Li Si"); // V put(K key, V value): map the key to value. If the key exists, overwrite the value and return the original value // System.out.println(map.put("001", "Zhang San")); // System.out.println(map.put("002", "Li Si")); // System.out.println(map.put("001", "king five")); // void clear(): clear all correspondence // System.out.println(map); // map.clear(); // System.out.println(map); // V remove(Object key): delete the corresponding relationship according to the specified key and return the value corresponding to the key. If the deletion is not successful, return null // System.out.println(map.remove("005")); // System.out.println(map.remove("001")); // boolean containsKey(Object key): judge whether the specified key exists // System.out.println(map.containsKey("003")); // System.out.println(map.containsKey("001")); // boolean containsValue(Object value): judge whether the specified value exists // System.out.println(map.containsValue("king five")); // System.out.println(map.containsValue("Zhang San")); // boolean isEmpty(): judge whether there is a corresponding relationship // System.out.println(map.isEmpty()); // map.clear(); // System.out.println(map.isEmpty()); // int size(): returns the number of corresponding relationships // System.out.println(map.size()); // V get(Object key): returns the corresponding value according to the specified key // System.out.println(map.get("002")); // Set < map. Entry < K, V > > entryset(): output all mapping relationships // System.out.println(map.entrySet()); // System.out.println(map); // //Set < k > keyset(): returns all keys in the form of set // Set<String> keys = map.keySet(); // for (String key : keys) { // System.out.println(key); // } // System.out.println("-----------"); // // //Collection < V > values(): all values are returned in the form of collection. List cannot be used // Collection<String> values = map.values(); // for (String value : values) { // System.out.println(value); // } } }
Two traversal methods of Map
import java.util.HashMap; import java.util.Map; import java.util.Set; public class MapDemo3 { public static void main(String[] args) { Map<String,String> m= new HashMap<String,String>(); m.put("Liu Bei", "Sun Shang Xiang"); m.put("Zhu Geliang", "Huang Yue Ying"); m.put("Cao Pi", "Zhen Ji"); // method(m); // method1(m); } private static void method1(Map<String, String> m) { /* * The second kind of traversal * Object oriented method * entry It is an internal class of map. It has two variables, key and value, which can get member variables like a class */ Set<Map.Entry<String, String>> set = m.entrySet(); for (Map.Entry<String, String> entry : set) { String key = entry.getKey(); String value = entry.getValue(); System.out.println("husband:"+key+"---daughter-in-law:"+value); } } private static void method(Map<String, String> m) { /* * The first kind of ergodic * Get the husbands first * Traversing husband * Let the husband find his wife */ Set<String> s = m.keySet(); for (String key : s) { String value = m.get(key); System.out.println("husband:"+key+"---daughter-in-law:"+value); } } }