Java - Basics

Lets learn the basics of Java collections with code samples


  1. Arrays
    Most of the time we only need one property of an array that is its length below is a sample code

    int[] 
    intArray = new int[10];
    int arrayLen = intArray.length;


  2. Chars
    Characters data type is another common data type used in programming. For example to convert a character to an integer

    int intValue = Character.getNumericValue(example.charAt(0));
    
    
  3. Strings
    Strings have several other useful properties unlike arrays like, length, convert to integer to string, to char array to name a few. Below are a set of examples.

    String example = "You are almost there";
    
    //Find the length of string 
    System.out.println("String Length is: " + example.length());
    
    //Convert string to Char array 
    char[] charArr = example.toCharArray();
    
    //Access a character at index i 
    char charAtIndex = example.charAt(0);
    
    //Get each character's ascii 
    int asciiValue = example.codePointAt(0);
    
    //Get access to the ASCII value of the character before index i 
    int k = example.codePointBefore(1);
    
    //To compare two strings, it return 0 if they are same,
    // else returns a negative number if argument is lexicographically greater// 
    return a positive number if the argument is lexicographically lesser than the parent string
    int compare = example.compareTo("You are almost there");
    
    //Same as compareTo except it ignores case 
    int ignoreCaseCompare = example.compareToIgnoreCase("you are almost there");
    
    //To concatenate a string to the existing one, it resturns a String 
    String concatenated = example.concat(" on your way to Google");
    
    //To compare another string for equality. 
    boolean bool = example.equals("You are almost there");
    
    //To find the starting index of another string within a String 
    int index = example.indexOf("almost");
    
    
  4. StringBuilder

    String
     is immutable, if you try to alter their values, another object gets created, whereas
    StringBuffer and 
    StringBuilder are mutable so they can change their values. Thread-

    Safety 
    Difference: The difference between StringBuffer and StringBuilder
     is that StringBuffer is thread-safe.

    Now lets see most frequently used functions of a StringBuilder class
  5. Stack
    Stack uses LIFO principle and is a very useful technique for any programming task.

    //Initialize a Stack 
    Stack stack = new Stack();
    //Push an element into a Stack
    stack.push(1);
    
    //Pop an element from a Stack, it returns a
    int k = stack.pop(); 
    
    
    //Peek an element from top of the stack
    
    stack.peek();
    //Check if the stack is empty
    stack.empty();
  6. Queue

    Queue q = new LinkedList();
    
    //To add an element in Queue
    q.add(1);
    //To remove an element in Queue
    q.remove()
  7. Priority Queue
  8. List

    List tempList = new ArrayList()
    int length = tempList.size();
  9. Array List
  10. Hash Map

    HashMap hashMap = new HashMap();
    
    
    //To get put elements in HashMap
    hashMap.put(1,"Arun"); hashMap.put(2,"Amma and Nanna"); hashMap.put(3,"Theertha");
    
    //To get size of hashMap
    System.out.println("Hash Map Size " + hashMap.size());
  11. Hash Set
         HashSet hSet = new HashSet();
     hSet.add(1);
     hSet.add(2);
     hSet.add(3);
       

     //To get size of hashMap
         if(hSet.contains(1))
          System.out.println("Hash Map Contains 1");
         





No comments:

Post a Comment