Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Create Java Priority Queue to ignore duplicates

import java.util.HashSet;
import java.util.PriorityQueue;
public class Demo {
	public static void main(String[] args) {
		HashSet<Integer>set = new HashSet<>();
		set.add(100);
		set.add(150);
		set.add(250);
		set.add(300);
		set.add(250);
		set.add(500);
		set.add(600);
		set.add(500);
		set.add(900);
		PriorityQueue<Integer>queue = new PriorityQueue<>(set);
		System.out.println("Elements with no duplicates = "+queue);
	}
}

Create a read-only list in Java

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Demo {
	public static void main(String args[]) throws Exception {
		List<String>list = new ArrayList<String>();
		list.add("A");
		list.add("B");
		list.add("C");
		list = Collections.unmodifiableList(list);
		// An exception is thrown since its a read-only list now
		list.add("D");
		list.add("E");
		list.add("F");
		System.out.println(list);
	}
}

Java Program to create a new list with values from existing list

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Demo {
	public static void main(String args[]) {
		List<Employee>emp = Arrays.asList(new Employee("Jack", 29, "South"), new Employee("Tom", 24, "North"), new Employee("Harry", 35, "West"),new Employee("Katie", 32, "East"));
		List<String>res =  emp.stream().map(u ->u.displayEmpName()).collect(Collectors.toList());
		System.out.println("Employee Names = "+res);
	}
}
class Employee {
	private String emp_name;
	private int emp_age;
	private String emp_zone;
	public Employee(String emp_name, int emp_age, String emp_zone) {
		this.emp_name = emp_name;
		this.emp_age = emp_age;
		this.emp_zone = emp_zone;
	}
	public String displayEmpName() {
		return this.emp_name;
	}
}

Java Program to create a new list

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
public class Demo {
	public static void main(String args[]) {
		List < Employee > emp = Arrays.asList(new Employee("Jack", 29, "South"), new Employee("Tom", 24, "North"), new Employee("Harry", 35, "West"), new Employee("Katie", 32, "East"));
		List < String > res = processElements(emp, p -> p.displayEmpName());
		System.out.println("Employee Names + " + res);
	}
	public static < X, Y > List < Y > processElements(Iterable < X > src,
	Function < X, Y > mapper) {
		List < Y > list = new ArrayList <> ();
		for (X p: src)
		list.add(mapper.apply(p));
		return list;
	}
}
class Employee {
	private String emp_name;
	private int emp_age;
	private String emp_zone;
	public Employee(String emp_name, int emp_age, String emp_zone) {
		this.emp_name = emp_name;
		this.emp_age = emp_age;
		this.emp_zone = emp_zone;
	}
	public String displayEmpName() {
		return this.emp_name;
	}
}

Create an unmodifiable map in Java

import java.util.*;
public class Demo {
	public static void main(String[] s) {
		Hashtable<String,String>hash = new Hashtable<String,String>();
		hash.put("key1", "value1");
		hash.put("key2", "value2");
		hash.put("key3", "value3");
		System.out.println("Initial collection: "+hash);
		Map map= Collections.unmodifiableMap(hash);
		// error will generate
		map.put("key3", "value3");
	}
}

Java Program to copy value from one list to another list

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class Demo {
	public static void main(String[] args) {
		String[] str = { "P", "Q", "R", "S", "T", "U", "V", "W" };
		int len = str.length;
		List<String>list1 = new ArrayList<String>();
		for (int i = 0; i<len; i++)
			list1.add(str[i]);
			List<String>list2 = new ArrayList<String>();
		for (int i = 0; i<len; i++)
			list2.add("");
			Collections.copy(list2,list1);
			ListIterator<String>iterator = list2.listIterator();
			System.out.println("New List...");
		while (iterator.hasNext())
			System.out.println(iterator.next());
	}
}

Java Program to add and remove elements in set

import java.util.LinkedHashSet;
public class Demo {
	public static void main(String[] args) {
		LinkedHashSet<Integer>set = new LinkedHashSet<Integer>();
		set.add(20);
		set.add(60);
		set.add(80);
		set.add(120);
		set.add(150);
		set.add(200);
		set.add(220);
		set.add(260);
		set.add(380);
		System.out.println("Set = "+set);
		set.remove(150);
		set.remove(260);
		System.out.println("Updated Set = "+set);
		set.remove(60);
		System.out.println("Updated Set = "+set);
	}
}

Create a Queue from LinkedList in Java

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Demo {
	public static void main(String[] args) {
		Queue<String>queue = new LinkedList<String>();
		queue.add("P");
		queue.add("Q");
		queue.add("R");
		queue.add("S");
		queue.add("T");
		queue.add("U");
		queue.add("V");
		List<String>list = new ArrayList<String>(queue);
		for (Object ob: list)
		 System.out.println(ob);
	}
}

Java Program to convert Properties list into a Map

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Demo {
	public static void main(String args[]) {
		Properties p = new Properties();
		p.setProperty("P", "1");
		p.setProperty("Q", "2");
		p.setProperty("R", "3");
		p.setProperty("S", "4");
		p.setProperty("T", "5");
		p.setProperty("U", "6");
		p.setProperty("V", "7");
		HashMap<String, String>map = new HashMap<String, String>((Map) p);
		Set<Map.Entry<String, String>>set =  map.entrySet();
		System.out.println("Key and Value of the Map... ");
		for (Map.Entry<String, String>m: set) {
			System.out.print(m.getKey() + ": ");
			System.out.println(m.getValue());
		}
	}
}

Java Program to get Tail Map from TreeMap

import java.util.SortedMap;
import java.util.TreeMap;
public class Demo {
	public static void main(String[] args) {
		TreeMap<String, String>tMap = new TreeMap<String,String>();
		tMap.put("1", "A");
		tMap.put("2", "B");
		tMap.put("3", "C");
		tMap.put("4", "D");
		tMap.put("5", "E");
		tMap.put("6", "F");
		tMap.put("7", "G");
		tMap.put("8", "H");
		tMap.put("9", "I");
		tMap.put("10", "J");
		SortedMap map = tMap.tailMap("2");
		System.out.println("Sub Map = " + map);
		map = tMap.tailMap("8");
		System.out.println("Sub Map = " + map);
		map = tMap.tailMap("1");
		System.out.println("Sub Map = " + map);
	}
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.