Archive for category UTILITY

java 7 comparator,condition Check

package list;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class MyClass {

public static void main(String[] args) {

List people = Arrays.asList(new Person(“Remzi”, “Altan”, 23), new Person(“Ali”, “Tuzen”, 23),
new Person(“Funda”, “Tekin”, 24), new Person(“Ceyda”, “Ozbay”, 25));

Collections.sort(people, new Comparator() {

@Override
public int compare(Person ob1, Person ob2) {

return ob1.getName().compareTo(ob2.getName());
}

});

printCond(people, new Condition() {

@Override
public boolean match(Person p) {

return p.getLastname().startsWith(“T”);
}
});

}

private static void printCond(List people, Condition condition) {
for (Person p : people) {
if (condition.match(p)) {
System.out.println(p.getName() + ” ” + p.getLastname() + ” ” + p.getAge());
}

}

}

}

interface Condition {
boolean match(Person p);
}

Advertisements

4 Comments

create directory and file

/**
* Create directory and file if not exists
*/
public File createDirectory() throws IOException {
File file = new File(“C:\\report”);
if (!file.exists()) {
file.mkdir();
}

file = new File(“C:\\report\\report.xls”);
if (!file.exists()) {
file.createNewFile();
}
return file;
}

 

Leave a comment

remove duplicate from java list

For object comparison  override equal and hashcode

such as

User.java
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BaseEntity other = (BaseEntity) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}

List updatedMultListWithoutDuplicate = new ArrayList<>(new HashSet<>(duplicatedMultList));

http://www.baeldung.com/java-remove-duplicates-from-list

https://www.dotnetperls.com/duplicates-java

 

,

Leave a comment