Java – Deque Example

import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;

public class DequeExample {

    public static void main(String[] args) {
        Deque deque = new LinkedList<>();

        // We can add elements to the queue in various ways
        deque.add("Element 1 (Tail)"); // add to tail
        deque.addFirst("Element 2 (Head)");
        deque.addLast("Element 3 (Tail)");
        deque.push("Element 4 (Head)"); //add to head
        deque.offer("Element 5 (Tail)");
        deque.offerFirst("Element 6 (Head)");
        deque.offerLast("Element 7 (Tail)");

        System.out.println(deque + "\n");

        // Iterate through the queue elements.
        System.out.println("Standard Iterator");
        Iterator iterator = deque.iterator();
        while (iterator.hasNext()) {
            System.out.println("\t" + iterator.next());
        }

        // Reverse order iterator
        Iterator reverse = deque.descendingIterator();
        System.out.println("Reverse Iterator");
        while (reverse.hasNext()) {
            System.out.println("\t" + reverse.next());
        }

        // Peek returns the head, without deleting it from the deque
        System.out.println("Peek " + deque.peek());
        System.out.println("After peek: " + deque);

        // Pop returns the head, and removes it from the deque
        System.out.println("Pop " + deque.pop());
        System.out.println("After pop: " + deque);

        // We can check if a specific element exists in the deque
        System.out.println("Contains element 3: " + deque.contains("Element 3 (Tail)"));

        // We can remove the first / last element.
        deque.removeFirst();
        deque.removeLast();
        System.out.println("Deque after removing first and last: " + deque);
    }
}

Output:

[Element 6 (Head), Element 4 (Head), Element 2 (Head), Element 1 (Tail), Element 3 (Tail), Element 5 (Tail), Element 7 (Tail)]

Standard Iterator
Element 6 (Head)
Element 4 (Head)
Element 2 (Head)
Element 1 (Tail)
Element 3 (Tail)
Element 5 (Tail)
Element 7 (Tail)
Reverse Iterator
Element 7 (Tail)
Element 5 (Tail)
Element 3 (Tail)
Element 1 (Tail)
Element 2 (Head)
Element 4 (Head)
Element 6 (Head)
Peek Element 6 (Head)
After peek: [Element 6 (Head), Element 4 (Head), Element 2 (Head), Element 1 (Tail), Element 3 (Tail), Element 5 (Tail), Element 7 (Tail)]
Pop Element 6 (Head)
After pop: [Element 4 (Head), Element 2 (Head), Element 1 (Tail), Element 3 (Tail), Element 5 (Tail), Element 7 (Tail)]
Contains element 3: true

Java JSP – Using Abstract Class

<HTML>
    <HEAD>
        <TITLE>Using Abstract Classes</TITLE>
    </HEAD>

    <BODY>
        <H1>Using Abstract Classes</H1>

        <%!
            javax.servlet.jsp.JspWriter localOut;

            abstract class a {
                abstract String getText() throws java.io.IOException;

                public void printem() throws java.io.IOException {
                    localOut.println(getText());
                }
            }

            class b extends a {
                String getText() throws java.io.IOException {
                    return "Hello from JSP!";
                }
            }
        %>
        <%
            localOut = out;     

            b bObject = new b();

            bObject.printem();
        %>
    </BODY>
</HTML>

Java – Bubble Sort Array Example

import java.util.Arrays;

/**
 * Algorithm
 *
 * 1] Compare each pair of adjacent elements from beginning
 * of an array and, if they are in reversed order, swap them.
 *
 * 2] If at least one swap has been done, repeat step 1.
 *
 */
public class MyBubbleSort {

	private void sortData(int[] data) {
		boolean swapDone = false;
		do {
			swapDone = false;
			for (int i = 0; i < data.length-1; i++) {
				if (data[i] > data[i+1]) {
					swapDone = true;
					int tmp = data[i];
					data[i] = data[i+1];
					data[i+1] = tmp;
				}
			}
		} while (swapDone == true);
	}

	public static void main(String[] args) {
		int[] data = {5,1,12,-5,16};
		System.out.println(Arrays.toString(data));

		new MyBubbleSort().sortData(data);
		System.out.println(Arrays.toString(data));
	}

}

Page 1 of 4912345...102030...Last »