Example usage for java.util Deque push

List of usage examples for java.util Deque push

Introduction

In this page you can find the example usage for java.util Deque push.

Prototype

void push(E e);

Source Link

Document

Pushes an element onto the stack represented by this deque (in other words, at the head of this deque) if it is possible to do so immediately without violating capacity restrictions, throwing an IllegalStateException if no space is currently available.

Usage

From source file:Main.java

public static void main(String[] args) {
    // Create a Deque and use it as stack
    Deque<String> deque = new ArrayDeque<>();
    deque.push("Oracle");
    deque.push("HTML");
    deque.push("CSS");
    deque.push("XML");

    System.out.println("Stack: " + deque);

    // remove all elements from the Deque
    while (deque.peek() != null) {
        System.out.println("Element at  top:  " + deque.peek());
        System.out.println("Popped: " + deque.pop());
        System.out.println("Stack: " + deque);
    }//w  w w  .  j  a  v a2s .c  om

    System.out.println("Stack is  empty:  " + deque.isEmpty());
}

From source file:Main.java

public static void main(String args[]) {
    Deque<String> stack = new ArrayDeque<String>();
    Deque<String> queue = new ArrayDeque<String>();

    stack.push("A");
    stack.push("B");
    stack.push("C");
    stack.push("D");

    while (!stack.isEmpty())
        System.out.print(stack.pop() + " ");

    queue.add("A");
    queue.add("B");
    queue.add("C");
    queue.add("D");
    while (!queue.isEmpty())
        System.out.print(queue.remove() + " ");
}

From source file:Main.java

public static void main(String[] args) {

    Deque<Integer> deque = new ArrayDeque<Integer>(8);

    deque.add(1);//from w w  w. ja v a 2 s  .  com
    deque.add(2);
    deque.add(3);

    // adding elements using push() method
    deque.push(4);
    deque.push(5);

    System.out.println(deque);
}

From source file:edu.oregonstate.eecs.mcplan.domains.spbj.SpBjSimulator.java

