Exploring Java Language_3rdUnit_Day3
Exploring Java Language_3rdUnit_Day3
The StringTokenizer class in Java is part of the java.util package and is used to break a
string into tokens. It's a legacy class, and while it's still available for use, newer classes like
String.split() or Scanner are generally preferred for more robust and flexible string parsing.
Legacy Class: Introduced in early versions of Java, before regular expressions were
added to the language.
Breaking Strings: Used to break a string into tokens based on specified delimiters.
Tokens: Each token is a substring separated by one or more delimiters.
Constructors:
Common Methods:
Example of StringTokenizer:
import java.util.StringTokenizer;
public class StringTokenizerExample {
public static void main(String[] args) {
String str = "Hello,World,Java,Programming";
StringTokenizer st = new StringTokenizer(str, ",");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
In this example, the string "Hello,World,Java,Programming" is broken into tokens using the
comma "," as the delimiter. The StringTokenizer iterates over each token and prints it.
Comparison with String.split():
While StringTokenizer is useful, the String.split() method introduced in Java 1.4 is generally
preferred because it offers more flexibility through regular expressions.
In this example, the String.split() method splits the string based on the comma delimiter, and
each token is printed using an enhanced for loop.
The StringTokenizer class is a legacy class used to break a string into tokens. Although still
available, newer methods like String.split() and Scanner are typically preferred for their
flexibility and ease of use. If you need to work with simple tokenization tasks,
StringTokenizer can still be handy.
BitSet
The BitSet class in Java is part of the java.util package and is used to create a dynamic array
of bits, which can grow as needed. It is an efficient way to handle sets of bits or booleans
where space efficiency is critical. Here’s an in-depth look at the BitSet class:
Common Constructors
Key Methods
import java.util.BitSet;
// Setting bits
bitSet1.set(0);
bitSet1.set(2);
bitSet1.set(4);
bitSet2.set(1);
bitSet2.set(2);
bitSet2.set(3);
// Logical AND
BitSet andSet = (BitSet) bitSet1.clone();
andSet.and(bitSet2);
System.out.println("AND: " + andSet);
// Logical OR
BitSet orSet = (BitSet) bitSet1.clone();
orSet.or(bitSet2);
System.out.println("OR: " + orSet);
// Logical XOR
BitSet xorSet = (BitSet) bitSet1.clone();
xorSet.xor(bitSet2);
System.out.println("XOR: " + xorSet);
The BitSet class in Java is a powerful and efficient way to manage sets of bits. It provides a
range of operations that allow for efficient manipulation of bits, making it suitable for
scenarios where space efficiency and fast operations are important.
Map & HashMap
import java.util.HashMap;
System.out.println(empIds);
System.out.println(empIds.get("Basha"));
System.out.println(empIds.containsKey("George"));
System.out.println(empIds.containsValue(86421901));
empIds.put("Raju", 99999);
System.out.println(empIds);
empIds.replace("Joker", 111);
System.out.println(empIds);
empIds.putIfAbsent("John", 222);
System.out.println(empIds);
empIds.remove("John");
System.out.println(empIds);
}
}
Output:
{Basha=54321, Raju=12345, Antony=86421901}
54321
false
true
{Basha=54321, Raju=99999, Antony=86421901}
{Basha=54321, Raju=99999, Antony=86421901}
{Basha=54321, John=222, Raju=99999, Antony=86421901}
{Basha=54321, Raju=99999, Antony=86421901}
Date & Time
Handling dates in Java can be accomplished in a few ways, depending on the version of Java you're
using. Here's an overview of two common approaches:
Using java.util.Date
java.util.Date is one of the earliest classes used to handle dates in Java, although it's largely
considered obsolete now. Still, it's good to know for historical context.
import java.util.Date;
Output:
Current Date: Mon Dec 30 20:28:14 UTC 2024
With Java 8, a new date and time API was introduced to address many of the shortcomings of the
older Date and Calendar classes. This API is more powerful and flexible.
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
Output
Current Date: 2024-12-30
Current Date and Time: 2024-12-30T20:28:50.634807233
Formatted Date and Time: 2024-12-30 20:28:50
The java.time package includes several classes like LocalDate, LocalTime, LocalDateTime,
and DateTimeFormatter that make working with dates and times much easier and more
intuitive.
Calendar
The java.util.Calendar class in Java is used for manipulating dates in a more flexible way
than the original java.util.Date class. Here’s how you can use it:
Basic Usage
import java.util.Calendar;
Output
Current Date and Time: 2024-12-30 20:31:9
import java.util.Calendar;
Output:
Current Date: Mon Dec 30 20:33:27 UTC 2024
Date after adding 5 days: Sat Jan 04 20:33:27 UTC 2025
Date after subtracting 3 months: Fri Oct 04 20:33:27 UTC 2024
import java.util.Calendar;
Output:
Specific Date and Time: Wed Dec 25 10:30:00 UTC 2024
Comparing Dates
You can also compare dates using the Calendar class:
import java.util.Calendar;
if (calendar1.before(calendar2)) {
System.out.println("calendar1 is before calendar2");
} else if (calendar1.after(calendar2)) {
System.out.println("calendar1 is after calendar2");
} else {
System.out.println("calendar1 is equal to calendar2");
}
}
Output:
calendar1 is after calendar2