Java Tutorial,Java Programs,Java interview Questions,JSP Tutorial,Servlet Tutorial,Spring Tutorial,Mysql Tutorial,java interview question,jsp interview question,spring interview question,mysql interview question,cassandra tutorial,guava tutorial,guava example,javascript tutorial,ajax example,python tutorial
Saturday, 30 May 2015
how to install spark on ubuntu
Step 1:
download spark click here
or
use this command
wget http://download.igniterealtime.org/spark/spark_2_6_3.tar.gz
step 2:
Now extract tar file Using this command
tar -zxvf spark_2_7_0.tar.gz
and it makes Spark folder
Step 3:
Now go to in Spark folder and run this Command
Dell-System-Inspiron-N4110:~/Spark$ ./Spark
After this you can see spark windows
Wednesday, 27 May 2015
How to check HashSet Empty Or not
import java.util.HashSet;
import java.util.Set;
public class IsEmpty {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("hashset = "+hs);
System.out.println(hs.isEmpty());
HashSet<String> hs2 = new HashSet<String>();
System.out.println(hs2.isEmpty());
}
}
Output:
hashset = [javascript, php, html, java, mysql]
false
true
import java.util.Set;
public class IsEmpty {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("hashset = "+hs);
System.out.println(hs.isEmpty());
HashSet<String> hs2 = new HashSet<String>();
System.out.println(hs2.isEmpty());
}
}
Output:
hashset = [javascript, php, html, java, mysql]
false
true
How to count number of element in HashSet
Size() Method return number of element in the HashSet.
import java.util.HashSet;
import java.util.Set;
public class CountElement {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("hashset = "+hs);
/*
* Returns the number of elements in set. If this set contains more than
* Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
*/
System.out.println("Size of hashset = "+hs.size());
}
}
Output:
hashset = [javascript, php, html, java, mysql]
Size of hashset = 5
import java.util.HashSet;
import java.util.Set;
public class CountElement {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("hashset = "+hs);
/*
* Returns the number of elements in set. If this set contains more than
* Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
*/
System.out.println("Size of hashset = "+hs.size());
}
}
Output:
hashset = [javascript, php, html, java, mysql]
Size of hashset = 5
How to find does HashSet contains elements or not
Returns true if this set contains the specified element.we can say returns true if and only if this set contains an element e such that (o==null ? e==null :
o.equals(e)).
import java.util.HashSet;
import java.util.Set;
public class ContailsElement {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("hashset = "+hs);
System.out.println(hs.contains("java"));
System.out.println(hs.contains("HTml"));
System.out.println(hs.contains("delhi"));
}
}
hashset = [javascript, php, html, java, mysql]
true
false
false
o.equals(e)).
import java.util.HashSet;
import java.util.Set;
public class ContailsElement {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("hashset = "+hs);
System.out.println(hs.contains("java"));
System.out.println(hs.contains("HTml"));
System.out.println(hs.contains("delhi"));
}
}
hashset = [javascript, php, html, java, mysql]
true
false
false
How To Convert Hashset To Array
import java.util.HashSet;
import java.util.Set;
public class HashSetToArray {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("hashset = "+hs);
String [] arr = new String[hs.size()];
/*
* Returns an array containing all of the
* elements in hs set
*/
hs.toArray(arr);
System.out.println("Array is ");
for (String str : arr) {
System.out.println("Element is = "+str);
}
}
}
Output:
hashset = [javascript, php, html, java, mysql]
Array is
Element is = javascript
Element is = php
Element is = html
Element is = java
Element is = mysql
import java.util.Set;
public class HashSetToArray {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("hashset = "+hs);
String [] arr = new String[hs.size()];
/*
* Returns an array containing all of the
* elements in hs set
*/
hs.toArray(arr);
System.out.println("Array is ");
for (String str : arr) {
System.out.println("Element is = "+str);
}
}
}
Output:
hashset = [javascript, php, html, java, mysql]
Array is
Element is = javascript
Element is = php
Element is = html
Element is = java
Element is = mysql
How to Remove Set From HashSet
package com.javapro.hashset;
import java.util.HashSet;
import java.util.Set;
public class RemoveAll {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
// print hashset
System.out.println("Before Remove Element hs = " + hs);
HashSet<String> hs2 = new HashSet<String>();
hs2.add("java");
hs2.add("html");
hs2.add("two");
System.out.println("hs2 is = " + hs2);
/*
* Removes from hs set all of its elements that are contained in the hs2
* collection
*/
hs.removeAll(hs2);
System.out.println("After Remove All hs = " + hs);
}
}
Output:
Before Remove Element hs = [javascript, php, html, java, mysql]
hs2 is = [two, html, java]
After Remove All hs = [javascript, php, mysql]
import java.util.HashSet;
import java.util.Set;
public class RemoveAll {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
// print hashset
System.out.println("Before Remove Element hs = " + hs);
HashSet<String> hs2 = new HashSet<String>();
hs2.add("java");
hs2.add("html");
hs2.add("two");
System.out.println("hs2 is = " + hs2);
/*
* Removes from hs set all of its elements that are contained in the hs2
* collection
*/
hs.removeAll(hs2);
System.out.println("After Remove All hs = " + hs);
}
}
Output:
Before Remove Element hs = [javascript, php, html, java, mysql]
hs2 is = [two, html, java]
After Remove All hs = [javascript, php, mysql]
How to remove all element from HashSet
import java.util.HashSet;
import java.util.Set;
public class RemoveAll {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("Before Remove element hashset is = "+hs);
/*
* Removes all of the elements from set
*/
hs.clear();
System.out.println("After Remove elements hashset is ="+hs);
}
}
Output:
Before Remove element hashset is = [javascript, php, html, java, mysql]
After Remove elements hashset is =[]
How To Remove Element of Hashset
import java.util.HashSet;
import java.util.Set;
public class RemoveElement {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("Before Remove element hashset is = "+hs);
/*
* Removes the specified element from set
* if it is present
*/
hs.remove("php");
System.out.println("After Remove element hashset is ="+hs);
}
}
Output:
Before Remove element hashset is = [javascript, php, html, java, mysql]
After Remove element hashset is =[javascript, html, java, mysql]
import java.util.Set;
public class RemoveElement {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("Before Remove element hashset is = "+hs);
/*
* Removes the specified element from set
* if it is present
*/
hs.remove("php");
System.out.println("After Remove element hashset is ="+hs);
}
}
Output:
Before Remove element hashset is = [javascript, php, html, java, mysql]
After Remove element hashset is =[javascript, html, java, mysql]
How to copy all element of a Hashset to other
import java.util.HashSet;
import java.util.Set;
public class AddAll {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
// print hashset
System.out.println("hs is = " + hs);
HashSet<String> hashSet = new HashSet<String>();
/*
* Adds all of the elements in the specified
* collection to hashset collection
*/
hashSet.addAll(hs);
System.out.println("hashset is="+hashSet);
}
}
Output:
hs is = [javascript, php, html, java, mysql]
hashset is=[javascript, php, java, html, mysql]
import java.util.Set;
public class AddAll {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
// print hashset
System.out.println("hs is = " + hs);
HashSet<String> hashSet = new HashSet<String>();
/*
* Adds all of the elements in the specified
* collection to hashset collection
*/
hashSet.addAll(hs);
System.out.println("hashset is="+hashSet);
}
}
Output:
hs is = [javascript, php, html, java, mysql]
hashset is=[javascript, php, java, html, mysql]
How to convert array to HashSet
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class ArrayToHashSet {
public static void main(String[] args) {
String[] arr = { "one", "two", "third" };
Set<String> hs = new HashSet<String>(Arrays.asList(arr));
// print hashset
System.out.println("hashset is =" + hs);
}
}
Output:
hashset is =[two, one, third]
import java.util.HashSet;
import java.util.Set;
public class ArrayToHashSet {
public static void main(String[] args) {
String[] arr = { "one", "two", "third" };
Set<String> hs = new HashSet<String>(Arrays.asList(arr));
// print hashset
System.out.println("hashset is =" + hs);
}
}
Output:
hashset is =[two, one, third]
How to Convert a HashSet to an ArrayList
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class HashSetToList {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("hashset = "+hs);
List<String> list = new ArrayList<String>(hs);
System.out.println("list from hashset = "+list);
}
}
Output:
hashset = [javascript, php, html, java, mysql]
list from hashset = [javascript, php, html, java, mysql]
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class HashSetToList {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");
//print hashset
System.out.println("hashset = "+hs);
List<String> list = new ArrayList<String>(hs);
System.out.println("list from hashset = "+list);
}
}
Output:
hashset = [javascript, php, html, java, mysql]
list from hashset = [javascript, php, html, java, mysql]
Tuesday, 26 May 2015
How to storing Java Object In HashSet
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class HashSetOfObject {
public static void main(String[] args) {
Set<User> hs = new HashSet<User>();
hs.add(new User("shivam",1213));
hs.add(new User("yogesh",8383));
hs.add(new User("ashwani", 8287));
Iterator<User> itr =hs.iterator();
while (itr.hasNext()) {
User user = (User) itr.next();
System.out.println("userName ="+user.getName()+" userId = "+user.getUserId());
}
}
}
class User{
private String name;
private int userId;
public User(String name, int userId) {
super();
this.name = name;
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
}
Output:
userName =ashwani userId = 8287
userName =shivam userId = 1213
userName =yogesh userId = 8383
How to create Hashset In Java
import java.util.HashSet;
import java.util.Set;
public class CreateHashset {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("vb");
//print hashset
System.out.println("hashset = "+hs);
}
}
Output:
hashset = [javascript, php, html, java, vb]
import java.util.Set;
public class CreateHashset {
public static void main(String[] args) {
Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("vb");
//print hashset
System.out.println("hashset = "+hs);
}
}
Output:
hashset = [javascript, php, html, java, vb]
How to Convert Array to Vector
import java.util.Arrays;
import java.util.Vector;
public class ArrayToVector {
public static void main(String[] args) {
String [] arr ={"java","c","cpp","php","html"};
Vector<String> vector = new Vector<String>(Arrays.asList(arr));
for (String str : vector) {
System.out.println("Element is = "+str);
}
}
}
Output:
Element is = java
Element is = c
Element is = cpp
Element is = php
Element is = html
import java.util.Vector;
public class ArrayToVector {
public static void main(String[] args) {
String [] arr ={"java","c","cpp","php","html"};
Vector<String> vector = new Vector<String>(Arrays.asList(arr));
for (String str : vector) {
System.out.println("Element is = "+str);
}
}
}
Output:
Element is = java
Element is = c
Element is = cpp
Element is = php
Element is = html
How to use retailAll in vector
Retains only the elements in vector that are contained in the
specified collection. In other words, removes from vector all
of its elements that are not contained in the specified collection.
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class RetainAll {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
List<String> list = new ArrayList<String>();
list.add("hockey");
list.add("php");
list.add("java");
/*
* Retains only the elements in this
* Vector that are contained in the
* specified Collection.
*/
vector.retainAll(list);
System.out.println("After retainAll vector is = "+vector);
}
}
Output:
Vector = [cricket, hockey, football, tennish]
After retainAll vector is = [hockey]
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class RetainAll {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
List<String> list = new ArrayList<String>();
list.add("hockey");
list.add("php");
list.add("java");
/*
* Retains only the elements in this
* Vector that are contained in the
* specified Collection.
*/
vector.retainAll(list);
System.out.println("After retainAll vector is = "+vector);
}
}
Output:
Vector = [cricket, hockey, football, tennish]
After retainAll vector is = [hockey]
How to insert element in vector
import java.util.Vector;
public class AddElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
/*
* Inserts the specified element at the specified
* position in this Vector.
*/
vector.add(1, "java");
System.out.println("vector after insert element = "+vector);
}
}
Output:
Vector = [cricket, hockey, football, tennish]
vector after insert element = [cricket, java, hockey, football, tennish]
public class AddElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
/*
* Inserts the specified element at the specified
* position in this Vector.
*/
vector.add(1, "java");
System.out.println("vector after insert element = "+vector);
}
}
Output:
Vector = [cricket, hockey, football, tennish]
vector after insert element = [cricket, java, hockey, football, tennish]
Monday, 25 May 2015
How to replace element of vector
We can replace any specific index element by other element using set(int index,element) method.
import java.util.Vector;
public class ReplaceElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Before replace Vector is = "+vector);
/*
* Replaces the element at the specified
* position in this Vector with the
* specified element.
*/
vector.set(1,"Apple");
System.out.println("After Replace element vector is = "+vector);
}
}
Output:
Before replace Vector is = [cricket, hockey, football, tennish]
After Replace element vector is = [cricket, Apple, football, tennish]
How to count number of element in vector
Size() Method return number of element in the vector.
import java.util.Vector;
public class CountElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector is = "+vector);
/*
* Returns the number of components in this vector.
*/
System.out.println("size = "+vector.size());
}
}
Output:
Vector is = [cricket, hockey, football, tennish]
size = 4
import java.util.Vector;
public class CountElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector is = "+vector);
/*
* Returns the number of components in this vector.
*/
System.out.println("size = "+vector.size());
}
}
Output:
Vector is = [cricket, hockey, football, tennish]
size = 4
How to shuffle elements in Vector
import java.util.Collections;
import java.util.Vector;
public class Shuffle {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
/*
* Randomly permutes the specified list using a
* default source of randomness.
*/
Collections.shuffle(vector);
System.out.println("Vector after first Shuffle = "+vector);
Collections.shuffle(vector);
System.out.println("Vector after second Shuffle = "+vector);
}
}
Output:
Vector = [cricket, hockey, football, tennish]
Vector after first Shuffle = [tennish, cricket, football, hockey]
Vector after second Shuffle = [cricket, hockey, tennish, football]
How to find does Vector contains elements or not
we use contains() and Returns true if this list contains the specified
element. More formally, returns true if and only if this list contains
at least one element e such that (o==null ? e==null : o.equals(e)).
import java.util.Vector;
public class ContailsElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
System.out.println(vector.contains("cricket"));
System.out.println(vector.contains("apple"));
}
}
Output:
Vector = [cricket, hockey, football, tennish]
true
false
import java.util.Vector;
public class ContailsElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
System.out.println(vector.contains("cricket"));
System.out.println(vector.contains("apple"));
}
}
Output:
Vector = [cricket, hockey, football, tennish]
true
false
How to get element of vector
import java.util.Vector;
public class GetElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
/*
* Returns the element at the specified
* position in this Vector.
*/
System.out.println("element at index 2 is = "+vector.get(2));
}
}
Output:
Vector = [cricket, hockey, football, tennish]
element at index 2 is = football
How to get sub list from Vector
we can use sublist(fromindex,toindex) for get sublist from a vector.
import java.util.List;
import java.util.Vector;
public class GetSubList {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
vector.add("java");
// print vector
System.out.println("Vector = "+vector);
/*
* Returns a view of the portion of this
* List between fromIndex, inclusive, and
* toIndex, exclusive. (If fromIndex and toIndex are equal,
* the returned List is empty.) T
*/
List<String> list = vector.subList(1,3);
System.out.println("list is ="+list);
}
}
Output:
Vector = [cricket, hockey, football, tennish, java]
list is =[hockey, football]
import java.util.List;
import java.util.Vector;
public class GetSubList {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
vector.add("java");
// print vector
System.out.println("Vector = "+vector);
/*
* Returns a view of the portion of this
* List between fromIndex, inclusive, and
* toIndex, exclusive. (If fromIndex and toIndex are equal,
* the returned List is empty.) T
*/
List<String> list = vector.subList(1,3);
System.out.println("list is ="+list);
}
}
Output:
Vector = [cricket, hockey, football, tennish, java]
list is =[hockey, football]
How to Remove List From Vector
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class RemoveList {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
List<String> list = new ArrayList<String>();
list.add("hockey");
list.add("tennish");
list.add("apple");
list.add("java");
System.out.println("list is = "+list);
/*
* Removes from this Vector all of its
* elements that are contained in the
* specified Collection.
*/
vector.removeAll(list);
System.out.println("Vector after remove list = "+vector);
}
}
Output:
Vector = [cricket, hockey, football, tennish]
list is = [hockey, tennish, apple, java]
Vector after remove list = [cricket, football]
import java.util.List;
import java.util.Vector;
public class RemoveList {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
List<String> list = new ArrayList<String>();
list.add("hockey");
list.add("tennish");
list.add("apple");
list.add("java");
System.out.println("list is = "+list);
/*
* Removes from this Vector all of its
* elements that are contained in the
* specified Collection.
*/
vector.removeAll(list);
System.out.println("Vector after remove list = "+vector);
}
}
Output:
Vector = [cricket, hockey, football, tennish]
list is = [hockey, tennish, apple, java]
Vector after remove list = [cricket, football]
How To Remove Element of Vector
import java.util.Vector;
public class RemoveElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
/*
* Removes the element at the specified position
* in this Vector. Shifts any subsequent elements
* to the left (subtracts one from their indices).
* Returns the element that was removed from the
* Vector.
*/
vector.remove(1);
System.out.println("After remove element vector is = "+vector);
}
}
Output:
Vector = [cricket, hockey, football, tennish]
After remove element vector is = [cricket, football, tennish]
public class RemoveElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
/*
* Removes the element at the specified position
* in this Vector. Shifts any subsequent elements
* to the left (subtracts one from their indices).
* Returns the element that was removed from the
* Vector.
*/
vector.remove(1);
System.out.println("After remove element vector is = "+vector);
}
}
Output:
Vector = [cricket, hockey, football, tennish]
After remove element vector is = [cricket, football, tennish]
How to remove all element from vector
We can remove all element of vector using clear() method . clear() removes all element from the list and return empty list.
import java.util.Vector;
public class RemoveAll {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector is = "+vector);
/*
* Removes all of the elements from this
* Vector. The Vector will be empty after
* this call returns (unless it throws an
* exception).
*/
vector.clear();
System.out.println("Vector After Clear is = "+vector);
}
}
Output:
Vector is = [cricket, hockey, football, tennish]
Vector After Clear is = []
import java.util.Vector;
public class RemoveAll {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector is = "+vector);
/*
* Removes all of the elements from this
* Vector. The Vector will be empty after
* this call returns (unless it throws an
* exception).
*/
vector.clear();
System.out.println("Vector After Clear is = "+vector);
}
}
Output:
Vector is = [cricket, hockey, football, tennish]
Vector After Clear is = []
How to swap elements of vector
import java.util.Collections;
import java.util.Vector;
public class SwapElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Before swap Vector = "+vector);
/*
* Swaps the elements at the specified positions
* in the specified list. (If the specified
* positions are equal, invoking this method
* leaves the list unchanged.)
*/
Collections.swap(vector, 1, 3);
System.out.println("After swap vector = "+vector);
}
}
Output:
Before swap Vector = [cricket, hockey, football, tennish]
After swap vector = [cricket, tennish, football, hockey]
import java.util.Vector;
public class SwapElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Before swap Vector = "+vector);
/*
* Swaps the elements at the specified positions
* in the specified list. (If the specified
* positions are equal, invoking this method
* leaves the list unchanged.)
*/
Collections.swap(vector, 1, 3);
System.out.println("After swap vector = "+vector);
}
}
Output:
Before swap Vector = [cricket, hockey, football, tennish]
After swap vector = [cricket, tennish, football, hockey]
How to add all elements of a list to Vector
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class AddAllElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
List<String> list = new ArrayList<>();
list.add("java");
list.add("c++");
list.add("php");
System.out.println("list = "+list);
/*
* Appends all of the elements in the specified
* Collection to the end of this Vector, in the
* order that they are returned by the specified
* Collection's Iterator.
*/
vector.addAll(list);
System.out.println("vector after add list ="+vector);
}
}
Output:
Vector = [cricket, hockey, football, tennish]
list = [java, c++, php]
vector after add list =[cricket, hockey, football, tennish, java, c++, php]
import java.util.List;
import java.util.Vector;
public class AddAllElement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
List<String> list = new ArrayList<>();
list.add("java");
list.add("c++");
list.add("php");
System.out.println("list = "+list);
/*
* Appends all of the elements in the specified
* Collection to the end of this Vector, in the
* order that they are returned by the specified
* Collection's Iterator.
*/
vector.addAll(list);
System.out.println("vector after add list ="+vector);
}
}
Output:
Vector = [cricket, hockey, football, tennish]
list = [java, c++, php]
vector after add list =[cricket, hockey, football, tennish, java, c++, php]
Reverse order of all elements of Vector
import java.util.Collections;
import java.util.Vector;
public class ReverseAllelement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Before reverse Vector = "+vector);
/*
* Reverses the order of the elements in the
* specified list. This method runs in linear time.
*/
Collections.reverse(vector);
System.out.println("After reverse Vector = "+vector);
}
}
Output:
Before reverse Vector = [cricket, hockey, football, tennish]
After reverse Vector = [tennish, football, hockey, cricket]
import java.util.Vector;
public class ReverseAllelement {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Before reverse Vector = "+vector);
/*
* Reverses the order of the elements in the
* specified list. This method runs in linear time.
*/
Collections.reverse(vector);
System.out.println("After reverse Vector = "+vector);
}
}
Output:
Before reverse Vector = [cricket, hockey, football, tennish]
After reverse Vector = [tennish, football, hockey, cricket]
Thursday, 21 May 2015
How To Shallow Copy Or Clone a Vector
import java.util.Vector;
public class CloneOfVector {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
/*
* Returns a clone of this vector. The copy will
* contain a reference to a clone of the internal
* data array, not a reference to the original internal
* data array of this Vector object.
*/
Vector<String> myvector = (Vector<String>) vector.clone();
for (String str : myvector) {
System.out.println("element is = "+str);
}
}
}
public class CloneOfVector {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
/*
* Returns a clone of this vector. The copy will
* contain a reference to a clone of the internal
* data array, not a reference to the original internal
* data array of this Vector object.
*/
Vector<String> myvector = (Vector<String>) vector.clone();
for (String str : myvector) {
System.out.println("element is = "+str);
}
}
}
How To Copy Vector To Array
import java.util.Vector;
public class VectorToArray {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
String [] arr = new String[vector.size()];
//Returns an array containing all of the
//elements in this Vector in the correct order.
vector.toArray(arr);
System.out.println("Array is :");
for (int i = 0; i < arr.length; i++) {
System.out.println("Element is = "+arr[i]);
}
}
}
Output:
Vector = [cricket, hockey, football, tennish]
Array is :
Element is = cricket
Element is = hockey
Element is = football
Element is = tennish
public class VectorToArray {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
String [] arr = new String[vector.size()];
//Returns an array containing all of the
//elements in this Vector in the correct order.
vector.toArray(arr);
System.out.println("Array is :");
for (int i = 0; i < arr.length; i++) {
System.out.println("Element is = "+arr[i]);
}
}
}
Output:
Vector = [cricket, hockey, football, tennish]
Array is :
Element is = cricket
Element is = hockey
Element is = football
Element is = tennish
Vector Iterator Example
import java.util.Iterator;
import java.util.Vector;
public class IterateVector {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
//Returns an iterator over the elements in
//this list in proper sequence.
Iterator<String> itr = vector.iterator();
while (itr.hasNext()) {
System.out.println("element is = "+itr.next());
}
}
}
Output:
element is = cricket
element is = hockey
element is = football
element is = tennish
import java.util.Vector;
public class IterateVector {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
//Returns an iterator over the elements in
//this list in proper sequence.
Iterator<String> itr = vector.iterator();
while (itr.hasNext()) {
System.out.println("element is = "+itr.next());
}
}
}
Output:
element is = cricket
element is = hockey
element is = football
element is = tennish
How to storing Java Object In Vector
import java.util.Vector;
public class VectorOfObject {
public static void main(String[] args) {
Vector<User> vector = new Vector<User>();
vector.add(new User("shivam",100));
vector.add(new User("raja",200));
vector.add(new User("mukesh",300));
vector.add(new User("piyush",500));
// print vector
for (User user : vector) {
System.out.println("user name="+user.getName()+" User id="+user.getUserId());
}
}
}
class User{
private String name;
private int userId;
public User(String name, int userId) {
super();
this.name = name;
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
}
Output:
user name=shivam User id=100
user name=raja User id=200
user name=mukesh User id=300
user name=piyush User id=500
public class VectorOfObject {
public static void main(String[] args) {
Vector<User> vector = new Vector<User>();
vector.add(new User("shivam",100));
vector.add(new User("raja",200));
vector.add(new User("mukesh",300));
vector.add(new User("piyush",500));
// print vector
for (User user : vector) {
System.out.println("user name="+user.getName()+" User id="+user.getUserId());
}
}
}
class User{
private String name;
private int userId;
public User(String name, int userId) {
super();
this.name = name;
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
}
Output:
user name=shivam User id=100
user name=raja User id=200
user name=mukesh User id=300
user name=piyush User id=500
How to create Vector In java
import java.util.Vector;
public class CreateVector {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
}
}
Output:
Vector = [cricket, hockey, football, tennish]
public class CreateVector {
public static void main(String[] args) {
Vector<String> vector = new Vector<String>();
//add element in vector
vector.add("cricket");
vector.add("hockey");
vector.add("football");
vector.add("tennish");
// print vector
System.out.println("Vector = "+vector);
}
}
Output:
Vector = [cricket, hockey, football, tennish]
Wednesday, 20 May 2015
Container request filter in Jersey
Request filter is executed before the resource method is run and before the
response is created. The filter has possibility to manipulate the request parameters including request
headers or entity.
package com.javaproficiency.jerseydemo.demo;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@Path("/user")
public class UserHander {
@POST
public Response addUser(@HeaderParam("token") String token) {
System.out.println("token=" + token);
return Response.status(Status.OK).entity(token).build();
}
}
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;
@Provider
public class RequestFilter implements ContainerRequestFilter{
public void filter(ContainerRequestContext requestContext) throws IOException {
System.out.println("Filter token="+requestContext.getHeaderString("token"));
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>JerseyDemo</display-name>
<servlet>
<servlet-name>RestServices</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.javaproficiency.jerseydemo.demo;com.javaproficiency.jerseydemo.filter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestServices</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>InstagramPost</groupId>
<artifactId>InstagramPost</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>com.craterzone.instagrampost</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.13</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.13</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<scope>provided</scope>
<version>2.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-servlet</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-simple-http</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-servlet</artifactId>
<version>2.16</version>
</dependency>
</dependencies>
</project>
UserHandler.java
package com.javaproficiency.jerseydemo.demo;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
@Path("/user")
public class UserHander {
@POST
public Response addUser(@HeaderParam("token") String token) {
System.out.println("token=" + token);
return Response.status(Status.OK).entity(token).build();
}
}
RequestFilter.java
package com.javaproficiency.jerseydemo.filter;import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;
@Provider
public class RequestFilter implements ContainerRequestFilter{
public void filter(ContainerRequestContext requestContext) throws IOException {
System.out.println("Filter token="+requestContext.getHeaderString("token"));
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>JerseyDemo</display-name>
<servlet>
<servlet-name>RestServices</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.javaproficiency.jerseydemo.demo;com.javaproficiency.jerseydemo.filter</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestServices</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>InstagramPost</groupId>
<artifactId>InstagramPost</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>com.craterzone.instagrampost</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.13</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.13</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<scope>provided</scope>
<version>2.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-servlet</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-simple-http</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-servlet</artifactId>
<version>2.16</version>
</dependency>
</dependencies>
</project>
Project Structure :
use rest console with
Url:
http://localhost:8080/JerseyDemo/rest/user
Method :
Post
And header param
token = java
Monday, 18 May 2015
How to use retailAll in LinkedList
Retains only the elements in this list that are contained in the
specified collection. In other words, removes from this list all
of its elements that are not contained in the specified collection.
import java.util.LinkedList;
import java.util.List;
public class RetainAll {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Mumbai");
linkedList.add("Delhi");
linkedList.add("Noida");
linkedList.add("Gao");
linkedList.add("Patna");
//print linkedlist
System.out.println(" Linklist is = "+linkedList);
List<String> brr = new LinkedList<String>();
brr.add("Delhi");
brr.add("KKR");
/*
* Retains only the elements in linkedlist
* collection that are contained in the brr
*/
linkedList.retainAll(brr);
System.out.println("After retainAll linkedlist is= "+linkedList);
}
}
Output:
Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]
After retainAll linkedlist is= [Delhi]
import java.util.LinkedList;
import java.util.List;
public class RetainAll {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Mumbai");
linkedList.add("Delhi");
linkedList.add("Noida");
linkedList.add("Gao");
linkedList.add("Patna");
//print linkedlist
System.out.println(" Linklist is = "+linkedList);
List<String> brr = new LinkedList<String>();
brr.add("Delhi");
brr.add("KKR");
/*
* Retains only the elements in linkedlist
* collection that are contained in the brr
*/
linkedList.retainAll(brr);
System.out.println("After retainAll linkedlist is= "+linkedList);
}
}
Output:
Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]
After retainAll linkedlist is= [Delhi]
How to insert element in LinkedList
we can insert element in Linkedlist at a given index.for this we use add(int index,element).
import java.util.LinkedList;
public class AddElement {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Mumbai");
linkedList.add("Delhi");
linkedList.add("Noida");
linkedList.add("Gao");
linkedList.add("Patna");
//print linkedlist
System.out.println(" Linklist is = "+linkedList);
/*
* Inserts the specified element at the specified position
in this list. Shifts the element currently at that position
(if any) and any subsequent elements to the right
*/
linkedList.add(2, "bangalore");
System.out.println("After Insert Element Linkedlist is = "+linkedList);
}
}
Output:
Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]
After Insert Element Linkedlist is = [Mumbai, Delhi, bangalore, Noida, Gao, Patna]
import java.util.LinkedList;
public class AddElement {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Mumbai");
linkedList.add("Delhi");
linkedList.add("Noida");
linkedList.add("Gao");
linkedList.add("Patna");
//print linkedlist
System.out.println(" Linklist is = "+linkedList);
/*
* Inserts the specified element at the specified position
in this list. Shifts the element currently at that position
(if any) and any subsequent elements to the right
*/
linkedList.add(2, "bangalore");
System.out.println("After Insert Element Linkedlist is = "+linkedList);
}
}
Output:
Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]
After Insert Element Linkedlist is = [Mumbai, Delhi, bangalore, Noida, Gao, Patna]
How to replace element in LinkedList
We can replace any specific index element by other element using set(int index,element) method.
import java.util.LinkedList;
public class ReplaceElement {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Mumbai");
linkedList.add("Delhi");
linkedList.add("Noida");
linkedList.add("Gao");
linkedList.add("Patna");
//print linkedlist
System.out.println("Before Replace Element Linklist is = "+linkedList);
/*
* Replaces the element at the specified position in
* this list with the specified element.
*/
linkedList.set(2,"chandigarh");
System.out.println("After Replace Element Linklist is = "+linkedList);
}
}
Output:
Before Replace Element Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]
After Replace Element Linklist is = [Mumbai, Delhi, chandigarh, Gao, Patna]
import java.util.LinkedList;
public class ReplaceElement {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Mumbai");
linkedList.add("Delhi");
linkedList.add("Noida");
linkedList.add("Gao");
linkedList.add("Patna");
//print linkedlist
System.out.println("Before Replace Element Linklist is = "+linkedList);
/*
* Replaces the element at the specified position in
* this list with the specified element.
*/
linkedList.set(2,"chandigarh");
System.out.println("After Replace Element Linklist is = "+linkedList);
}
}
Output:
Before Replace Element Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]
After Replace Element Linklist is = [Mumbai, Delhi, chandigarh, Gao, Patna]
How to count number of element in LinkedList
Size() Method return number of element in the LinkdeList .
import java.util.LinkedList;
public class CountElement {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Mumbai");
linkedList.add("Delhi");
linkedList.add("Noida");
linkedList.add("Gao");
linkedList.add("Patna");
//print linkedlist
System.out.println("Before Remove Element Linklist is = "+linkedList);
//Returns the number of elements in this list.
System.out.println("number of element in linkedlist = "+linkedList.size());
}
}
Output:
Before Remove Element Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]
number of element in linkedlist = 5
import java.util.LinkedList;
public class CountElement {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Mumbai");
linkedList.add("Delhi");
linkedList.add("Noida");
linkedList.add("Gao");
linkedList.add("Patna");
//print linkedlist
System.out.println("Before Remove Element Linklist is = "+linkedList);
//Returns the number of elements in this list.
System.out.println("number of element in linkedlist = "+linkedList.size());
}
}
Output:
Before Remove Element Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]
number of element in linkedlist = 5
How to shuffle elements in LinkedList
import java.util.Collections;
import java.util.LinkedList;
public class Shuffle {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Mumbai");
linkedList.add("Delhi");
linkedList.add("Noida");
linkedList.add("Gao");
linkedList.add("Patna");
//print linkedlist
System.out.println("Linklist is = "+linkedList);
//Randomly permutes the specified list using a default
//source of randomness.
Collections.shuffle(linkedList);
for (String str : linkedList) {
System.out.println(str);
}
Collections.shuffle(linkedList);
System.out.println("After Second Shuffle");
for (String str : linkedList) {
System.out.println(str);
}
}
}
Output:
Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]
Gao
Noida
Mumbai
Patna
Delhi
After Second Shuffle
Noida
Patna
Gao
Mumbai
Delhi
import java.util.LinkedList;
public class Shuffle {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Mumbai");
linkedList.add("Delhi");
linkedList.add("Noida");
linkedList.add("Gao");
linkedList.add("Patna");
//print linkedlist
System.out.println("Linklist is = "+linkedList);
//Randomly permutes the specified list using a default
//source of randomness.
Collections.shuffle(linkedList);
for (String str : linkedList) {
System.out.println(str);
}
Collections.shuffle(linkedList);
System.out.println("After Second Shuffle");
for (String str : linkedList) {
System.out.println(str);
}
}
}
Output:
Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]
Gao
Noida
Mumbai
Patna
Delhi
After Second Shuffle
Noida
Patna
Gao
Mumbai
Delhi
How to find does LinkedList contains elements or not
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
import java.util.LinkedList;
public class ContailsElement {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Mumbai");
linkedList.add("Delhi");
linkedList.add("Noida");
linkedList.add("Gao");
linkedList.add("Patna");
//print linkedlist
System.out.println(" Linklist is = "+linkedList);
//Returns true if this list contains the specified element
System.out.println(linkedList.contains("Delhi"));
System.out.println(linkedList.contains("panjab"));
}
}
Output:
Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]
true
false
import java.util.LinkedList;
public class ContailsElement {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Mumbai");
linkedList.add("Delhi");
linkedList.add("Noida");
linkedList.add("Gao");
linkedList.add("Patna");
//print linkedlist
System.out.println(" Linklist is = "+linkedList);
//Returns true if this list contains the specified element
System.out.println(linkedList.contains("Delhi"));
System.out.println(linkedList.contains("panjab"));
}
}
Output:
Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]
true
false
How to get element of LinkedList
We use method get(int index) for get any element at specific index.
import java.util.LinkedList;
public class GetElement {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Mumbai");
linkedList.add("Delhi");
linkedList.add("Noida");
linkedList.add("Gao");
linkedList.add("Patna");
//print linkedlist
System.out.println("Before Remove Element Linklist is = "+linkedList);
System.out.println("Element At Index 2 ="+linkedList.get(2));
}
}
Output:
Before Remove Element Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]
Element At Index 2 =Noida
import java.util.LinkedList;
public class GetElement {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("Mumbai");
linkedList.add("Delhi");
linkedList.add("Noida");
linkedList.add("Gao");
linkedList.add("Patna");
//print linkedlist
System.out.println("Before Remove Element Linklist is = "+linkedList);
System.out.println("Element At Index 2 ="+linkedList.get(2));
}
}
Output:
Before Remove Element Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]
Element At Index 2 =Noida
Subscribe to:
Posts (Atom)