Java Collection framework
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 1) ArrayList(a collection of mutable arrays) and LinkedList(a collection of linked lists) are the implementation classes of List(a collection of ordered, not unique data) HashSet and TreeSet —- Unordered Set(elements added to a Set have no indexed position, 1) Implement classes for HashMap and TreeMap(sorting functions) 2) Store data as key-value pairs. 4. Collections and Arrays are the Collections framework utility classes
A List of objects
List< element type > List =new ArrayList< element type >(); < element type > : indicates that only object elements of the specified type can be placed in the collection. Example:
List<String> ls=new ArrayList<String>();
List<Student> ls=new ArrayList<Student>();
Copy the code
1) Add objects to collection list.add(object elements); Set list. Set (index position, object element); Remove list. Remove (index position) from collection; Element type object =list.get(index position); Int size=list.size(); Boolean flag= list.isempty (); Flag is true, indicating that there is no element. Flag is false, indicating that there are elements
Common methods and usage of Set
Myset =new HashSet< element type >();
1) Add an object to myset.add(object element); 2) Remove the specified object myset.remove(object element) from the collection; Int size=myset.size(); Object[] obs= myset.toarray (); Boolean flag=myset.isEmpty(); Iterator (1) Iterator< element type > it=myset.iterator(); Iterator (1) Iterator< element type > it=myset.iterator(); Boolean flag=it.hasNext(); Element type object =it.next(); Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator Iterator
for(Element type object: collection or array){...... }Copy the code
Iv. Map set
Map< primary key, value type > Map =new HashMap< primary key, value type >(); 1) Add and modify elements map.put(primary key, value object); Remove object map.remove(primary key); Int size=map.size(); Set
5. Common methods of the Collections class
Collections differs from Collection in that the former is an operation class for Collections, while the latter is the common static method provided by the Collection interface Collections sort(): sort binarySearch() : find Max ()\min(): find Max ()\min(): find minimum values
Java generic collections
Generics allow you to specify a type parameter when defining a class or interface. This parameter will determine the format of the generic when declaring variables and creating objects: Typical generic sets ArrayList
, HashMap
,
,
represent the types of elements in the generic set. Except that the types of elements in the set are specified, the usage of generic sets is exactly the same as that of the collections we learned earlier. 1. Move the ClassCastExcpetion that occurs at runtime to compile time. Convenient for programmers to solve problems, so that the run-time problems reduce 2, avoid the trouble of forced conversion
Java wrapper classes
Java provides two type systems, primitive types and reference types. Using primitive types is efficient. However, in many cases, objects are created because objects can do more than one thing. Wrapper classes convert primitive type data into objects, and each primitive type has a corresponding wrapper class in the java.lang package
2. Wrapper class inheritance
3, between the basic type and the corresponding packaging class object, back and forth conversion process is called “packing” and “unpacking” :
packing
Unpacking: Converts wrapper objects to their corresponding primitive types
Viii. Other practical classes
The java.lang.Math class provides common mathematical operations and two static constants E(the base of the natural logarithm) and PI(the value of PI). The Random class java.util.Random class generates Random numbers The String class, located in the java.lang package, has a number of methods for calculating the length of strings, comparing strings, concatenating strings, extracting strings, and so on (1). The String class provides the length() method and determines the length of strings (2). The String class provides equals(). Equals (): checks whether the contents of two string objects are identical. ==: Checks whether the contents of two string objects are identical. EqualsIgnoreCase (); toLowerCase(); toUpperCase(); Public int indexOf(int ch) public int indexOf(String value) public int indexOf(String value) Public int lastIndexOf(int ch) public int lastIndexOf(String value) -1 public String beginIndex (int beginIndex) public String beginIndex (int beginIndex) Int endIndex) public String trim() // Returns a copy of the call String without any Spaces before or after
Use of Java date classes
Date class (1) Create Date class object Date =new Date(); String sdate= date.tolocaleString (); sdate= date.tolocaleString (); 2) Get the year, month and day
int year=date.getFullYear();
int month=date.getMonth();
int day=date.getDate();
Copy the code
1) Create a date-formatting class for the SimpleDateFormat file in the java.text package. SimpleDateFormat dateFormat=new SimpleDateFormat(” YYYY-MM-DD “); Date = dateformat. parse(dateformat. parse); String sdate= dateformat. format(an object of Date type);
10. Input and output streams
1. Definition of flows Data in memory flows in a specified order and direction, called flows. 2
3. Input stream
4. Output flow
5. File class File
File File =new File(" File path ");
File file=new File("d:\\aa.txt");
boolean flag=file.exists();
boolean flag=file.isFile();
flag=file.isDirectory();
String fname=file.getName();
String path=file.getPath();
String path=file.getAbsolutePath();
file.createNewFile();
file.mkdir();
file.delete();
long size=file.length();
Byte stream
FileOutputStream (1) FileOutputStream (2) BufferedOutputStream (BufferedOutputStream (3) ObjectOutputStream class
FileOutputStream fout=new FileOutputStream(File object or File destination path [,true]); // True means incremental write (append), false means overwrite (2) method
'fout.write(value or variable of type int); // Write data byte by byte to file 'fout.write(byte[]); // Write the byte array data to the file fout.flush(); // Clear buffer data fout.close(); // Close the output streamCopy the code
(1)FileInputStream– the number of bytes in a file, reads the specified location of the file data. Input to application memory (2)BufferedInputStream– buffer input stream class to speed up the reading of input streams (3)ObjectInputStream– ObjectInputStream class to read stored Java object binary data from a file, FileInputStream fin=new FileInputStream(file object or file path to read); Int leng=fin.available(); int leng=fin.available(); (2) Read input stream data into byte array fin.read(byte array object); Int b=fin.read(); Fin.close ();
Character stream
2. Character stream can only process data of plain text documents (.txt,.html,.xml and other information documents that can be opened and understood with Notepad). 3. Character input stream Reader class (superclass of character input stream class) FileReader fr=new FileReader(file object or file path to read); 2) Common methods
int c=fr.read(); // Read fr.read(character array object) character by character; // Read data from the stream into the character array fr.close();Copy the code
FileWriter — file character output stream BufferedWriter– buffer character output stream (2) Common method 1) Create the output stream class FileWriter Fw =new FileWriter(file object or file path [,true]); True indicates incremental write 2) common method
Fw. write(character variable); // Write character by character to the file fw.write(character array object); Fw.write (string variable or value); // Write the string to the file store fw.flush(); // Refresh buffer fw.close(); / / closeCopy the code
Byte flow character stream
InputStreamReader isr=new InputStreamReader isr=new InputStreamReader This allows you to read character data in different encoding formats
Osw =new OutputStreamWriter(byte output stream object,” encoding format “); This allows you to write character data in different encoding formats
Serialization and deserialization
Serialization defines the process of converting a Java object into binary data. This process is called serialization
Public class implements Serializable{attributes... . }Copy the code
Deserialization The process of converting binary Java object streams stored in files into Java objects themselves is called deserialization