Collections in Java Practical Programs

Collections in Java


  • What is List in Java ?
Represents an ordered collection. Lists can contain duplicate elements.
  • What is SortedSet in Java ?
Represents a set whose elements are maintained in a sorted order. 
  • What is Set in Java ?
Represents an unordered collection that does not permit duplicate elements.
  • What is Queue in Java ?
A collection used to hold multiple elements prior to processing. A Queue provides additional insertion, extraction and inspection operations.
  • What is Map in Java ?
Represents key-value pairs. A Map cannot contain duplicate keys; each key can map to at most one value. Does not extend collection.
  • What is Sorted Map in Java ?
Represents a Map that maintains its mappings in ascending key order

  • What is Collection in Java ?

The most general collection interface type. A collection represents a group of objects known as its elements. The Java platform doesn't provide any direct implementations of this interface. 


    Accept ‘n’ integers from the user and store them in a collection. Display them in the sorted order. The collection should not accept duplicate elements.Search for an particular element using predefined search method in the Collection framework. 

    Program :

    import java.util.*;
    import java.io.*;
    
    class codingexpert10
    {
                public static void main(String[] args) throws Exception
                {
                            int no,element,i;
                            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                            TreeSet ts=new TreeSet();
                            System.out.println("Enter the of elements :");
                            no=Integer.parseInt(br.readLine());
                            System.out.println("Enter elements");
                            for(i=0;i<no;i++)
                            {
                                                    
                                 element=Integer.parseInt(br.readLine());
                                 ts.add(element);
                            }
                           
                                        System.out.println("The elements in sorted order :"+ts);       
                            System.out.println("Enter element to be serach : ");
                            element = Integer.parseInt(br.readLine());
                            if(ts.contains(element))
                                        System.out.println("Element is found");
                            else
                                        System.out.println("Element is NOT found");
                }
    }
    

    Output :




    codingexpert10@kali-linux:~/Desktop/Ty$ javac codingexpert10.java
    Note: codingexpert10.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    codingexpert@kali-linux:~/Desktop/Ty$ java codingexpert10 
    Enter the of elements :
    3
    Enter elements
    1
    2
    3
    The elements in sorted order :[1, 2, 3]
    Enter element to be serach : 
    3
    Element is found
    

    Post a Comment

    1 Comments

    1. Java course in Delhi
      https://techplanet.today/post/how-to-find-the-right-why-is-java-important-for-your-specific-skills
      APTRON Delhi is an Authorized Java Training Center in Delhi for various Java Course in Delhi. APTRON Delhi is proud to say that we are rated as the best online and classroom training provider in Delhi. Our Java course in Delhi is designed as per the IT industry requirements. APTRON Delhi has a dedicated Placement team to ensure that every student trained gets an aspiring career. Students get unlimited support until they get placed.

      ReplyDelete

    Thanks,To visit this blog.