public static void main(final String[] argv) throws IOException {
    final int seed = 43;
    final RandomGenerator rng = new MersenneTwister(seed);
    //      final Deck deck = new InfiniteSpanishDeck( rng );

    // TODO: Debugging code
    final Deque<Card> stacked = new ArrayDeque<Card>();
    for (int i = 0; i < 20; ++i) {
        stacked.push(Card.C_2c);
        stacked.push(Card.C_2d);/*ww  w.j  av a2  s  .c o  m*/
        stacked.push(Card.C_2h);
        stacked.push(Card.C_2s);
    }
    final Deck deck = new StackedDeck(stacked);

    //      final ArrayList<ArrayList<Card>> test_hands = new ArrayList<ArrayList<Card>>();
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_Kc, Card.C_9h, Card.C_2c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_6c, Card.C_7h, Card.C_8c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_6c, Card.C_7c, Card.C_8c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_6s, Card.C_7s, Card.C_8s ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_7c, Card.C_7h, Card.C_7c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_7c, Card.C_7c, Card.C_7c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_7s, Card.C_7s, Card.C_7s ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_9c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_6c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c ) ) );
    //      test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_3c, Card.C_2c, Card.C_Ac ) ) );
    //
    //      final ArrayList<ArrayList<Card>> dealer_test_hands = new ArrayList<ArrayList<Card>>();
    //      dealer_test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_Kc, Card.C_Kc ) ) );
    //      dealer_test_hands.add( new ArrayList<Card>( Arrays.asList( Card.C_Kc, Card.C_Ac ) ) );
    //
    //      for( final ArrayList<Card> dealer_cards : dealer_test_hands ) {
    //         for( final ArrayList<Card> cards : test_hands ) {
    //            final SpBjState s = new SpBjState( deck );
    //            s.init();
    //            s.dealer_hand.clear();
    //            s.dealer_hand.addAll( dealer_cards );
    //            s.player_hand.hands.set( 0, cards );
    //            final SpBjSimulator sim = new SpBjSimulator( s );
    //            sim.takeAction( new JointAction<SpBjAction>(
    //               new SpBjAction( new SpBjActionCategory[] { SpBjActionCategory.Pass } ) ) );
    //
    //            System.out.print( "Hand: " );
    //            System.out.print( sim.state().player_hand );
    //            System.out.print( " (" );
    //            final ArrayList<int[]> values = sim.state().player_hand.values();
    //            for( int i = 0; i < values.size(); ++i ) {
    //               if( i > 0 ) {
    //                  System.out.print( ", " );
    //               }
    //               System.out.print( Arrays.toString( values.get( i ) ) );
    //            }
    //            System.out.println( ")" );
    //
    //            System.out.print( "Reward: " );
    //            System.out.println( Arrays.toString( sim.reward() ) );
    //            System.out.print( "Dealer hand: " );
    //            System.out.print( sim.state().dealerHand().toString() );
    //            System.out.print( " (" );
    //            System.out.print( SpBjHand.handValue( sim.state().dealerHand() )[0] );
    //            System.out.println( ")" );
    //            System.out.println( "----------------------------------------" );
    //         }
    //      }

    while (true) {
        final SpBjState s = new SpBjState(deck);
        s.init();
        final SpBjSimulator sim = new SpBjSimulator(s);

        final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (!s.isTerminal()) {
            System.out.print("Dealer showing: ");
            System.out.println(sim.state().dealerUpcard());

            System.out.print("Hand: ");
            System.out.print(sim.state().player_hand);
            System.out.print(" (");
            final ArrayList<int[]> values = sim.state().player_hand.values();
            for (int i = 0; i < values.size(); ++i) {
                if (i > 0) {
                    System.out.print(", ");
                }
                System.out.print(Arrays.toString(values.get(i)));
            }
            System.out.println(")");

            final SpBjActionGenerator actions = new SpBjActionGenerator();
            actions.setState(sim.state(), 0);
            for (final SpBjAction a : Fn.in(actions)) {
                System.out.println(a);
            }

            final String cmd = reader.readLine();
            assert (cmd.length() == sim.state().player_hand.Nhands);
            final SpBjActionCategory[] cat = new SpBjActionCategory[cmd.length()];
            for (int i = 0; i < cmd.length(); ++i) {
                final char c = cmd.charAt(i);
                if ('h' == c) {
                    cat[i] = SpBjActionCategory.Hit;
                } else if ('p' == c) {
                    cat[i] = SpBjActionCategory.Pass;
                } else if ('d' == c) {
                    cat[i] = SpBjActionCategory.Double;
                } else if ('s' == c) {
                    cat[i] = SpBjActionCategory.Split;
                }
            }
            sim.takeAction(new JointAction<SpBjAction>(new SpBjAction(cat)));
        }

        System.out.print("Hand: ");
        System.out.print(sim.state().player_hand);
        System.out.print(" (");
        final ArrayList<int[]> values = sim.state().player_hand.values();
        for (int i = 0; i < values.size(); ++i) {
            if (i > 0) {
                System.out.print(", ");
            }
            System.out.print(Arrays.toString(values.get(i)));
        }
        System.out.println(")");

        System.out.print("Reward: ");
        System.out.println(Arrays.toString(sim.reward()));
        System.out.print("Dealer hand: ");
        System.out.print(sim.state().dealerHand().toString());
        System.out.print(" (");
        System.out.print(SpBjHand.handValue(sim.state().dealerHand())[0]);
        System.out.println(")");
        System.out.println("----------------------------------------");
    }
}

From source file:org.dataconservancy.packaging.tool.impl.generator.IPMUtil.java

public static String path(Node node, String suffix) {
    Deque<String> pathStack = new ArrayDeque<>();

    pathStack.push(suffix);
    pathStack.push(name(node));/*w  ww . j a va 2 s  .com*/

    while (!node.isRoot()) {
        node = node.getParent();
        if (node.getFileInfo() != null && !node.getFileInfo().isFile()) {
            pathStack.push("/");
            pathStack.push(name(node));
        }
    }

    StringBuilder builder = new StringBuilder();
    pathStack.forEach(builder::append);
    return builder.toString();

}

From source file:PathUtils.java

/**
 * Calculates the relative path between a specified root directory and a target path.
 *
 * @param root   The absolute path of the root directory.
 * @param target The path to the target file or directory.
 * @return The relative path between the specified root directory and the target path.
 * @throws IllegalArgumentException <ul><li>The root file cannot be null.</li><li>The target cannot be
 *                                  null.</li><li>The root file must be a directory.</li><li>The root file must be
 *                                  absolute.</li></ul>
 *///from  ww w.  ja  v a2  s  .c o m
