DEV Community

Uday Yadav
Uday Yadav

Posted on

Java Internal DataStructures

Arrays

  • arr.length : int

    • returns the length of array
  • .toString(arr) : String

  • binarySearch(arr, (Object) key) : int

    • return negative if not found
    • else return index of the key
  • .deepToString(arr[][]) : for 2d array

  • sort(arr) : void

    • Sorts the array
  • .stream(arr).sum() : int

import java.util.Arrays

class App {
    public static void main(String[] args) {

        int[] numArr = new int[10];
        for (int i = 0; i < numArr.length; i++) {
            numArr[i] = i * 2;
        }

        int[] numArr2 = new int[10];
        for (int i = 0; i < numArr.length; i++) {
            numArr2[i] = 10 - i;
        }

        int[][] arr = new int[4][4];

        // arr.length
        System.out.println("length: " + numArr.length);

        // toString method
        System.out.println("print arr : ");
        System.out.println(Arrays.toString(numArr));
        System.out.println(Arrays.toString(numArr2));

        // returns index
        System.out.println("index of 4 in numArr: " +
                Arrays.binarySearch(numArr, 4));

        // to print 2D array as string
        System.out.println("deepToString() : ");
        System.out.println(Arrays.deepToString(arr));

        // to sort array
        Arrays.sort(numArr2);
        System.out.println(Arrays.toString(numArr2));

        // to calc sum : convert to stream
        System.out.println("Sum: " + Arrays.stream(numArr).sum());

    }
}
Enter fullscreen mode Exit fullscreen mode

List : ArrayList

  • arr.size() : int
  • arr.get(index) : int
  • arr.isEmpty() : boolean
  • arr.add(Object) : boolean
  • arr.addAll(Collections)
  • arr.clear() : void
  • arr.contains(Object) : boolean
  • arr.indexOf(Object) : int
  • arr.remove(index) : int
  • arr.sort(null)
  • arr.toArray() : Object[]
  • arr.set(index, element)
import java.util.ArrayList;
import java.util.Arrays;

class App {
    public static void main(String[] args) {

        ArrayList<Integer> arrlst = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            arrlst.add(i);
        }

        System.out.println(arrlst.toString());

        // size to arrlst
        System.out.println("Size: " + arrlst.size());

        // get element from index arrlst
        System.out.println("index: " + arrlst.get(3));

        // arrlst isEmpty ?
        System.out.println("isEmpty : " + arrlst.isEmpty());

        // add one element
        arrlst.add(10);

        ArrayList<Integer> arrlst2 = new ArrayList<>();
        arrlst2.add(11);
        arrlst2.add(12);
        // addAll
        arrlst.addAll(arrlst2);

        System.out.println(arrlst.toString());
        System.out.println(arrlst2.toString());
        // clear
        arrlst2.clear();

        // contains
        System.out.println(arrlst.contains(13) + " " + arrlst.contains(1));

        // indexOf
        System.out.println("indexof: " + arrlst.indexOf(6));

        // remove
        System.out.println("element removed: " + arrlst.remove(6));
        System.out.println(arrlst.toString());

        // set
        arrlst.set(2, 4);
        System.out.println(arrlst.toString());

        // sort
        arrlst.sort(null);
        System.out.println(arrlst.toString());

        // toArray
        var something = arrlst.toArray();
        System.out.println(Arrays.toString(something));

    }
}
Enter fullscreen mode Exit fullscreen mode

Stack

  • pop
  • push
  • peek
  • empty
  • size
public class App {

    public static void main(String[] args) {
        Stack<Integer> stck = new Stack<>();

        try {
            stck.peek();
        } catch (Exception e) {
            System.out.println("if stack is empty, then peek throws an exception");
        }

        stck.push(1);
        stck.push(2);
        stck.push(3);

        System.out.println(stck.pop());
        System.out.println(stck.peek());
        System.out.println(stck.isEmpty());
        System.out.println(stck.isEmpty());
        System.out.println(stck.size());
    }
}
Enter fullscreen mode Exit fullscreen mode

Queue

public class App {

    public static void main(String[] args) {
        Queue<Integer> queue = new LinkedList<>();

        queue.add(1);
        queue.add(2);
        queue.add(3);
        queue.add(4);

        // throws exception when queue is empty
        System.out.println(queue.remove());
        // returns null when queue is empty
        System.out.println(queue.poll());

        System.out.println(queue.peek());

        // if queue under capacity then offer returns true else returns false
        System.out.println(queue.offer(6));
        System.out.println(queue.toString());

    }
}
Enter fullscreen mode Exit fullscreen mode

Set

public class App {

    public static void main(String[] args) {

        Set<Integer> set = new HashSet<>();
        set.add(1);
        set.add(2);
        set.add(3);

        System.out.println(set.contains(2));
        System.out.println(set.remove(3));
        System.out.println(set.isEmpty());
        System.out.println(set.isEmpty());
        System.out.println(set.toString());
    }
}
Enter fullscreen mode Exit fullscreen mode

Map

public class App {

    public static void main(String[] args) {
        Map<Integer,String> hmap = new HashMap<>();

        hmap.put(1,"uday yadav");
        hmap.putIfAbsent(2,"uday yadav");
        hmap.putIfAbsent(2,"another uday");
        hmap.putIfAbsent(3,"yadav uday");


        System.out.println(hmap.containsKey(2));
        hmap.getOrDefault(6,"");
        hmap.remove(3);
        System.out.println(hmap.toString());
        hmap.size();
        hmap.clear();
        System.out.println(hmap.toString());
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)