Tutorialspoint

Remove duplicate elements in Java with HashSet

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
public class Demo {
	public static void main(String[] argv) {
		List<Integer>list1 = new ArrayList<Integer>();
		list1.add(100);
		list1.add(200);
		list1.add(300);
		list1.add(400);
		list1.add(400);
		list1.add(500);
		list1.add(600);
		list1.add(600);
		list1.add(700);
		list1.add(400);
		list1.add(500);
		HashSet<Integer>set = new HashSet<Integer>(list1);
		List<Integer>list2 = new ArrayList<Integer>(set);
		System.out.println("List after removing duplicate elements:");
		for (Object ob: list2)
			System.out.println(ob);
	}
}

Java Program to convert this duration to the total length in milliseconds

import java.time.Duration;
public class Demo {
	public static void main(String[] args) {
		Duration d1 =  Duration.ofDays(20);
		Duration d2 =  Duration.ofHours(100);
		Duration d3 =  Duration.ofMinutes(150);
		System.out.println("Milliseconds in 20 days = "+d1.toMillis());
		System.out.println("Milliseconds in 100 hours = "+d2.toMillis());
		System.out.println("Milliseconds in 150 minutes = "+d3.toMillis());
	}
}

Java Program to change a file attribute to writable

import java.io.File;
public class Demo {
	public static void main(String[] args) throws Exception {
		File myFile = new File("input.txt");
		myFile.createNewFile();
		myFile.setReadOnly();
		if (myFile.canWrite()) {
			System.out.println("Writable!");
		} else {
			System.out.println("Read only mode!");
		}
		// set file to writable
		myFile.setWritable(true);
		if (myFile.canWrite()) {
			System.out.println("Writable!");
		} else {
			System.out.println("Read only mode!");
		}
	}
}

Java Program to remove an element from List with ListIterator

import java.util.ArrayList;
import java.util.ListIterator;
public class Demo {
	public static void main(String[] args) {
		ArrayList<String>arrList = new ArrayList<String>();
		arrList.add("Jack");
		arrList.add("Tom");
		arrList.add("Brad");
		arrList.add("Amy");
		arrList.add("Ben");
		arrList.add("Peter");
		arrList.add("Katie");
		arrList.add("Tim");
		System.out.println("List...");
		for (String str: arrList){
			System.out.println(str);
		}
		ListIterator<String>iterator = arrList.listIterator();
		iterator.next();
		iterator.remove();
		System.out.println("Updated List...");
		for (String str: arrList){
			System.out.println(str);
		}
	}
}

Java Program to sort a List in case insensitive order

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Demo {
	public static void main(String[] argv) throws Exception {
		String[] arr = new String[] { "P", "W", "g", "K", "H", "t", "E" };
		List<String>list = Arrays.asList(arr);
		System.out.println("List = "+list);
		Collections.sort(list);
		System.out.println("Case Sensitive Sort = "+list);
		Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
		System.out.println("Case Insensitive Sort = "+list);
	}
}

Program to merge duplicates of a List with TreeSet

import java.util.ArrayList;
import java.util.TreeSet;
import java.util.List;
public class Demo {
	public static void main(String[] args) {
		List<String>ls = new ArrayList<String>();
		ls.add("A");
		ls.add("B");
		ls.add("C");
		ls.add("D");
		ls.add("D");
		ls.add("E");
		ls.add("F");
		ls.add("G");
		ls.add("E");
		TreeSet<String>set = new TreeSet<String>();
		set.addAll(ls);
		System.out.println("TreeSet = "+set);
	}
}

Fill multiple copies of specified Object to a List in Java

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Demo {
	public static void main(String[] args) {
		List<Integer>list = Collections.nCopies(10, 100);
		Iterator<Integer>iterator = list.iterator();
		System.out.println("Displaying 100 multiple times...");
		while (iterator.hasNext())
			System.out.println(iterator.next());
	}
}

Java Program to convert LocalDate to java.util.Date in UTC

import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Date;
public class Demo {
    public static void main(String[] args) {
        LocalDate date  = LocalDate.now();
	    System.out.println("Date = "+date);
	    System.out.println("Date (UTC) = "+Date.from(date.atStartOfDay().toInstant(ZoneOffset.UTC)));
   }
}

Java Program to convert LocalDate to java.util.Date by timezone offset

import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.Date;
public class Demo {
    public static void main(String[] args) {
        LocalDate date = LocalDate.now();
	    System.out.println("Date = "+date);
	    ZoneOffset timeZone =  ZoneOffset.UTC;
	    System.out.println(Date.from(date.atStartOfDay().toInstant(timeZone)));
   }
}

Java Program to find keys from a Linked HashMap

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Demo {
	public static void main(String[] args) {
		Map<String, String>map = new LinkedHashMap<String, String>();
		map.put("1", "Katie");
		map.put("2", "Peter");
		map.put("3", "Amy");
		map.put("4", "Kane");
		map.put("5", "Colin");
		map.put("6", "Andre");
		map.put("7", "Pollard");
		map.put("8", "David");
		map.put("9", "Jofra");
		map.put("10", "Akila");
		List<String>list = new ArrayList<String>();
		list.addAll(map.keySet());
		System.out.println("Keys...");
		for (String str: list) {
		 System.out.println(str);
		}
	}
}

Previous 1 ... 4 5 6 7 8 9 10 ... 8401 Next
Advertisements
Loading...

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