Please enable JavaScript.
Coggle requires JavaScript to display documents.
Basic Data Structures and Input/Output - Coggle Diagram
Basic Data Structures and Input/Output
Collections in Java
a framework that provides an architecture to store and manipulate a group of objects
Operations
searching
sorting
insertion
manipulation
deletion
a single unit of objects
Interfaces
Set
represents the unordered set of elements which doesn't allow us to store the duplicate items
HashSet
Set<data-type> s1 = new HashSet<data-type>();
represents the collection that uses a hash table for storage
used to store the elements in the HashSet
contains unique items
LinkedHashSet
Set<data-type> s2 = new LinkedHashSet<data-type>();
represents the LinkedList implementation of Set Interface
contains unique elements
maintains the insertion order and permits null elements
TreeSet
uses a tree for storage
contains unique elements
access and retrieval time of TreeSet is quite fast
elements are stored in ascending order
SortedSet
provides a total ordering on its elements
elements are arranged in the increasing (ascending) order
provides additional methods that inhibit the natural ordering of the elements
SortedSet<data-type> set = new TreeSet();
List
child interface of collection interface
inhibits a list type data structure in which we can store ordered collection of objects
Classes
ArrayList
a dynamic array to store the duplicate element of different data types
maintains the insertion order and is non-synchronized
elements stored can be randomly accessed
a lot of shifting needs to occur if any element is removed from the array list
Constructors
ArrayList()
ArrayList(Collection<?> extends E> c)
ArrayList(int capacity)
old and non-generic:
ArrayList list=new ArrayList();
generic:
ArrayList<String> list=new ArrayList<String>();
Sorting
static method sort()
Collections.sort()
Iterating
iterator interface
for-each loop
ListIterator Interface
for loop
forEach() method
forEachRemaining() method
LinkedList
List <data-type> list2 = new LinkedLIst();
uses a doubly linked list internally to store the elements
can store the duplicate elements
Vector
List <data-type> list3 = new Vector();
uses a dynamic array to store the data elements.
Similar to ArrayList but is synchronized and contains many methods that are not the part of Collection framework
Stack
List <data-type> list4 = new Stack();
subclass of Vector
implements the last-in-first-out data structure
contains all of the methods of Vector class and also provides its
methods
boolean push(), boolean peek(), boolean push(object o), which defines its properties
Queue
maintains the first-in-first-out order
an ordered list that is used to hold the elements which are about to be processed
Classes
PriorityQueue
Queue<String> q1 = new PriorityQueue();
holds the elements or objects which are to be processed by their priorities
doesn't allow null values to be stored in the queue
Deque
Queue<String> q3 = new Deque();
we can remove and add the elements from both the side
stands for a double-ended queue which enables us to perform the operations at both the ends
Deque d = new ArrayDeque();
ArrayDeque
Queue<String> q2 = new ArrayDeque();
facilitates us to use the Deque
Unlike queue, we can add or delete the elements from both the ends
faster than ArrayList and Stack and has no capacity restriction
Methods of Collection Interface
public boolean add(E e)
inserts an element in the collection
public boolean addAll(Collection<? extends E> c)
inserts the specified collection elements in the invoking collection
public boolean remove(Object element)
deletes an element from the collection
public boolean removeAll(Collection<?> c)
deletes all elements of the specified collection from the invoking collection
default boolean removelf(Predicate<? super E> filter)
deletes all the elements of the collection that satisfy the predicate
public boolean retainAll(Collection<?> c)
deletes all the elements of invoking collection except the specified collection
public int size()
returns the total number of elements in the collection
public void clear()
removes the total number of elements from the collection
public boolean contains(Object element)
used to search an element
public boolean containsAll(Collection<?> c)
searches the specified collection in the collection
public Iterator iterator()
it returns an iterator
public <T> T[] toArray(T[] a)
converts collection into array
public boolean isEmpty()
checks if collection is empty
default Stream<E> parallelStream()
returns a possibly parallel Stream with the collection as its source
default Stream<E> stream()
returns a sequential Stream with the collection as its source
default Spliterator<E> spliterator()
generates a Spliterator over the specified elements in the collection
public boolean equals(Object element)
matches two collections
public int hashCode()
returns the hash code number of the collection
Iterator Interface
provides the facility of iterating the elements in a forward direction only
Methods
public boolean hasNext()
returns true if the iterator has more elements otherwise it returns false
public Object next()
returns the element and moves the cursor pointer to the next element
public void remove()
removes the last elements returned by the iterator
Iterable Interface
the root interface for all the collection classes
iterator<T> iterator()
abstract method
Iterable
<
extends
Collection
<
extends
List
< implements
AbstractList
< extends
ArrayList
Input/Output
Default streams
System.in
standard input stream that is used to read characters from the keyboard or any other standard input device
System.out
the standard output stream that is used to produce the result of a program on an output device like the computer screen
print()
println()
printf()
System.err
standard error stream that is used to output all the error data that a program might throw, on a computer screen or any standard output device.
Types of Streams
Input stream
reads data that must be taken as an input from a source array or file or any peripheral device
FIleInputStream, BufferedInputStream, ByteArrayInputStream
Output stream
writes data as outputs into an array or file or any output peripheral device
FileOutputStream, BufferedOutputStream, ByteArrayOutputStream
ByteStream
processes data byte by byte (8 bits)
BufferedInputStream
used for Buffered Input Stream
DataInputStream
contains method for reading java standard data types
FileInputStream
reads from a file
InputStream
abstract class that describes stream input
PrintStream
contains the most used print() and println() method
BufferedOutputStream
used for Buffered Output Stream
DataOutputStream
contains method for writing java standard data types
FileOutputStream
writes to a file
OutputStream
an abstract class that describe stream output
CharacterStream
BufferedReader
handles buffered input stream
FileReader
an input stream that reads from file
InputStreamReader
translates byte to character
OutputStreamReader
translates character to byte
Reader
an abstract class that define character stream input
PrintWriter
contains the most used print() and println() method
Writer
an abstract class that define character stream output
BufferedWriter
handles buffered output stream
FileWriter
output stream that writes to file
automatically allows for reading/writing data character by character