Basic Programming Session 4
Basic Programming Session 4
Basic Programming
Session 4
endava.com
Basic Programming
Garbage collection
String & StringBuilder
toString
Collections
ArrayList
HashSet
HashMap
Q&A
String
The String Class
Step 1
String myString = Hello;
The heap
Hello
myString
(reference
variable)
Step 2
String secondString =
myString;
myString
(reference variable)
The heap
Hello
secondString
(reference variable)
Step 3
myString =
myString.concat( World)
myString
The heap
(reference variable)
secondString
Hello World
Hello
(reference variable)
toString()
Implementing toString method in java is done by overriding the Objects toString method.
The java toString() method is used when we need a string representation of an object. It is defined in
Object class. This method can be overridden to customize the String representation of the Object.
Demo
7
Conclusion
Since our example has no toString method, the default one in Object is used. The format of the default
toString method of the Object is as shown below.
Class Name, @, and the hex version of the objects hashcode concatenated into a string.
The default hashCode method in Object is typically implemented by converting the memory address of the
object into an integer.
The StringBuilder class was added in Java 5. It has exactly the same API as the
StringBuffer class, except StringBuilder is not thread safe.
8
This method returns a StringBuffer object and updates the value of the StringBuffer
object that invoked the method call. In both cases, the characters in the StringBuffer
are reversed, the first character becoming the last, the second becoming the second
to the last, and so on:
This method returns the value of the StringBuffer object that invoked the method call
as a String
9
public synchronized
StringBuffer
append(String s)
update the value of the object that invoked the method, whether or not the return is
assigned to a variable. This method will take many different arguments, including
boolean, char, double, float, int, long, and others
public StringBuilder
delete(int start, int
end)
This method returns a StringBuilder object and updates the value of the StringBuilder
object that invoke the method call. In both cases, a substring is removed from the
original object.
public StringBuilder
insert(int offset,
String s)
This method returns a StringBuilder object and updates the value of the StringBuilder
object that invoked the method call. In both cases, the String passed in to the second
argument is inserted into the original StringBuilder starting at the offset location
represented by the first argument (the offset is zero-based).
Java Collections
1.
2.
3.
4.
5.
10
11
11
Classification
12
QUALITY. PRODUCTIVITY.
INNOVATION.
Map
HashMap
Set
List
Ordered
Sorted
No
No
Hashtable
No
No
TreeMap
Sorted
By natural order or
custom comparison rules
LinkedHashMap
By insertion order
or last
13 access order
No
HashSet
No
No
TreeSet
Sorted
By natural order or
custom comparison rules
LinkedHashSet
By insertion order
No
ArrayList
By index
No
Vector
By index
No
LinkedList
By index
No
13
ArrayList
add (Object o) - adds an object to the ArrayList
add(int index, Object o) adds the object o the the array list at
the given index
get(int index) - retrieves object reference from ArrayList index
position
size() - returns ArrayList size
remove(int index) - removes element at specified position in the
list. Shifts any subsequent elements to the left
14
remove (Object o) - removes the object o from the list
set(int index, Object o)- used for updating an element; it
replaces the element present at the specified index with the
object o
int indexOf(Object o)- finds the index in the list of the first
occurrence of the specified element. If the element is not
present in the list returns -1
Object get(int index) returns the object of list which is present
at the specified index
clear() - removes all the elements
14
HashSet
15
HashMap
void clear()- removes all the keys and values form the specified
map
16
Set keySet()- returns the Set of the keys fetched from the map
value put(Key k, Value v)-inserts key value mapping into the
map.
16
Q&A
17