Bài giảng Phát triển phần mềm phân tán - Bài 9: Utility classes and collections - Vũ Thị Hương Giang

LESSON IX.  
Utility classes and collections  
Vu Thi Huong Giang  
SoICT (School of Information and  
Communication Technology)  
HUST (Hanoi University of Science  
and Technology)  
Objectives  
• After this lesson, students can:  
– Understand the meanings of Wrapper class as well as its  
main purpose of conversion  
– Learn how to use mathematic constants and functions  
– Differentiate between String and StringBuffer and use  
appropriately  
– Understand Java Collection Framework and work with  
Set, List, Map data structure.  
– Understand the relations between Java Colllection and  
Iterator and Comparator  
Content  
• I. Utility classes  
– Wrapper class  
– Math class  
– String and StringBuffer  
• II. Collections Framework  
– Collections Interface  
– ArrayList Class  
– HashSet Class  
– HashMap Class  
• III. Iterator and Comparator Interface  
I. Utility class  
1. Wrapper Class  
2. Math Class  
3. String and StringBuffer  
1. Wrapper Class-Introduction (1/2)  
• Wrapper class:  
– is a wrapper around a primitive data type  
– represents primitive data types in their corresponding  
class instances  
• Two primary purposes of wrapper classes:  
– provide a mechanism to “wrap” primitive values in an  
object so that the primitives can be included in activities  
reserved for objects  
– provide an assortment of utility functions for primitives.  
Most of these functions are related to various  
conversions: converting primitives to and from String  
objects, etc.  
1. Wrapper-Introduction (2/2)  
• Primitive types and corresponding wrapper  
classes:  
Primitive  
boolean  
byte  
Wrapper  
java.lang.Boolean  
java.lang.Byte  
char  
java.lang.Character  
java.lang.Double  
java.lang.Float  
java.lang.Integer  
java.lang.Long  
java.lang.Short  
java.lang.Void  
double  
float  
int  
long  
short  
void  
1. Wrapper class - features  
• The wrapper classes are immutable  
• The wrapper classes are final  
• All the methods of the wrapper classes are static.  
• All the wrapper classes except Boolean and  
Character are subclasses of an abstract class  
called Number  
– Boolean and Character are derived directly from the  
Object class  
Creating Wrapper Objects with the  
new Operator  
• All of the wrapper classes provide two  
constructors (except Character):  
– One takes a primitive of the type being constructed,  
– One takes a String representation of the type being  
constructed  
Boolean wbool = new Boolean("false");  
Boolean ybool =new Boolean(false);  
Byte wbyte = new Byte("2");  
Byte ybyte =new Byte(2);  
Float wfloat = new Float("12.34f");  
Float yfloat = new Float(12.34f);  
Double wdouble = new Double("12.56d");  
Double vdouble = new Double(12.56d);  
Character c1 = new Character('c');  
(Similar to Short, Integer, Long)  
Creating wrapper objects by wrapping  
primitives using a static method  
• valueOf(String s)  
• valueOf(String s, int radix)  
• Return the object that is wrapping what we  
passed in as an argument.  
Integer i2 = Integer.valueOf("101011", 2);  
// converts 101011 to 43 and assigns the value 43 to the  
//Integer object i2  
Float f2 = Float.valueOf("3.14f");  
// assigns 3.14 to the Float object f2  
Using Wrapper Conversion Utilities  
• primitive-typeValue(): convert the value of a  
wrapped numeric to a primitive  
– No-argument methods  
– 6 conversions for each six numeric wrapper class  
Integer i2 = new Integer(42); // make a new wrapper object  
byte b = i2.byteValue(); // convert i2's value to a byte primitive  
short s = i2.shortValue(); // another of Integer's xxxValue methods  
double d = i2.doubleValue(); // yet another of Integer's xxxValue  
//methods  
Converting Strings to Primitive Types  
• Convert a string value to a primitive value  
(except Character wrapper class):  
static <type> parse<Type>(String s)  
– s: String representation of the value to be converted  
– <type>: primitive type to convert to (byte, short, int,  
long, float, double, boolean, except char)  
– <Type> : the same as <type> with the first letter  
uppercased  
String s = "123";  
int i = Integer.parseInt(s); //assign an int value of 123 to the int variable i  
short j= Short.parShort(s) //assign an short value of 123 to the short variable j  
I. Utility class  
1. Wrapper Class  
2. Math Class  
3. String and StringBuffer  
2. Math class-introduction  
• The java.lang.Math class provides  
useful mathematical functions and  
constants.  
• All of the methods and fields are  
static, and there is no public  
constructor.  
• Most of methods get double  
parameters and return double value.  
• It’s unnecessary to import any  
package for the Math class.  
2. Math Class-Constants  
• double E: the base of natural logarithm, 2.718...  
• double PI: The ratio of the circumference of a  
circle to its diameter, 3.14159...  
2. Math Class-Methods (1/3)  
• static double abs (double x):Returns the absolute  
value of the argument  
• static float abs (float x): Returns the absolute  
value of the argument  
• static int abs (int x): Returns the absolute value  
of the argumentstatic  
• static long abs (long x): Returns the absolute  
value of the argument  
2. Math Class-Methods (2/3)  
• static double acos (double): Returns the arccos of the  
argument (in radians), in the range (0, pi)  
• static double asin (double): Returns the arcsin of the  
argument (in radians), in the range (-pi/2, pi/2)  
• static double atan (double): Returns the arctan of the  
argument (in radians), in the range (-pi/2, pi/2)  
• static double atan2 (double y, double x): Returns the  
arctan of y/x (in radians), in the range [0, 2*pi)  
• static double cos (double): Returns the cosine of the  
argument (in radians)  
• static double sin (double): Returns the sine of the  
argument (in radians)  
2. Math Class-Methods (3/3)  
• static double exp (double): Returns e raised to the  
power of the argument  
• static double log (double): Returns the natural  
logarithm of the argumentstatic  
• double pow (double base, double exp): Returns  
the base raised to the exp power  
• static double sqrt (double): Returns the square root of  
the argument  
• static long round (double): Returns the nearest  
integer to the argument  
• static int round (float): Returns the nearest integer to  
the argument  
• static double random (): Returns a pseudorandom  
number in the range [0, 1)  
I. Utility class  
1. Wrapper Class  
2. Math Class  
3. String and StringBuffer  
3. String, StringBuffer & StringBuilder  
• String is immutable whereas StringBuffer and StringBuilder  
can change their values.  
• The only difference between StringBuffer and StringBuilder  
is that StringBuilder is unsynchronized whereas  
StringBuffer is synchronized. So when the application needs  
to be run only in a single thread then it is better to use  
StringBuilder. StringBuilder is more efficient than  
StringBuffer.  
• Criteria to choose among String, StringBuffer and  
StringBuilder  
– If your text is not going to change use a string Class because a  
String object is immutable.  
– If your text can change and will only be accessed from a single  
thread, use a StringBuilder because StringBuilder is  
unsynchronized.  
– If your text can changes, and will be accessed from multiple  
threads, use a StringBuffer because StringBuffer is synchronous.  
StringBuffer is faster than String in  
concatenation (1/2)  
• StringBuffer is faster than String when  
performing simple concatenations  
– Ex1:  
String str = new String ("First ");  
str += "Second";  
– Ex2:  
StringBuffer str = new StringBuffer ("First ");  
str.append("Second");  
Tải về để xem bản đầy đủ
pdf 64 trang Thùy Anh 11960 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Phát triển phần mềm phân tán - Bài 9: Utility classes and collections - Vũ Thị Hương Giang", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_phat_trien_phan_mem_phan_tan_bai_9_utility_classes.pdf