1) poll, pollFirst, pollLast : display plus remove
importjava.util.LinkedList;publicclassLinkedListExample {publicstaticvoidmain(String[] args) {LinkedList<Integer> linkedList =newLinkedList<Integer>();linkedList.add(23);linkedList.add(24);linkedList.add(25);linkedList.add(28);linkedList.add(29);System.out.println("Linked list before poll() ");System.out.println(linkedList);linkedList.poll();System.out.println("Linked list after poll() ");System.out.println(linkedList);System.out.println("Linked list before pollFirst() ");System.out.println(linkedList);linkedList.pollFirst();System.out.println("Linked list after pollFirst() ");System.out.println(linkedList);System.out.println("Linked list before pollLast() ");System.out.println(linkedList);linkedList.pollLast();System.out.println("Linked list after pollLast() ");System.out.println(linkedList); }}
Linked list before poll()[23,24,25,28,29]Linked list after poll()[24,25,28,29]Linked list before pollFirst()[24,25,28,29]Linked list after pollFirst()[25,28,29]Linked list before pollLast()[25,28,29]Linked list after pollLast()[25,28]
importjava.util.LinkedList;publicclassLinkedListExample {publicstaticvoidmain(String[] args) {LinkedList<Integer> linkedList =newLinkedList<Integer>();linkedList.add(23);linkedList.add(24);linkedList.add(25);linkedList.add(28);linkedList.add(29);System.out.println("Linked list before peek() ");System.out.println(linkedList);System.out.println("element peeked "+linkedList.peek());System.out.println("Linked list after peek() ");System.out.println(linkedList);System.out.println("Linked list before peekFirst() ");System.out.println(linkedList);System.out.println("element peeked "+linkedList.peekFirst());System.out.println("Linked list after peekFirst() ");System.out.println(linkedList);System.out.println("Linked list before peekLast() ");System.out.println(linkedList);System.out.println("element peeked "+linkedList.peekLast());System.out.println("Linked list after peekLast() ");System.out.println(linkedList); }}
Linked list before peek()[23,24,25,28,29]element peeked 23Linked list after peek()[23,24,25,28,29]Linked list before peekFirst()[23,24,25,28,29]element peeked 23Linked list after peekFirst()[23,24,25,28,29]Linked list before peekLast()[23,24,25,28,29]element peeked 29Linked list after peekLast()[23,24,25,28,29]
3) offer, offerFirst, offerLast : to add element at first or last
importjava.util.LinkedList;publicclassLinkedListExampleOp2 {publicstaticvoidmain(String[] args) {LinkedList<Integer> linkedList =newLinkedList<Integer>();linkedList.add(23);linkedList.add(24);linkedList.add(25);linkedList.add(28);linkedList.add(29);System.out.println("Linked list before offer(someInt) ");System.out.println(linkedList);System.out.println("element offered "+linkedList.offer(30));System.out.println("Linked list after offer(someInt) ");System.out.println(linkedList);System.out.println("Linked list before offerFirst(someInt) ");System.out.println(linkedList);System.out.println("element offered "+linkedList.offerFirst(31));System.out.println("Linked list after offerFirst(someInt) ");System.out.println(linkedList);System.out.println("Linked list before offerLast(someInt) ");System.out.println(linkedList);System.out.println("element offered "+linkedList.offerLast(32));System.out.println("Linked list after offerLast(someInt) ");System.out.println(linkedList); }}
Linked list before offer(someInt)[23,24,25,28,29]element offered trueLinked list after offer(someInt)[23,24,25,28,29,30]Linked list before offerFirst(someInt)[23,24,25,28,29,30]element offered trueLinked list after offerFirst(someInt)[31,23,24,25,28,29,30]Linked list before offerLast(someInt)[31,23,24,25,28,29,30]element offered trueLinked list after offerLast(someInt)[31,23,24,25,28,29,30,32]
4) indexOf , lastIndexOf :
importjava.util.LinkedList;publicclassLinkedListIndexOfEx {publicstaticvoidmain(String[] args) {LinkedList<Integer> linkedList =newLinkedList<Integer>();linkedList.add(23); // 0linkedList.add(24); // 1linkedList.add(25); // 2 -- first index of 25linkedList.add(28); // 3linkedList.add(25); // 4linkedList.add(29); // 5linkedList.add(25); // 6 -- last index of 25linkedList.add(31); // 7int index =linkedList.indexOf(25);System.out.println("first occurence index of 25 is "+ index); index =linkedList.lastIndexOf(25);System.out.println("last occurence index of 25 is "+ index); }}
first occurence index of 25 is 2last occurence index of 25 is 6