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

Advertisements

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]