@SuppressWarnings ("resource") is used to suppress resource disclosure warnings. For example, using the IO class, the last is not closed.
Set Set Features:
1) unordered, not random
2) Element Unique
3) No subscript
Note: The Collection List Set is an interface
Note: Because the set has no subscript, the value cannot be used for, only with iterators
HashSet: The internal structure is a hash table, so the element is unique, unordered, without subscript.
TreeSet: The interior is a two-fork tree, the elements can be compared, can be compared in natural order, but also by the programmer to customize the comparison mode, no
Standard.
Linkedhashset: The internal structure is a hash table and a linked list, so ordered, element unique, no subscript.
HashSet:
hashset<string> ss = new hashset<string> ();
Ss.add ("a");
Ss.add ("K");
Ss.add ("D");
Ss.add ("K");
iterator<string> its = Ss.iterator ();
while (Its.hasnext ()) {
System.out.println (Its.next ());
}
Result: unordered
Linkedhashset:
linkedhashset<string> s = new linkedhashset<string> ();
S.add ("a");
S.add ("G");
S.add ("D");
S.add ("G");
Iterator<string> it = S.iterator ();
while (It.hasnext ()) {
System.out.println (It.next ());
}
Result: A G D is ordered (taken out in order of deposit)
The
Map interface features: A key (key), a value, and a double appearance.
Methods in Map interface
1. Add v put (K key, v value)
2. Remove the V Remove (Object key)
void Clear ()
3. Judge Boolean IsEmpty ()
Boolean ContainsKey (Object key)
Boolean Containsvalue (object value)
4. Get V get (object key)
int size ()
set<k> keySet ()
Map value
//Not heavy, no order, no subscript, no iteration, value in double appearance, key and value may be empty (can be empty at the same time), if key is the same, it will overwrite
lid value
Map <String,String> map = new hashmap<string,string> ();
Map.put ("1", "AA");
Map.put ("2", "BB");
Map.put ("3", "CC");
Map.put ("4", "FF");
Map.put ("5", "FF");
//map.get ("5");//One fetch
set<string> set = Map.keyset ();//The current Set is stored in key
Iterator <String> it = Set.iterator ();
while (It.hasnext ()) {
String key = It.next ();
String value = Map.get (key);
System.out.println ("key=" +key+ ", value=" +value);
}
The difference between HashMap and Hashtable
HashMap: Thread is unsafe, fast, and allows null keys to be stored, null values.
Hashtable: Thread safe, slow, not allowed to hold null key, null value, has been replaced by HashMap.
TreeMap: Sorts the keys, with the same sort principle as treeset.
New usage for the FOR loop:
arraylist<string> list = new arraylist<string> ();
List.add ("a");
List.add ("B");
1) General for
for (int i=0;i<list.size (); i++) {
System.out.println (List.get (i));
}
2) Enhanced for
for (String temp:list)
{System.out.println (temp);
}
Note: 1) enhanced for to be used with generics
2) Enhanced for no subscript
Why should we have a wrapper class: easy for us to manipulate data in basic data types
Attention:
char (character type) Character
int (integer) integer
The rest of the first letter capitalized
Basic data Type------------string
1) + string Join character
int i = 1;
String str = i + "";
2) There are methods in String
String str2=string.valueof (i);
string-------base data type
String str3= "a";
int A=integer.parseint (STR3);
Note: The string must be a number or an error numberformatexception
Wrapper class------Basic data type
Integer itsss = new Integer ("20");
int age = Itsss.intvalue ();
Automatic box packing and automatic unpacking
Formerly: int m = 4;
int n = 5;
int b = m + N;
If: integer m= new Integer (4);
int B=m.intvalue () +5; --->m is changed from wrapper class to basic data type +5
Now: JDK5.0 after the new function
Integer p=4;//Auto-boxing (basic data type into wrapper class)
int b=p+5; Automatic unpacking (wrapper class to basic data type)
list<integer> list = new arraylist<integer> ();
List.add (1);//Auto-boxing (basic data type turned into wrapper class)
Benefits: Easy Program writing.
The Equals () method in a wrapper class
The wrapper class overrides the Equals () method in object, comparing whether the values are equal
Integer a1= new Integer ("20");
Integer a2= new Integer (20);
System.out.println (A1.equals (A2));//true
The CompareTo () method in a wrapper class
public int CompareTo (Integer anotherinteger)
Integer a1= new Integer ("40");
Integer a2= new Integer (30);
System.out.println (A1.compareto (A2));
The value of the A1 value ==A2 returns 0
A1 The value of <a2 returns a negative number
A1 The value of >A2 returns a positive number
Object class: Is the root class of all classes, all methods in this class, all classes inherit.
equals and = =
1) Comparison of basic data types:
3==3 true (the value is compared)
2) Comparison of objects:
New person () = = new Person () false (compared to memory address)
3) Comparison of objects:
New person (). Equals (new person ()) False (compared to memory address)
4) equals in the String class, overriding equals in object, comparing the contents of a string
= = The address is compared
Hashcode
Returns the hash code value for this object:
New person (). Hashcode ()-->int value 25860399 object's address 10 binary
Integer.tohexstring (25860399)->10 binary number converted to 16 binary number
Similar to [email protected]
The hash code value is the address value of the object.
GetClass
Object name. GetClass (); Gets the package name and class name of the current object
Tostring
Fortest f6 = new Fortest ();
System.out.println (f6); F6 is equivalent to F6.tostring (), which calls the ToString () method in the object class by default
Attention:
If we rewrite ToString () in the Fortest class, then System.out.println (F6); The value after the output is the value in the overridden method.
1) Chang (directly assigned value):
String a = "abc"; --Create an "ABC" string in the pool
A= "BCD"---> "Create a" BCD "string in a pool
A = "abc"; ---> Created "abc" string in the pool
2) String a = new string ("AA");----> "AA" is not in the pool in the object
Common methods of the string class
1. Gets the length of the string int length ()
2. Get character char charAt (int index) based on location
3. Gets the position of the first occurrence in the string based on the character
int indexOf (String str)
int indexOf (String str, int fromIndex) to find from the specified position
4. Gets the position of the first occurrence in the string based on the character
int lastIndexOf (String str)
int lastIndexOf (String str, int fromIndex) to find from the specified position
5. Get a subset of strings in a string, also called substrings
String substring (int beginindex)
String substring (int beginindex, int endIndex) Beginindex-start index (including) EndIndex-
End index (not included).
6. Conversion
1) Convert string to string array (cut)
String[] Split (String regex), # space \.
2) Convert string to character array
Char[] ToCharArray ()
3) Convert string to byte array
Byte[] GetBytes ()
4) Capitalize all characters in the string
String toUpperCase ()
5) The characters in the string are all lowercase
String toLowerCase ()
6) Replace the contents of the string
String replace (char OldChar, char Newchar)
7) Remove the whitespace from both ends of the string
String Trim ()
8) String Connection
String concat (String str)
9) Change the base type to a string
static string valueOf (Object obj) 100 becomes a string
7. Judging
1) Compare String contents:
Boolean equals (Object anobject)
2) Do not consider case comparison string contents:
Boolean equalsignorecase (String anotherstring)
3) Whether the string contains the specified string
Boolean contains (String s)
4) Whether the string begins with the specified string to specify the end of the string
A Boolean startsWith (string prefix) tests whether this string starts with the specified prefix.
Boolean endsWith (string suffix) tests whether this string ends with the specified suffix (case-sensitive)
8. Compare
int CompareTo (String anotherstring)
StringBuffer class
is a string buffer, the function of a string buffer is to facilitate the manipulation of strings
StringBuffer class Characteristics
1. Variable length
2. Can store different types of data
3. Receive different types of data and eventually convert to strings for use
StringBuffer sb = new StringBuffer ("AA");
System.out.println (Sb.tostring ());
4. Can modify the string
StringBuffer Construction Method
StringBuffer sb = new StringBuffer (); The buffer is empty and the length is 0
StringBuffer sb = new StringBuffer ("AA"); The buffer is not empty and the length is 2
StringBuffer method
1. Add
StringBuffer Append (Boolean B)
StringBuffer Insert (int offset, Boolean b)
2. Delete
Public StringBuffer Delete (int start, int end) does not include end
StringBuffer deletecharat (int index) deletes a character from a position
3. Find
char charAt (int index) to find a character at a location
int indexOf (string str) to find the subscript for the specified string
int lastIndexOf (String str)
4. Modifications
StringBuffer replace (int start, int end, String str) does not include end
void Setcharat (int index, char ch)
5. Reversal
StringBuffer reverse ()
Finalize ()
The method that the garbage collector calls before it is reclaimed, and this method is typically used to free resources.
System.GC (): Recommend that the garbage collector work. Generally not used manually.
Math class
Static double Ceil (double A) returns the smallest integer greater than the parameter
Static double floor (double a) returns the largest integer less than the parameter
static int round (float a) rounding
Static Double pow (double A, double b) A's B-square
The static double random () returns a double with a positive number that is greater than or equal to 0.0 and less than 1.0.
System class
Returns the current time in milliseconds.
Static Long Currenttimemillis ()
static void exit (int status) terminates the currently running Java virtual machine. Status=0 Normal, 1 abnormal exit
The static void GC () "Recommended" runs the garbage collector.
Date
Current system time. Location in Java.util bag
Method:
New Date (); Get the current time display format of the system is not the Chinese way
New Date (long date); By the number of milliseconds to get month and day, tick seconds
Long GetTime ()//Gets the current number of milliseconds
System.currenttimemillis (); Gets the current number of milliseconds
Note: Conversions between date objects and milliseconds
1) Date Object--conversion----"milliseconds
Date d = new Date ();
Long lo = D.gettime ();
2) milliseconds-Convert----Date Object
Date d =new Date (LO);
DateFormat
Format time (the style of the format is fixed) position in the Java.text package
This dateformat is an abstract class and cannot create objects
The format is obtained:
Dateformat.getdateinstance (); This formatter only has a year-month-day
Dateformat.getdatetimeinstance ();//year-month-day time: minutes: seconds
dateformat.getdateinstance (int parameter)://Specify style by parameter
Example:
Date d =new Date ();
DateFormat df = dateformat.getdateinstance ();//2016-10-27 Thu Oct 27 10:57:57
CST------format------->2016-10-27
String Datestr=df.format (d);
System.out.println (DATESTR);
DateFormat df1 = Dateformat.getdatetimeinstance ();//2016-10-27 10:36:38 Thu Oct
10:57:57 CST------format------->2016-10-27 10:36:38
String dateStr1 = Df1.format (d);
System.out.println (DATESTR1);
DateFormat DF2 = dateformat.getdateinstance (dateformat.full);//October 27, 2016 stars
Period Four
String datestr2= Df2.format (d);
System.out.println (DATESTR2);
DateFormat df3 = dateformat.getdateinstance (Dateformat.long);//October 27, 2016
String datestr3= Df3.format (d);
System.out.println (DATESTR3);
DateFormat df4 = dateformat.getdateinstance (dateformat.short);//16-10-27
String datestr4= Df4.format (d);
System.out.println (DATESTR4);
DateFormat df5 = dateformat.getdateinstance (dateformat.medium);//2016-10-27
String datestr5= Df5.format (d);
System.out.println (DATESTR5);
SimpleDateFormat
Format time with the (style of the format is custom) location in the Java.text package. specifying formatting by parameters
Date d = new Date ();
SimpleDateFormat SDF = new SimpleDateFormat ("Yy--mm--dd hh-mm-ss");
String str = Sdf.format (d);
System.out.println (str);
Results: 16--03--05 09-20-22
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
Date date = Sdf.parse ("2009-06-14 09:20:22");
Convert a date of type string to date of type date
Calendar
Calendar location in Java.util package
is an abstract class that cannot create objects,
Get the calendar:
Calendar RightNow = Calendar.getinstance ();
System.out.println (RightNow);
Use the method in the calendar to get the value:
int year = Rightnow.get (calendar.year);//Take calendar years
SYSTEM.OUT.PRINTLN (year);
int month = Rightnow.get (calendar.month);//The month in the calendar starts from 0
SYSTEM.OUT.PRINTLN (month);
int date = Rightnow.get (Calendar.day_of_month),//day of the calendar 1) Day_of_month 2)
DATE starts at 1, and two parameters are the same.
SYSTEM.OUT.PRINTLN (date);
int weekday = Rightnow.get (Calendar.day_of_week);//Take the day of the week from Sunday onwards
1 weeks, 4, 5.
SYSTEM.OUT.PRINTLN (weekday);
Java Learning Notes (iii)