public static String relativize(final File root, final File target) throws IllegalArgumentException {
    if (root == null)
        throw new IllegalArgumentException("The root file cannot be null.");
    if (target == null)
        throw new IllegalArgumentException("The target cannot be null.");
    if (!root.isDirectory())
        throw new IllegalArgumentException("The root file must be a directory.");
    if (!root.isAbsolute())
        throw new IllegalArgumentException("The root file must be absolute.");
    if (!target.isAbsolute())
        return target.toString();

    if (root.equals(target))
        return ".";

    // Deconstruct hierarchies
    final Deque<File> rootHierarchy = new ArrayDeque<File>();
    for (File f = root; f != null; f = f.getParentFile())
        rootHierarchy.push(f);
    final Deque<File> targetHierarchy = new ArrayDeque<File>();
    for (File f = target; f != null; f = f.getParentFile())
        targetHierarchy.push(f);

    // Trace common root
    while (rootHierarchy.size() > 0 && targetHierarchy.size() > 0
            && rootHierarchy.peek().equals(targetHierarchy.peek())) {
        rootHierarchy.pop();
        targetHierarchy.pop();
    }
    // Create relative path
    final StringBuilder sb = new StringBuilder(rootHierarchy.size() * 3 + targetHierarchy.size() * 32);
    while (rootHierarchy.size() > 0) {
        sb.append("..");
        rootHierarchy.pop();
        if (rootHierarchy.size() > 0 || targetHierarchy.size() > 0)
            sb.append(File.separator);
    }
    while (targetHierarchy.size() > 0) {
        sb.append(targetHierarchy.pop().getName());
        if (targetHierarchy.size() > 0)
            sb.append(File.separator);
    }
    return sb.toString();
}

From source file:info.servertools.core.util.FileUtils.java

public static void zipDirectory(File directory, File zipfile, @Nullable Collection<String> fileBlacklist,
        @Nullable Collection<String> folderBlacklist) throws IOException {
    URI baseDir = directory.toURI();
    Deque<File> queue = new LinkedList<>();
    queue.push(directory);
    OutputStream out = new FileOutputStream(zipfile);
    Closeable res = out;//from   w  w  w .j a  va  2s . c  o m
    try {
        ZipOutputStream zout = new ZipOutputStream(out);
        res = zout;
        while (!queue.isEmpty()) {
            directory = queue.removeFirst();
            File[] dirFiles = directory.listFiles();
            if (dirFiles != null && dirFiles.length != 0) {
                for (File child : dirFiles) {
                    if (child != null) {
                        String name = baseDir.relativize(child.toURI()).getPath();
                        if (child.isDirectory()
                                && (folderBlacklist == null || !folderBlacklist.contains(child.getName()))) {
                            queue.push(child);
                            name = name.endsWith("/") ? name : name + "/";
                            zout.putNextEntry(new ZipEntry(name));
                        } else {
                            if (fileBlacklist != null && !fileBlacklist.contains(child.getName())) {
                                zout.putNextEntry(new ZipEntry(name));
                                copy(child, zout);
                                zout.closeEntry();
                            }
                        }
                    }
                }
            }
        }
    } finally {
        res.close();
    }
}

From source file:org.ambraproject.rhino.content.xml.AbstractXpathReader.java

private static List<String> getParentChain(Node node) {
    Deque<String> stack = new ArrayDeque<>();
    while (node != null) {
        stack.push(node.getNodeName());
        node = node.getParentNode();//w  w  w.  j  av  a2 s.com
    }
    return ImmutableList.copyOf(stack);
}

From source file:org.apache.nifi.web.security.DnUtils.java

/**
 * Tokenizes the specified proxy chain.//from   w w w .  ja  v a2 s.co m
 *
 * @param rawProxyChain raw chain
 * @return tokenized proxy chain
 */
public static Deque<String> tokenizeProxyChain(String rawProxyChain) {
    final Deque<String> dnList = new ArrayDeque<>();

    // parse the proxy chain
    final Matcher rawProxyChainMatcher = proxyChainPattern.matcher(rawProxyChain);
    while (rawProxyChainMatcher.find()) {
        dnList.push(rawProxyChainMatcher.group(1));
    }

    return dnList;
}

From source file:net.myrrix.common.io.IOUtils.java

/**
 * Attempts to recursively delete a directory. This may not work across symlinks.
 *
 * @param dir directory to delete along with contents
 * @return {@code true} if all files and dirs were deleted successfully
 *///from  www.j  av a 2s  . com
public static boolean deleteRecursively(File dir) {
    if (dir == null) {
        return false;
    }
    Deque<File> stack = new ArrayDeque<File>();
    stack.push(dir);
    boolean result = true;
    while (!stack.isEmpty()) {
        File topElement = stack.peek();
        if (topElement.isDirectory()) {
            File[] directoryContents = topElement.listFiles();
            if (directoryContents != null && directoryContents.length > 0) {
                for (File fileOrSubDirectory : directoryContents) {
                    stack.push(fileOrSubDirectory);
                }
            } else {
                result = result && stack.pop().delete();
            }
        } else {
            result = result && stack.pop().delete();
        }
    }
    return result;
}