KEMBAR78
Java STL I | PDF | Computer Data | Object Oriented Programming
0% found this document useful (0 votes)
12 views8 pages

Java STL I

The document provides an overview of Java's Collections Framework, detailing its components such as containers, algorithms, iterators, and functional objects. It compares arrays and ArrayLists, highlighting the flexibility and functionality of ArrayLists, and introduces the AbstractMap.SimpleEntry class as a way to create pairs of values. Key features and methods for both ArrayLists and SimpleEntry are also discussed, emphasizing their utility in Java programming.

Uploaded by

partesujit007
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)
12 views8 pages

Java STL I

The document provides an overview of Java's Collections Framework, detailing its components such as containers, algorithms, iterators, and functional objects. It compares arrays and ArrayLists, highlighting the flexibility and functionality of ArrayLists, and introduces the AbstractMap.SimpleEntry class as a way to create pairs of values. Key features and methods for both ArrayLists and SimpleEntry are also discussed, emphasizing their utility in Java programming.

Uploaded by

partesujit007
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
You are on page 1/ 8

ava Collections Part 1

●​ Introduction
●​ Pair
●​ ArrayList

Introduction

●​ Java's Collections Framework provides a comprehensive toolkit for data


storage, manipulation, and processing.
●​ It includes a variety of data structures, algorithms, and iterators, much like the
C++ STL, but with Java's object-oriented design.

Fundamental Components of the Java Collections


Framework

1.​ Containers:

●​ Java offers containers like Lists (ArrayList, LinkedList), Sets (HashSet,


TreeSet), Maps (HashMap, TreeMap), and others.
●​ These are akin to STL's vectors, lists, maps, and sets. They are used for
efficient data storage and manipulation.
2.​ Algorithms:

●​ Java provides a range of algorithms, particularly through the


Collections and Arrays classes.
●​ These include sorting (​
Collections.sort()), searching (Collections.binarySearch()), and
shuffling (Collections.shuffle()), similar to STL's algorithms.
3.​ Iterators:
●​ Java's iterators, like the Iterator and ListIterator, are used to traverse
through collections.
●​ They are specifically designed to work with Java's collection types.
4.​ Function Objects:

●​ Java utilizes functional interfaces, especially with the advent of


lambdas in Java 8, to pass behaviour to algorithms.
●​ These are similar to C++'s functions but leverage Java's lambda
expressions and method references.

Arrays vs. ArrayLists in Java

Aspect Arrays ArrayLists

Fixed size; the size is determined at


Size Dynamic size; ArrayLists can grow
the time of creation and cannot
Flexibility and shrink at runtime as needed.
change.

Supports dynamic initialization;


Can be initialized with a fixed set of
Initialization elements can be added or removed
elements or predefined size.
dynamically.

Limited built-in functionality; Extensive built-in functions for


Functions requires manual effort for resizing adding, removing, searching, and
and advanced operations. resizing, among others.

Pair in Java
●​ In Java, while there isn't a direct built-in equivalent to C++'s​
std::pair in the standard Java libraries up until Java 7, the concept of pairing
two values together is often implemented using classes from the standard
library itself.
●​ An alternative to using the​
Pair is the SimpleEntry class from the java.util.AbstractMap package,
available as part of Java's standard library.

AbstractMap.SimpleEntry Class - Introduction

●​ The​
SimpleEntry class functions similarly to the std::pair in C++.
●​ It typically stores two objects or values together, such as a key and a value in
a map.
●​ This class is handy in scenarios where you need to return two values from a
method or when you want to maintain a relationship between two related
objects without creating a separate class.

Key Features and Methods of AbstractMap.SimpleEntry

●​ Creation:
○​ To create an instance of​
SimpleEntry, you use its constructor, passing the two values you want
to pair.
○​ AbstractMap.SimpleEntry<KeyType, ValueType> variableName = new
AbstractMap.SimpleEntry<>(key, value);
●​ Accessing Values:
○​ getKey(): Returns the first element of the entry, typically used as the
key.
○​ getValue(): Returns the second element of the entry, typically used as
the value.
●​ Equality Check:
○​ equals(): Compares two SimpleEntry objects based on the values
they store.
●​ String Representation:
○​ toString(): Returns a string representation of the entry.
●​ Hash Code:
○​ hashCode(): Generates a hash code for the entry, useful in collections
that use hashing (like HashMap, HashSet).

●​

Explanation:

●​ Creating a SimpleEntry:
○​ A SimpleEntry is instantiated with two values: an integer 1 and a string
"One". The integer acts as a key, and the string is the value.
●​ Accessing the Values:
○​ The getKey() method retrieves the key (1 in this case), and getValue()
retrieves the associated value ("One").
●​ Checking Equality:
○​ Another SimpleEntry object is created and compared to the first. The
equals() method checks if both entries have the same key and value.
●​ String Representation:
○​ The toString() method provides a string representation of the entry,
typically displaying its contents.
●​ Hash Code:
○​ The hashCode() method generates a hash code for the entry, which is
useful for hashing algorithms in data structures like HashMap.

ArrayList in Java

●​ ArrayLists are part of the Java Collections Framework and provide a way to
use dynamically sized arrays.
●​ Unlike regular arrays, they offer more flexibility and a variety of useful built-in
methods.

Key Features and Methods of ArrayList

1.​ Creation:

●​ ArrayLists can be created and initialized in various ways.

ArrayList<Type> arrayListName = new ArrayList<>();

Adding Elements:

●​ add(element): Adds an element to the ArrayList.


●​ add(index, element): Inserts an element at the specified index.
2.​ Accessing Elements:

●​ get(index): Returns the element at the specified index.


3.​ Modifying Elements:

●​ set(index, element): Updates the element at the specified index.


4.​ Size and Capacity:

●​ size(): Returns the number of elements in the ArrayList.


5.​ Removing Elements:

●​ remove(index): Removes the element at the specified index.


●​ remove(Object): Removes the first occurrence of the specified
element.
6.​ Other Useful Methods:

●​ clear(): Removes all elements from the ArrayList.


●​ isEmpty(): Returns true if the ArrayList is empty.
●​ In this example, an​
ArrayList of integers is created, and elements are added, accessed, modified,
and removed.
●​ The​
ArrayList class offers a variety of methods for managing a dynamic array,
making it a versatile data structure in Java.

ArrayList of SimpleEntry(Pair) in Java

●​ It's important to note that​


AbstractMap.SimpleEntry can be used in Java to create a pair of two values.
●​ You can use Java's​
ArrayList to create a dynamic list of these entries.
Using AbstractMap.SimpleEntry requires no additional dependencies since it's part
of the standard Java library, making it a convenient choice for projects where
minimal external dependencies are desired.

You might also like