0% found this document useful (0 votes)
1 views6 pages

Java Fundamentals Part 3

This document provides an overview of Java fundamentals, focusing on key concepts such as Strings, Lists (ArrayList), Tuples (Arrays/Immutable Lists), Dictionaries (HashMap), and Sets (HashSet). It covers string manipulation, including methods for concatenation, indexing, slicing, and formatting, as well as ArrayList characteristics and common methods. Additionally, it explains the usage of HashMaps for key-value pairs and HashSets for unique collections, along with basic operations like union and intersection.

Uploaded by

manthan.gajra251
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Java Fundamentals Part 3

This document provides an overview of Java fundamentals, focusing on key concepts such as Strings, Lists (ArrayList), Tuples (Arrays/Immutable Lists), Dictionaries (HashMap), and Sets (HashSet). It covers string manipulation, including methods for concatenation, indexing, slicing, and formatting, as well as ArrayList characteristics and common methods. Additionally, it explains the usage of HashMaps for key-value pairs and HashSets for unique collections, along with basic operations like union and intersection.

Uploaded by

manthan.gajra251
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA HAND WRITTEN NOTES :

Java Fundamentals (Part 3)


Concepts: String, List (ArrayList), Tuple (Arrays/Custom Objects), Dictionary (HashMap) &
Set (HashSet)

Strings

What is a String?

A string in Java is a sequence of characters enclosed in double quotes ( ""):

String str1 = "hello world";


String str2 = "Prime";

Strings are immutable in Java just like in Python, meaning once created, their contents
cannot be changed directly.

.length() Method

We use the .length() method to find the total number of characters in a string:

String word = "Prime";


[Link]([Link]()); // 5

Concatenation

We can concatenate (join) strings using the + operator:

String str1 = "Apna";


String str2 = "College";
String word = str1 + str2; // "ApnaCollege"
[Link](word);

Looping over Strings

To loop through a string character by character, we can use a for loop with .charAt(index)
or turn it into a character array using .toCharArray():

String s = "Python";
for (char ch : [Link]()) {
[Link](ch);
}
Indexing & Slicing in Strings

Indexing

Java follows zero-based indexing (the first character is at index 0). We access characters
using the .charAt(index) method rather than brackets [].

String s = "Python";
[Link]([Link](0)); // 'P'
[Link]([Link](3)); // 'h'
// Note: Java does not support negative indexing like s[-1].
// To get the last character:
[Link]([Link]([Link]() - 1)); // 'n'

Slicing (Substring)

In Java, slicing is done using the .substring(start, end) method.

• start – index where the slice begins (inclusive).


• end – index where the slice ends (exclusive).

String s = "Python";
[Link]([Link](0, 2)); // "Py"
[Link]([Link](2)); // "thon" (from index 2 to end)
[Link]([Link](0, 3)); // "Pyt"

(Note: Java does not have a built-in step argument or a step inversion like [::-1] for native
strings. To reverse a string, we typically use new
StringBuilder(s).reverse().toString()).

String Formatting

Java provides structural string formatting via [Link]() or directly in the console
output via [Link]().

Using Placeholders

Instead of Python’s {} or f-strings, Java uses specific format specifiers like %s (for Strings)
and %d (for integers).

String name = "Rahul";


int age = 25;

String text = [Link]("My name is %s and I am %d years old", name,


age);
[Link](text);

To handle complex operations or expressions, compute them directly inside the argument
array:

int a = 5;
int b = 10;
[Link]("sum of %d + %d = %d\n", a, b, (a + b));
[Link]("avg of %d + %d = %.1f\n", a, b, ((a + b) / 2.0));

Lists (ArrayList)

What is a List?

In Java, the structural equivalent to a Python mutable list is an ArrayList. It is an ordered,


resizable array collection that allows duplicate values.

import [Link];

ArrayList<Integer> myList = new ArrayList<>();


