[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/CodeForHunger/Ultimate-Java-Resources/master/JavaCollections.java [Back]  [Original]

import java.util.*; 
  
public class CollectionsFramework { 
  
    // main method 
    public static void main(String[] args) { 
        
        //declaring an array in java
        int arr[] = {10, 20, 30, 40, 50};
        for(int i=0; i < arr.length; i++)
        {
               System.out.print(" " + arr[i]);              
        }
        
        // declaring any 2d array in Java or it can be used to declare the 1d matrix using new keyword
        int arr[][] = new int[2][];

        // Making the above array Jagged

        // First row has 3 columns
        arr[0] = new int[3];

        // Second row has 2 columns
        arr[1] = new int[2];
        // Example 
        int[][] arr = { { 1, 2 }, { 3, 4 } };
      
        System.out.println(Integer.bitCount(4)); // Gives out the set bits count in 4
      
        // Strings in Java
      
        String s= "GeeksforGeeks";
        // or String s= new String ("GeeksforGeeks");

        // Returns the number of characters in the String.
        System.out.println("String length = " + s.length());

        // Returns the character at ith index.
        System.out.println("Character at 3rd position = "
                           + s.charAt(3));

        // Return the substring from the ith index character
        // to end of string
        System.out.println("Substring " + s.substring(3));

        // Returns the substring from i to j-1 index.
        System.out.println("Substring = " + s.substring(2,5));

        // Concatenates string2 to the end of string1.
        String s1 = "Geeks";
        String s2 = "forGeeks";
        System.out.println("Concatenated string = " +
                            s1.concat(s2));

        // Returns the index within the string
        // of the first occurrence of the specified string.
        String s4 = "Learn Share Learn";
        System.out.println("Index of Share " + 
                           s4.indexOf("Share"));

        // Returns the index within the string of the
        // first occurrence of the specified string,
        // starting at the specified index.
        System.out.println("Index of a = " + 
                           s4.indexOf('a',3));

        // Checking equality of Strings
        Boolean out = "Geeks".equals("geeks");
        System.out.println("Checking Equality " + out);
        out = "Geeks".equals("Geeks");
        System.out.println("Checking Equality " + out);

        out = "Geeks".equalsIgnoreCase("gEeks ");
        System.out.println("Checking Equality " + out);
        
        //If ASCII difference is zero then the two strings are similar
        int out1 = s1.compareTo(s2);
        System.out.println("the difference between ASCII value is="+out1);
        // Converting cases
        String word1 = "GeeKyMe";
        System.out.println("Changing to lower Case " +
                            word1.toLowerCase());

        // Converting cases
        String word2 = "GeekyME";
        System.out.println("Changing to UPPER Case " + 
                            word2.toUpperCase());

        // Trimming the word
        String word4 = " Learn Share Learn ";
        System.out.println("Trim the word " + word4.trim());

        // Replacing characters
        String str1 = "feeksforfeeks";
        System.out.println("Original String " + str1);
        String str2 = "feeksforfeeks".replace('f' ,'g') ;
        System.out.println("Replaced f with g -> " + str2);
          
        // https://www.geeksforgeeks.org/how-to-learn-java-collections-a-complete-guide/
        // Instantiate an ArrayList Object 
        // Integer is a wrapper class for  
        // the basic datatype int 
        ArrayList intArr = new ArrayList(); 
          
        // Add elements using add() method 
        intArr.add(10); 
        intArr.add(12); 
        intArr.add(25); 
        intArr.add(19); 
        intArr.add(11); 
        intArr.add(3); 
          
        // Print the ArrayList on the console 
        System.out.println(intArr); 
          
        // Remove elements at index 1 and 4 
        intArr.remove(1); 
        intArr.remove(4); 
          
        // Print the ArrayList on the console 
        System.out.println(intArr); 
          
        // Check if intArr contains the element 25 
        if(intArr.contains(25)) 
        { 
            System.out.println("The ArrayList contains 25"); 
        } 
        else
        { 
            System.out.println("No such element exists"); 
        } 
          
        // Use get method to get the element at index 1 
        int elementAt1 = intArr.get(1); 
        System.out.println("The Element at index 1 now is " + elementAt1); 
      
        Collections.reverse(intArr);  // reversing an arraylist
      
        List arr = intArr.subList(st,en);       // slicing of arraylist in java List is an interface of arraylist


        //-------------------------------------------------------------------------------

        Vector intVector = new Vector(); 
          
        // Print the initial size of the Vector 
        System.out.println("The initial size of the Vector = " + intVector.size()); 
        System.out.println(); 
          
        // Add elements using add method 
        intVector.add(11); 
        intVector.add(18); 
        intVector.add(1); 
        intVector.add(87); 
        intVector.add(19); 
        intVector.add(11); 
          
        // Print the Vector on the console 
        System.out.println("The Vector intVector : "); 
        System.out.println(intVector); 
        System.out.println("Size of intVector : " + intVector.size()); 
          
        System.out.println(); 
          
        // Remove the element at index 2 
        intVector.remove(2); 
          
        // Print the vector again on the console 
        System.out.println("The Vector intVector after removing element at 2 : "); 
        System.out.println(intVector); 
          
        System.out.println(); 
          
        // Clear all elements of the Vector and 
        // Print the Vector on the console 
        intVector.clear(); 
        System.out.println("The Vector intVector after using clear : "); 
        System.out.println(intVector); 


        //--------------------------------------------------------------------------------------

        Stack strStack = new Stack(); 
          
        // Add elements using the push() method 
        strStack.push("Stack"); 
        strStack.push("a"); 
        strStack.push("is"); 
        strStack.push("This"); 
          
        // The size() method gives the 
        // number of elements in the Stack 
        System.out.println("The size of the Stack is : " + strStack.size()); 
          
        // The search() method is 
        // used to search an element 
        // it returns the position of 
        // the element 
        int position = strStack.search("a"); 
        System.out.println("\nThe string 'a' is at position " + position); 
          
        System.out.println("\nThe elements of the stack are : "); 
        String temp; 
        int num = strStack.size(); 
          
        for(int i = 1; i 

Web Proxy Viewer  |  New URL  |  Original Page