- In the code block that catches the exception (the code in try {}), if the previous code has an exception, the following code will not be executed.
- The getMessage() method is used to get information about the exception time.
- printStackTrace() is used to track the contents of the execution stack when the exception time occurs.
HashSet
- It stores the elements in the collection according to the Hash algorithm, so it has good access and search performance.
- characteristic:
- The rehearsal order of elements cannot be guaranteed
- Non repeatable
- HashSet is not thread safe.
- Collection elements can use null
- HashSet determines the condition that two objects are equal: the two objects are compared through the equals() method, and the hashCode() method of the two objects returns the value page.
- Demo:
public class HashSetDemo { public static void main(String[] args) { Set s = new HashSet(); //Add element s.add(1); s.add("a"); //Removing Elements s.remove(1); //Include an element System.out.println(s.contains("a")); s.clear(); s.add("a"); s.add("b"); //The first traversal method is recommend ed for (Object o : s) { System.out.println(o); } //Traversal using iterators Iterator iterator = s.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } //The size of the collection System.out.println(s.size()); //Collection stores objects of the same type Set<Integer> intSet = new HashSet<Integer>(); } }
TreeSet
- TreeSet ensures that collection elements are in a sorted state.
- Two sorting methods are supported: natural sorting and custom sorting. The default is natural sorting.
- Objects of the same type must be placed, otherwise type conversion exceptions may occur.
public class TreeSetDemo { public static void main(String[] args) { Set<Integer> s = new TreeSet<Integer>(); s.add(5); s.add(3); s.add(2); s.add(1); System.out.println(s); s.remove(5); System.out.println(s.contains(2)); for (Integer integer : s) { System.out.println(integer); } //In the constructor of TreeSet, we need to pass in - > New person(), because we have customized comparetor Set<Person> personSet = new TreeSet<>(new Person()); Person p1 = new Person(12,"bob"); Person p2 = new Person(18, "alice"); personSet.add(p1); personSet.add(p2); System.out.println("order -> "); for (Person person : personSet) { System.out.println(person.age + " : " + person.name); } } } class Person implements Comparator<Person> { public int age; public String name; public Person(){ } public Person(int age, String name){ this.age = age; this.name = name; } @Override public int compare(Person p1, Person p2) { if (p1.age > p2.age) { return 1; } else if(p1.age < p2.age) { return -1; } else { return 0; } } }
List and ArrayList
- List represents an ordered and repeatable collection of elements, and each element in the collection has its corresponding sequential index.
- List allows you to use duplicate elements, and you can access collection elements at a specified location through an index.
- List sets the index of elements by default in the order in which they are added.
- The List collection adds some methods to manipulate the collection elements according to the index.
public class ListDemo { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("a"); list.add("c"); list.add("b"); System.out.println(list); //Access elements by index System.out.println(list.get(2)); //Can repeat list.add("a"); System.out.println(list.size()); //Inserts an element at the specified index subscript list.add(1,"f"); System.out.println(list); List<String> l = new ArrayList<>(); l.add("123"); l.add("456"); //Inserts a collection at the specified location list.addAll(2,l); System.out.println(list); //Gets the index subscript of the first and last occurrence of the specified element in the collection System.out.println(list.indexOf("f")); list.add("f"); System.out.println(list.lastIndexOf("f")); //Removes data based on the specified index subscript list.remove(0); System.out.println(list); //Modifies the value of the specified index list.set(0,"fffff"); System.out.println(list); //Intercept list set, sublist (I, j), I - > j, excluding j List<String> subList = list.subList(1, 3); System.out.println(subList); //Length of collection System.out.println(list.size()); } }
Map
- Map is used to save data with mapping relationship. Therefore, two groups of values are saved in the map set. One group of values is used to save the Key in the map and the other group is used to save the Value in the map.
- Duplicate keys in a Map are not allowed, that is, any two keys of the same Map object return false in the comparison through the equals method.
- HashMap is mainly used
public class HashMapDemo { public static void main(String[] args) { Map<String, Integer> hashMap = new HashMap<>(); hashMap.put("a",1); hashMap.put("b",2); hashMap.put("c",3); System.out.println(hashMap); //Value based on key System.out.println(hashMap.get('b')); //Remove key value pairs according to key hashMap.remove("a"); //Size of map System.out.println(hashMap.size()); //Judge whether the current map contains the specified key and value System.out.println(hashMap.containsKey("b")); System.out.println(hashMap.containsValue(2)); //ergodic //The first traversal method Set<String> keySet = hashMap.keySet(); for (String key : keySet) { System.out.println("key:"+key+", Value:"+hashMap.get(key)); } //The second traversal method Set<Map.Entry<String, Integer>> entries = hashMap.entrySet(); for (Map.Entry<String, Integer> entry : entries) { System.out.println(entry.getKey()+"->" +entry.getValue()); } } }
- TreeMap
- TreeMap can ensure that all key value pairs are in an orderly state. By default, it is sorted according to the key of the Map.
Collection
public class CollectionDemo { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("b"); list.add("c"); list.add("d"); list.add("b"); //Replace the old value with the new value Collections.replaceAll(list,"c","a"); System.out.println(list); //Returns the number of occurrences of the specified element int frequency = Collections.frequency(list,"b"); System.out.println(frequency); System.out.println(list); //Swap positions 0 and 3 Collections.swap(list,0,2); System.out.println(list); //Returns the largest element in the list String max = Collections.max(list); System.out.println(max); //Reverse the order of the list collection Collections.reverse(list); System.out.println(list); //Randomly sort the list set Collections.shuffle(list); System.out.println(list); //list set dictionary in ascending order Collections.sort(list); System.out.println(list); Student s1 = new Student(12, "a"); Student s2 = new Student(14, "b"); Student s3 = new Student(13, "c"); ArrayList<Student> studentArrayList = new ArrayList<>(); studentArrayList.add(s1); studentArrayList.add(s2); studentArrayList.add(s3); for (Student student : studentArrayList) { System.out.println(student.age+"->"+student.name); } //Sort by age Collections.sort(studentArrayList,new Student()); for (Student student : studentArrayList) { System.out.println(student.age + "->" + student.name); } } } class Student implements Comparator<Student>{ int age; String name; public Student(int age, String name) { this.age = age; this.name = name; } public Student() { } @Override public int compare(Student t1, Student t2) { if(t1.age > t2.age) { return 1; } else if(t1.age < t2.age) { return -1; } else { return 0; } } }