[Link](1);
[Link](2);
[Link](3);
[Link](myList); // [1, 2, 3]

Important Note: Java arrays and ArrayLists are typically homogeneous (they hold elements
of a single specified data type). If you absolutely need a heterogeneous list like Python's, you
declare an array list of type Object:

ArrayList<Object> myList2 = new ArrayList<>();


[Link](10);
[Link]("Hello");
[Link](3.14);

ArrayList Characteristics

1. Ordered – Items have a defined order and can be accessed by index.


2. Mutable – Items can be added, changed, or removed.
3. Allows duplicates – Same value can appear more than once.

Accessing & Modifying Elements

ArrayList<String> fruits = new ArrayList<>();


[Link]("apple");
[Link]("banana");
[Link]("cherry");

// Access
[Link]([Link](0)); // "apple" (uses .get() instead of [0])

// Modify
[Link](0, "mango");
[Link](fruits); // [mango, banana, cherry]

Common ArrayList Methods

Method Description Example


.size()
Returns total element [Link]();
count (like len())
Appends element to
.add(element) the end (like [Link](7);
append())

.add(idx, element)
Inserts element at a [Link](1, 4);
specific index
[Link](list) Sorts list in ascending [Link](nums);
order

Looping & Linear Search on Lists

ArrayList<Integer> numbers = new ArrayList<>();


// imagine values [5, 12, 7, 3, 18, 9] are added...

// Standard iteration
for (int num : numbers) {
[Link](num);
}

// Linear Search
int target = 18;
for (int i = 0; i < [Link](); i++) {
if ([Link](i) == target) {
[Link](target + " found at index = " + i);
break;
}
}

Tuples \rightarrow Java Fixed Arrays / Immutable Lists

Java does not have a built-in Tuple keyword. To replicate an ordered collection that shouldn't
change size, we use standard native Arrays or an Immutable List.

// Method 1: Fixed-size standard Java Array


int[] tup = {10, 20, 30};
[Link](tup[0]); // 10

// Method 2: Java modern Immutable List (Acts exactly like a Python Tuple)
import [Link];
List<Integer> immutableTup = [Link](10, 20, 30);
// [Link](40); // Throws an error! (Immutable)

Dictionaries \rightarrow HashMaps

What is a Dictionary?

In Java, dictionaries are built using the HashMap class. It stores elements as key-value pairs.

import [Link];

HashMap<String, Object> student = new HashMap<>();


[Link]("name", "Bob");
[Link]("age", 20);

// Accessing values using a key


[Link]([Link]("name")); // Bob
Key HashMap Methods

• .put(key, value) – Adds or updates a key-value pair.


• .get(key) – Accesses a value safely. Returns null if the key does not exist.
• .keySet() – Returns a set collection of all keys.
• .values() – Returns a collection of all values.
• .entrySet() – Returns a set of all key-value pairs.

Looping through a HashMap

for ([Link]<String, Object> entry : [Link]()) {


[Link]([Link]() + " " + [Link]());
}

Sets (HashSet)

What is a Set?

A set is an unordered collection of unique elements. In Java, this is implemented using the
HashSet class.

import [Link];

HashSet<Integer> mySet = new HashSet<>();


[Link](1);
[Link](2);
[Link](2); // Duplicate entry, will be ignored safely

[Link](mySet); // [1, 2]
[Link]([Link]()); // 2

Set Operations and Methods

HashSet<Integer> A = new HashSet<>([Link](1, 2, 3));


HashSet<Integer> B = new HashSet<>([Link](3, 4, 5));

// 1. Union
HashSet<Integer> union = new HashSet<>(A);
[Link](B);
[Link](union); // [1, 2, 3, 4, 5]

// 2. Intersection
HashSet<Integer> intersection = new HashSet<>(A);
[Link](B);
[Link](intersection); // [3]

// 3. Remove
[Link](1); // removes element 1

Keep learning & Keep exploring!

You might also like