Java – Remove Item from Collection

It is tricky to remove items from a list while within a loop, this is due to the fact that the index and length of the list get changed.
It is a good idea to delete an item from List using Iterator interface remove() method. The same goal can be achieved using removeIf() option

 

package com.pwn.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class RmvItemFrmCollectoin {

	public static void main(String[] args) {
		List<String> cities = new ArrayList<String>();
		cities.add("San Francisco");
		cities.add("New York");
		cities.add("San Diego");
		System.out.println("Before deleting List " + cities);
		Iterator<String> citiesIterator = cities.iterator();
		while (citiesIterator.hasNext()) {
			String city = citiesIterator.next();

			if ("San Diego".equals(city)) {
				citiesIterator.remove();
			}
		}

		System.out.println("After deleting List " + cities);
		cities = new ArrayList<String>();
		cities.add("San Francisco");
		cities.add("New York");
		cities.add("San Diego");

		cities.removeIf(p -> "San Diego".equals(p));

		System.out.println("After deleting List " + cities);

	}

}
Before deleting List [San Francisco, New York, San Diego]
After deleting List [San Francisco, New York]
After deleting List [San Francisco, New York]

Java – Recursive Folder reading

  

package com.pwn.nio;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;

public class NIORecrusiveFolderReading {
	public static void main(String args[]) {

		Path dirPath = new File("C://install").toPath();

		try {
			Files.walkFileTree(dirPath,
					EnumSet.noneOf(FileVisitOption.class), 1,
					new SimpleFileVisitor() {
						@Override
						public FileVisitResult preVisitDirectory(
								Path selectedPath,
								BasicFileAttributes attrs)
								throws IOException {
							System.out.println("d "
									+ selectedPath.toAbsolutePath());
							return FileVisitResult.CONTINUE;
						}

						@Override
						public FileVisitResult visitFile(
								Path selectedPath,
								BasicFileAttributes attrs)
								throws IOException {
							System.out.println("f "
									+ selectedPath.toAbsolutePath());
							return FileVisitResult.CONTINUE;
						}
					});
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}
d C:\install
f C:\install\AnypointStudio
f C:\install\Apache Tomcat 8.0.27
f C:\install\apache-maven-3.5.2
f C:\install\eclipse
f C:\install\glassfish-4.1.1
f C:\install\instructions.txt
f C:\install\NetBeans 8.2
f C:\install\spring-tool-suite-3.9.1
f C:\install\sqldeveloper
f C:\install\user.xml
f C:\install\userDetail.xml
f C:\install\workspace

Java – read files using try-with-resources

The try-with-resources statement is a try statement that declares one or more resources.   A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement.
Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

 
package com.pwn.nio;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class NIOReadFile {

	public static void main(String[] args) {
		String file = "c://install//instructions.txt";
		try (Stream stream = Files.lines(Paths.get(file))) {

			stream.forEach(System.out::println);

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

This is sample Instructions File