Fifteenth Day of Java Learning

Keywords: Java less ascii

Map: Independent Interface

HashMap: Use hash tables to ensure that keys do not repeat: int hashCode() boolean equals(Object obj)

TreeMap: Use binary tree, sort by key, Comparable < E > int compare To (E) Comparator < E > int compare (E 1, E 2)

Note: compare To () or comparison method return value of 0 is considered to be the same key

2. Array to string:
  Arrays.toString()
Example: int[] arr={2,4,5,6,7,8};
  String ss = Arrays.toString(arr);

3. Array Transfer Set:

  Arrays.asList()
Disadvantage: Collections converted by arrays cannot add or delete data, and cannot be expanded
Example: String[] arr2 = {"hello","haha","hehe"};
  List<String> list=Arrays.asList(arr2);

Purpose: We can make use of the rich methods of set sets.


4. Set to Array:

  Object[] toArray()
  <T> T[] toArray(T[] a)

Attention:
1. Given array length greater than set size, use a given array
2. Given that the array length is less than the collection size, a new array is created and returned
3. The length of the given array is best the same as the size of the set.
Example: String[] arr=list.toArray(new String[list.size()]);
Purpose: When you don't want to add or delete data at will

5. Tool classes for collections:

  <T extends Comparable<? super T>> void Collections.sort(List<T> t)

Collections.sort(List<T> t)
Collections.sort(List<T> t,Comparator c)
Comparator Collections.reverseOrder()
Collections.max(Collection list)
Collections.reverse(List list);

 1 Example:
 2 import java.util.*;
 3 
 4 class ComByLeng implements Comparator<String>
 5 {
 6 public int compare(String s1,String s2){
 7 int n = s1.length()-s2.length();
 8 return n==0?s1.compareTo(s2):n;
 9 }
10 }
11 class Demo6 
12 {
13 public static void main(String[] args) 
14 {
15 //Use list Collection, sorting
16 //static <T extends Comparable<? super T>> void sort(List<T> list) 
17 
18 List<Student> list = new ArrayList<>();
19 list.add(new Student("lisi",21));
20 list.add(new Student("zhaosi",20));
21 list.add(new Student("wangsi",18));
22 
23 Collections.sort(list);
24 sop(list);
25 
26 List<String> list = new ArrayList<>(); //String implements Comparable
27 list.add("zwoieurowie"); 
28 list.add("wlksjioerwerwreserere");
29 list.add("alks");
30 
31 //Collections.sort(list);//The default ordering rules for objects in a collection are used
32 //Collections.sort(list,Collections.reverseOrder()); //Sort strings from large to small
33 //sop(list);
34 
35 //Collections.sort(list,new ComByLeng());//Sorting with custom sorting,Sort by string length
36 //Collections.sort(list,Collections.reverseOrder(new ComByLeng())); //Sort by string length from long to short
37 //sop(list);
38 
39 //static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll) 
40 //String max = Collections.max(list);//Finding the Maximum in a Set
41 //sop(max);
42 
43 Collections.reverse(list);//Inversion set
44 sop(list);
45 }
46 public static void sop(Object obj){
47 System.out.println(obj);
48 }
49 }

6. Objects in a collection are not duplicated and orderly:

  LinkedHashSet : Collections.synchronizedSet(new LinkedHashSet())

  LinkedHashMap : Collections.syncharonizedMap(new LinkedHashMap())
Note: Thread is not safe in itself, but thread-safe can be defined by the Collections.synchronizedMap() method

 

Example:
import java.util.*;
class Demo7 
{
public static void main(String[] args) 
{
//Hope that the objects in the collection are not duplicated and that they are orderly

//Two kinds of data structures are used to achieve data ordering 123 123
//Thread insecure
LinkedHashSet<String> lhs = new LinkedHashSet<>();
lhs.add("java01");
lhs.add("java02");
lhs.add("java03");

//sop(lhs);

//Creating thread-safe LinkedHashSet object
Set<String> set=Collections.synchronizedSet(new LinkedHashSet<String>());
set.add("java05");
set.add("java06");
set.add("java07");

//sop(set);



LinkedHashMap<String,String> lhm = new LinkedHashMap<>();
lhm.put("name","lisi");
lhm.put("age","20");
lhm.put("address","shanghai");

//sop(lhm);

//Creating thread-safe LinkedHashMap object
Map<String,String> map = Collections.synchronizedMap(new LinkedHashMap<String,String>());
map.put("name","lisi");
map.put("age","20");
map.put("address","shanghai");

sop(map);


//SortedSet----TreeSet SortedMap----TreeMap
}


public static void sop(Object obj){
System.out.println(obj);
}
}

7.SortedSet ---TreeSet
 SortedMap ---TreeMap

8. Enhanced for cycle:

For (Data Type Variable Name: Collection Set or Array Traversed){}
Note: Using an enhanced for loop for arrays, subscripts cannot be manipulated


9. Variable parameters: the principle of arrays
Definition: void show (int... a) {}
  List<T> asList(T... a)

Note: Variable parameters must be at the end of the parameter list

 


10. Static import: import static java.util.Arrays;
Function: Class names can be omitted, such as import static java.util.Arrays; method names can be written directly without Arrays when calling static methods of Arrays
Note: With the same method name, the class name cannot be omitted.
The method name is the same and the class name cannot be saved.
Class names are the same, so the package names can not be omitted, which can be distinguished by the package names.


  

Properties: Is a Map Collection classes,The default key and value must be String Type, Storage Properties
Properties pro = System.getProperties();//What we get is the set of system attributes.

Set<String> keys=pro.stringPropertyNames();//Get the set of all keys in the set

for(String key:keys)
{
String v=pro.getProperty(key);
System.out.println(key+"="+v);
}

pro.setProperty("line.separator","\r\n");//Amount to Map Medium put Overlay the key-value pairs of the same name in the set, not modifying the original data, but modifying the data in the set.

 

 

11.IO stream: (input output) Input and output stream, used for data transmission between devices

Classification of IO flows:
Direction: input stream, output stream (relative to memory)

According to the data of operation:
Byte Stream: You can manipulate any type of data, text, pictures, audio, video

Character streams: Only text-type data can be manipulated, and character streams are coded on the basis of byte streams.
So character streams are based on byte streams.

Common Coding: ASCII, ISO8859-1 (European Coding Table), gb2312,gbk,UTF-8


12. Byte stream (input, output)
The parent class of InputStream byte input stream
The parent class of OutputStream byte output stream

13. Character Stream (Input, Output)
The parent class of Reader character input stream
The parent class of Writer character output stream

14 Character Stream: Device: Hard Disk
Using FileWriter
Create a file output stream object that is associated with the file. The file can exist in advance or does not exist. If it does not exist, it will be created automatically.

    FileWriter fileWriter=ewn FileWriter("temp.txt");
2. Write data to a file using the file output stream object (to throw an exception)
fileWriter.write("abc"); //Because a table lookup process is required, the data is written to the buffer inside the stream object first.

3. Brush data from buffer to file
    fileWriter.flush()

4. Close the stream (you can't write data after closing it, and refresh when closing the stream, that is, flush())
    fileWriter.close()

Posted by thryb on Sun, 25 Aug 2019 07:45:44 -0700