Java Unit 10

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Holding Collection of Data

1.Arrays And Collection Classes/Interfaces


Arrays:

An array is a data structure that stores a fixed-size sequential collection of elements


of the same type.
In Java, you declare an array using square brackets and the type of the elements that
the array will hold.
Collection Classes/Interfaces:

Collection classes/interfaces are data structures that can hold a variable number of
objects of different types. They are part of the Java Collections Framework.
Some common collection classes/interfaces in Java include:
ArrayList: an ordered list that allows duplicates
LinkedList: a linked list that allows duplicates
HashSet: a set that does not allow duplicates
TreeSet: a set that stores elements in sorted order
HashMap: a map that stores key-value pairs
To use a collection class/interface, you first need to create an instance of it using the
"new" keyword and its constructor.

Map/List/Set Implementations:
Map Implementations:

A Map is a collection that stores key-value pairs.


In Java, the most commonly used Map implementations are HashMap and TreeMap.
HashMap is an unordered collection, meaning that the elements are not stored in
any particular order. It provides constant-time performance for the basic
operations (get and put), assuming that the hash function distributes the
elements properly among the buckets.
TreeMap is an ordered collection, meaning that the elements are sorted according to
their natural order (or according to a specified comparator). It provides log(n)
time complexity for the basic operations.
List Implementations:

A List is an ordered collection of elements.


In Java, the most commonly used List implementations are ArrayList and LinkedList.
ArrayList is an array-backed list that provides constant-time performance for most
operations (get and set), assuming that the underlying array has sufficient
capacity. It provides linear-time performance for adding or removing elements at
arbitrary positions.
LinkedList is a linked list that provides constant-time performance for adding or
removing elements at arbitrary positions. It provides linear-time performance for
most other operations (get, set, remove).
Set Implementations:

A Set is a collection that contains no duplicate elements.


In Java, the most commonly used Set implementations are HashSet and TreeSet.
HashSet is an unordered collection that provides constant-time performance for the
basic operations (add, remove, contains), assuming that the hash function
distributes the elements properly among the buckets.
TreeSet is an ordered collection that stores the elements in sorted order. It provides
log(n) time complexity for the basic operations.

Collection Classes:
1.Array List
ArrayList is an implementation of the List interface in Java, which allows for the
storage of elements in an ordered sequence and provides indexed access to
those elements.
ArrayList is implemented as a resizable array, which means that elements can be
added or removed dynamically as needed.
To use ArrayList in Java, you must first import the java.util.ArrayList package.
Here are some common methods used when working with ArrayList:
add(element): Adds an element to the end of the ArrayList.
add(index, element): Adds an element to the ArrayList at the specified index, shifting
all subsequent elements to the right.
get(index): Returns the element at the specified index.
set(index, element): Replaces the element at the specified index with the specified
element.
remove(index): Removes the element at the specified index, shifting all subsequent
elements to the left.
size(): Returns the number of elements in the ArrayList.
clear(): Removes all elements from the ArrayList.
2.Linked List
LinkedList is an implementation of the List interface in Java, which allows for the
storage of elements in an ordered sequence and provides indexed access to
those elements.
LinkedList is implemented as a doubly-linked list, which means that each element in
the list has a reference to both the previous and next elements in the list.
To use LinkedList in Java, you must first import the java.util.LinkedList package.
Here are some common methods used when working with LinkedList:
add(element): Adds an element to the end of the LinkedList.
addFirst(element): Adds an element to the beginning of the LinkedList.
addLast(element): Adds an element to the end of the LinkedList (same as
add(element)).
get(index): Returns the element at the specified index.
set(index, element): Replaces the element at the specified index with the specified
element.
remove(index): Removes the element at the specified index.
removeFirst(): Removes the first element of the LinkedList.
removeLast(): Removes the last element of the LinkedList.
size(): Returns the number of elements in the LinkedList.
clear(): Removes all elements from the LinkedList.
3.Hash Set
HashSet is an implementation of the Set interface in Java, which allows for the
storage of elements in an unordered collection and does not allow duplicates.
HashSet uses a hash table for storage and retrieval of elements.
To use HashSet in Java, you must first import the java.util.HashSet package.
Here are some common methods used when working with HashSet:
add(element): Adds an element to the HashSet.
remove(element): Removes the specified element from the HashSet.
contains(element): Returns true if the HashSet contains the specified element, false
otherwise.
size(): Returns the number of elements in the HashSet.
clear(): Removes all elements from the HashSet.
4.Tree Set
TreeSet is an implementation of the SortedSet interface in Java, which allows for the
storage of elements in a sorted collection and does not allow duplicates.
TreeSet uses a binary search tree for storage and retrieval of elements, which
ensures that the elements are always in sorted order.
To use TreeSet in Java, you must first import the java.util.TreeSet package.
Here are some common methods used when working with TreeSet:
add(element): Adds an element to the TreeSet.
remove(element): Removes the specified element from the TreeSet.
contains(element): Returns true if the TreeSet contains the specified element, false
otherwise.
size(): Returns the number of elements in the TreeSet.
clear(): Removes all elements from the TreeSet.
first(): Returns the first (lowest) element in the TreeSet.
last(): Returns the last (highest) element in the TreeSet.

Accessing Collections/Use of An Iterator:


In Java, you can access elements in a collection using an iterator.
An iterator is an interface that provides methods for iterating over the elements in a
collection.
To use an iterator in Java, you must first obtain an iterator object for the collection
you want to iterate over.
Here are the steps to iterate over a collection using an iterator in Java:
Obtain an iterator object for the collection using the iterator() method.
Use the hasNext() method to check if there are more elements in the collection.
Use the next() method to retrieve the next element in the collection.
Repeat steps 2-3 until all elements in the collection have been processed.

Comparator:
In Java, the Comparator interface is used to define a custom ordering of objects.
Comparator provides a way to sort objects based on criteria other than their natural
ordering, which is defined by the Comparable interface.
Comparator has two methods: `compare()` and `equals()`.
The `compare()` method compares two objects and returns an integer that indicates
their relative order. If the first object is "less than" the second object, a negative
number is returned. If the first object is "greater than" the second object, a
positive number is returned. If the two objects are "equal", zero is returned.
The `equals()` method is used to check if two comparators are equal.

You might also like