Programmer Coding

Set Structure in Java

Sets in java is a structure that gives an architecture to store and change the group of objects.

 

All the missions that you apply on a data such as searching, sorting, inserting, manipulation, deletion etc. can be pull off by Java Sets.

 

Java Set simply means a single unit of objects. Java Set structure gives a lot of borderlines (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PrecedenceQueue, HashSet, LinkedHashSet, TreeSet etc).

 

What is structure in java

  1. gives readymade
  2. represents set of classes and
  3. is

 

What is Set structure

 

Set structure represents a unified architecture for storing and manipulating group of objects. It has:

 

  1. Borderlines and its ful filments e. classes
  2. Algorithm

Java ArrayList class

Java ArrayList class uses a dynamic array for storing the elements. It inherits ConceptualList class and applies List borderline.

 

The important points about Java ArrayList class are:

 

  1. Java ArrayList class can store(contain) duplicate
  2. Java ArrayList class maintains inserting
  3. Java ArrayList class is non
  4. Java ArrayList allows random entrance because array works at the index
  5. In Java ArrayList class, manipulation is slow because a lot of shifting needs to be happenred if any element is removed from the array

 

ArrayList class announcement

 

Let’s see the announcement for java.util.ArrayList class.

 

Creaters of Java ArrayList

 

 

 

Creater                                 Description

ArrayList() It is used to design an empty array list.
ArrayList(Set c) It is used to design an array list that is established with the elements of the set c.
ArrayList(int capacity) It is used to design an array list that has the specified  initial capacity.

 

Java ArrayList Example import java.util.*;

class TestSet1{

public static void main(String args[]){

ArrayList<String> list=new ArrayList<String>();//Creating arraylist list.add(“Ravi”);//Adding object in arraylist

list.add(“ram”);

list.add(“syam”);

list.add(“ayushi”);

//Traversing list through Iterator Iterator itr=list.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } }}

ram syam ayushi

syam

 

vector

 

ArrayList and Vector both applies List borderline and maintains inserting order.

 

But there are a lot of differences between ArrayList and Vector classes that are given below.

 

ArrayList Vector
1) ArrayList is not defended. Vector is defended.
2)ArrayList additions 50% of current array size if number of element exceeds from its capacity. Vector additions 100% means doubles the array size if total number of element exceeds than its capacity.
3)ArrayList is not a legacy class, it is introduced in JDK 1.2. Vector is a legacy class.
 

4) ArrayList is fast because it is non-defended.

 

Vector is slow because it is defended i.e. in alternating environment, it will hold the other alternates in runnable or non-runnable state until current alternate releases the lock of object.

5) ArrayLis tuses Iterator borderline to traverse the elements. Vector uses Iteration borderline to traverse the                                      elements. But it can use Iterator also.

 

 

Example of Java Vector

 

Let’s see a simple example of java Vector class that uses Iteration borderline.

 

  1. import util.*;
  2. class TestVector1{
  3. public static void main(String args[]){
  4. Vector<String> v=new Vector<String>();//creating vector
  5. add(“ram”);//way(method) of Set
  6. addElement(“vishnu”);//way(method) of Vector
  7. addElement(“pratap”);
  8. //traversing elements using Iteration

 

  1. Iteration e=v.elements();
  2. while(e.hasMoreElements()){
  3. out.println(e.nextElement()); 12. } } }

 

Output:

ram

vishnu pratap

 

 

Java Disputable class

 

Java Disputable class applies a disputable, which maps keys to values. It inherits Dictionary class and applies the Map borderline.

 

The important points about Java Disputable class are:

 

  • A Disputable is an array of list. Each list is known as a bucket. The position of bucket is identified by calling the hashcode() way(method) . A Disputable insure values based on the
  • It insure only unique
  • It may have not have any null key or
  • It is

 

Disputable class announcement

 

Let’s see the announcement for java.util.Disputable class.

 

  1. public class Disputable<K,V> expand Dictionary<K,V> applies Map<K,V>, Cloneable, Ser ializable

Disputable class Parameters

 

Let’s see the Parameters for java.util.Disputable class.

 

  • K: It is the type of keys reconstructed by this
  • V: It is the type of mapped

 

Creaters of Java Disputable class

 

Creater

     

Description

Disputable() It is the default creater of hash table it instantiates the Disputable class.
Disputable(int size) It is used to accept an integer parameter and produces a hash table that has an initial size specified by integer value size.
 

Disputable(int fillRatio)

size, float  

It is used to produce a hash table that has an initial size specified by size and a fill ratio specified by fillRatio.

Java Disputable Example

import java.util.*;

class TestSet16{

public static void main(String args[]){ Disputable<Integer,String> hm=new Disputable<Integer,String>(); hm.put(100,”vishnu”);

hm.put(102,”ram”);

hm.put(101,”ayushi”);

hm.put(103,”shyam”);

for(Map.Entry m:hm.entrySet()){ System.out.println(m.getKey()+” “+m.getValue());

} } }

 

Output:

103 vishnu

102 ram

101 ayushi

100 shyam

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top