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

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);
}
}

enhance for loop in java

enhance for loop or foreach loop :

In java 5 enhance for() loop intoduced. It is used to iterate array elements in java.

foreach() explanation :

A foreach style loop is designed to iterate a collection, which store objects like array that strictly sequential and from start to end.

Advantage :

With foreach loop there is no need of new keyword and no pre-existing code is broken.
It is also known as anhance for loop.

Syntax :


for(type itr_var_name : collection){
//statement block
}

Where type can be String, Object or whatever. itr_var_name can be valid varible name that will recieve elements from collection. Collection can be array.

Example :

 


package com.pkjavacode.com

public class ForeachExample {

 public static void main(String[] args) {
 String arr[] = {"pradeep", "vijay", "john", "martin"};
 System.out.println("Employee list are :");
 for (String emp : arr) {
 System.out.println(emp);
 }
 }
}

Output :
Employee list are :

pradeep
vijay
john
martin

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]

HashMap in collection

HashMap :

HashMap class uses a hashtable to implement Map interface.HashMap using put() to store key/value pair.

Example :

 


package com.pkjavacode.com;

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

public class HashMapExample {

public static void main(String[] args) {
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "Pradeep");
map.put(2, "Bharat");
map.put(3, "Amit");
map.put(4, "Rahul");
Iterator itr = map.entrySet().iterator();
while (itr.hasNext()) {
Map.Entry<Integer, String> me = (Map.Entry<Integer, String>) itr
.next();
System.out.print("" + me.getKey());
System.out.println(" " + me.getValue());
}

}

}

 

Output :

1 Pradeep
2 Bharat
3 Amit
4 Rahul

Dependency Injection via set collection property

Dependency Injection via set collection property is simple just object injection through set.

Employee.java


package com.pkjavacode.com;

public class Employee {
String name;

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

public void getEmployeeDetails() {
System.out.println(name);
}

}

 

EmployeeTest.java


package com.pkjavacode.com;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class EmployeeTest {

/**
* @param args
*/
public static void main(String[] args) {

BeanFactory b = new XmlBeanFactory(new ClassPathResource("beans.xml"));

Object o = b.getBean("emp");
Employee e = (Employee) o;
e.getEmployeeDetails();
}

}

beans.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="emp" class="com.pkjavacode.com.Employee">
<property name="name">
<set value-type="String">
<value>Pradeep</value>
<value>Varun</value>
<value>Amit</value>
<value>Monty</value>
<value>Raj</value>
</set>
</property>
</bean>
</beans>

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]