vector collection in java


package com.pkjavacode.com;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

public class VectorExample {
public static void main(String args[]) {
List<String> list = new Vector<String>();
list.add("Pradeep");
list.add("Ajit");
list.add("Vijay");
Iterator<String> itr = list.iterator();
while (itr.hasNext()) {
Object element = (Object) itr.next();
System.out.println("" + element);
}
}
}

Output :
Vector elements are :
Pradeep
Ajit
Vijay

Criteria API in Hibernate

Criteria API in Hibernate :

The criteria is a simple api for fetching entities by creating Criterion object. With criteria you can apply number of condition with query such as using like wildcard, order by etc.

Here you will see create criteria operation through annotation  in Hibernate. To create criteria you have to create following java class.

  • Employee.java
  • HibernateTest.java

Employee.java :


package com.pkjavacode.com;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author pradeep
*/
@Entity
@Table(name = "employee")
public class Employee implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "salary")
private Integer salary;

public Employee() {
}

public Employee(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getSalary() {
return salary;
}

public void setSalary(Integer salary) {
this.salary = salary;
}
}

HibernateTest.java :


package com.pkjavacode.com;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
*
* @author pradeep
*/
public class HibernateTest {

public static void main(String a[]) {

Session s = null;
try {
s = new AnnotationConfiguration().configure().buildSessionFactory().openSession();
Transaction t = s.beginTransaction();

List employees=s.createCriteria(Employee.class).add(Restrictions.like("name", "p%")).addOrder(Order.asc("salary")).list();
t.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
s.close();
}
}
}

convert map to list in java


import java.util.*;

public class MapToList{

public static void main(String args[])
{
Map<Integer, String> map=new HashMap<Integer, String>();
map.put(1, "India");
map.put(2, "Pak");
map.put(3, "China");
map.put(4, "UsA");

List<Integer> list=new ArrayList<Integer>(map.keySet());
System.out.println("Key of map as a lis is :"+list);
List<String> list1=new ArrayList<String>(map.values());
System.out.println("value of map as a list is :"+list1);
}
}

HashMap with single key and multiple value using List

HashMap with single key and multiple value using List : To create such type of hashmap we will have to first create list add values in this list and after that put this list into map.

Example :


package com.pkjavacode.com;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class HashMapExample {

public static void main(String[] args) {
Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
List<String> list = new ArrayList<String>();
list.add("Pradeep");
list.add("Bharat");
list.add("Amit");

List<String> list2 = new ArrayList<String>();
list2.add("Rahul");
list2.add("Saurabh");
list2.add("Sonu");

List<String> list3 = new ArrayList<String>();
list3.add("Mortin");
list3.add("Petric");
list3.add("John");

map.put(1, list);
map.put(2, list2);
map.put(3, list3);
// map.put(4, "Rahul");
Iterator itr = map.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry<Integer, List<String>> me = (Map.Entry<Integer, List<String>>) itr
.next();
System.out.print("" + me.getKey());
System.out.println(" " + me.getValue());
}

}

}

 

Output :

 

1 [Pradeep, Bharat, Amit]
2 [Rahul, Saurabh, Sonu]
3 [Mortin, Petric, John]

Convert array to list


package com.pkjavacode.com;

import java.util.Arrays;
import java.util.List;

public class ListTOArrayExample {
public static void main(String args[]) {
String arr[] = { "Pradeep", "Vinay", "John", "Monty" };
System.out.println("Array elements are :");
for (String element : arr)
System.out.print(" " + element);
System.out.println();
System.out.println("List elements are :");
List<String> list = Arrays.asList(arr);
System.out.print(list);
}
}

 

Output :

 

Array elements are :
Pradeep Vinay John Monty
List elements are :
[Pradeep, Vinay, John, Monty]

Convert List to array

Convert List to Array is very simple just call toArray() method of List.

Example


package com.pkjavacode.com;

import java.util.List;
import java.util.ArrayList;

public class ListTOArrayExample {
 public static void main(String args[]) {
 // create a list
 List<Integer> list = new ArrayList<Integer>();
 list.add(2);
 list.add(5);
 list.add(3);
 list.add(7);
 list.add(9);
 list.add(1);
 System.out.println("List elements are :" + list);

 //create an object array call toArray() of List

 Object arr[] = list.toArray();
 System.out.print("Array elements are :");
 for (Object element : arr)
 System.out.print(" "+element);
 }
}

Convert list to array in java

import java.util.*;
import java.io.*;
class ListConversion
{
public static void main(String args[])
{
List<String> li=new ArrayList<String>();
li.add(“New Delhi”);
li.add(“Varanasi”);
li.add(“Allahabad”);
li.add(“Lucknow”);
li.add(“Noida”);
li.add(“Mumbai”);
System.out.println(“Element of ArrayList is :”+li);
Object arr[]=li.toArray();
System.out.println(“Element of array is :”);
for(Object s : arr)
{
System.out.println(s);

}
}
}