Header
Group Services: Technology Consulting
phone +91-9999-283-283/9540-283-283
email info@sisoft.in

Java Collections Example

List of Java Collections Example

1. List & Set Examples (ArrayList, LinkedList, TreeSet, HashSet)

2. Map Examples (HashMap)

3. Comparable and Comparator Example

List & Set Examples (ArrayList, LinkedList, TreeSet, HashSet)

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.TreeSet;

public class ExampleArrayList {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ExampleArrayList eal = new ExampleArrayList();
		eal.ex_TreeSet();
		System.out.println("another");
		eal.ex_HashSet();
	}

void ex_ArrayList()
{
	ArrayList alf = new ArrayList();
	alf.add(3.5f);
	alf.add(4.5f);
	alf.add(5.5f);
	System.out.println(alf);
	System.out.println("Using For Each:");
	for (Float f1: alf)
	{
		System.out.println(f1);
	}
	alf.add(23.4f);
	System.out.println("Using Iterator:");
	Iterator it = alf.iterator();
	while (it.hasNext())
	{
		System.out.println(it.next());
	}
	for (int i=0 ; i< alf.size(); i++){
		System.out.println(alf.get(i));
	}
	
}
void ex_LinkedList()
{
	LinkedList alf = new LinkedList();
	alf.add(3.5f);
	alf.add(4.5f);
	alf.add(5.5f);
	System.out.println(alf);
	System.out.println("Using For Each:");
	for (Float f1: alf)
	{
		System.out.println(f1);
	}
	alf.add(23.4f);
	alf.add(5.5f);
	System.out.println("Using Iterator:");
	ListIterator it = alf.listIterator();
	while (it.hasNext())
	{
		System.out.println(it.next());
	}
	for (int i=0 ; i< alf.size(); i++){
		System.out.println(alf.get(i));
	}
	
}

void ex_HashSet()
{
	HashSet alf = new HashSet();
	alf.add(35.5f);
	alf.add(49.f);
	alf.add(3.5f);
	alf.add(4.5f);
	alf.add(5.5f);
	System.out.println(alf);
	System.out.println("Using For Each:");
	for (Float f1: alf)
	{
		System.out.println(f1);
	}
	alf.add(23.4f);
	alf.add(5.5f);
	System.out.println("Using Iterator:");
	Iterator it = alf.iterator();
	while (it.hasNext())
	{
		System.out.println(it.next());
	}
	
}

void ex_TreeSet()
{
	TreeSet alf = new TreeSet();
	alf.add(35.5f);
	alf.add(49.f);

	alf.add(3.5f);
	alf.add(4.5f);
	alf.add(5.5f);
	System.out.println(alf);
	System.out.println("Using For Each:");
	for (Float f1: alf)
	{
		System.out.println(f1);
	}
	alf.add(23.4f);
	alf.add(5.5f);
	System.out.println("Using Iterator:");
	Iterator it = alf.iterator();
	while (it.hasNext())
	{
		System.out.println(it.next());
	}
	
}

	
}

Map Examples (HashMap)


import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ExampleMap {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashMap students = new HashMap<>();
		students.put("Name", "Vijay");
		students.put("Class", "B.Tech");
		students.put("DOB", "23-06-1975");
	
		System.out.println("Size of Map:"+students.size());
		Set keyInfo = students.keySet();
		
		for (String x: keyInfo)
		{
			String val=students.get(x);
			System.out.println(x+":"+val);
			if (val.equals("Vijay")){
				students.put(x, "Vikas");
			}
		}
		
		Set> entry = students.entrySet();
		
		for(Map.Entry y: entry){
			String key = y.getKey();
			String val = y.getValue();
			System.out.println(key+":"+val);
		}
		}
		
		
		
	}

Comparable and Comparator Example



import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeSet;

public class SortMethods {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TreeSet al_st = new TreeSet(new StudentHeight());
//		ArrayList al_st = new ArrayList();

		Student s1 = new Student(1,"Jitesh", 12,5.9f);
		al_st.add(s1);
		Student s2 = new Student(2,"Amit", 11,5.7f);
		al_st.add(s2);
		Student s3 = new Student(3,"Rohan", 10,5.8f);
		al_st.add(s3);
		for(Student s4: al_st)
		{
			System.out.println(s4.roll_no+","+s4.name+","+s4.height);
			
		}
		
		
	}

}

class Student implements Comparable{
	int roll_no;
	String name ;
	int std ;
	float height ;

	Student(int r1, String n1, int s1, float h1){
		this.roll_no = r1 ;
		this.name = n1 ;
		this.std = s1 ;
		this.height = h1 ;
	}

	@Override
	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		Student sx = (Student) o ;
		return this.name.compareTo(sx.name);
//		return (s1.roll_no - this.roll_no);
	}
	
	
}

class StudentHeight implements Comparator{

	@Override
	public int compare(Student o1, Student o2) {
		// TODO Auto-generated method stub
		if ((o1.height-o2.height)>0){
			return 1 ;
		}
		else if ((o1.height-o2.height)<0){
			return -1 ;
		}
		else {
			return 0 ;
		}
		
//		return (int) (o1.height-o2.height);
	}
	
}