Skip to content

Instantly share code, notes, and snippets.

@BacLuc
Created May 12, 2017 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BacLuc/8ef6fabc107429a4fd2777b2875cd504 to your computer and use it in GitHub Desktop.
Save BacLuc/8ef6fabc107429a4fd2777b2875cd504 to your computer and use it in GitHub Desktop.
test remove elements with various methods
package listtest;
/**
* Created by lucius on 12.05.17.
*/
public class Language {
private String name;
public Language(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(Object otherlanguage){
if(otherlanguage instanceof Language){
return ((Language)otherlanguage).getName().equals(getName());
}
return false;
}
public int hashCode(){
return getClass().getName().concat(getName()).hashCode();
}
public String toString(){
return getName();
}
}
package listtest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringJoiner;
import java.util.stream.Collectors;
/**
* Created by lucius on 12.05.17.
*/
public class LanguageList {
private ArrayList<Language> languageList;
public LanguageList() {
this.languageList = new ArrayList<>();
}
public boolean add(String name) {
return languageList.add(new Language(name));
}
/**
* does not work with more than 4 elements
* @param name
*/
public void remove(String name){
Language language = new Language(name);
for(Language lang : languageList){
if(lang.equals(language)){
languageList.remove(lang);
}
}
}
public void removeWithIterator(String name){
Language language = new Language(name);
int size = languageList.size();
Iterator<Language> iterator= languageList.iterator();
while(iterator.hasNext()){
Language lang = iterator.next();
if(lang.equals(language)){
iterator.remove();
}
}
}
public void removeWithContains(String name){
Language language = new Language(name);
while(languageList.contains(language)){
languageList.remove(language);
}
}
public void removeWithFor(String name){
Language language = new Language(name);
for(int i = 0; i<languageList.size();i++){
if(language.equals(languageList.get(i))){
languageList.remove(i);
i--;
}
}
}
public void removeWithFilter(String name){
Language language = new Language(name);
languageList = languageList.stream()
.filter(language1 -> !language.equals(language1))
.collect(Collectors.toCollection(ArrayList::new));
}
public void removeWithParallelFilter(String name){
Language language = new Language(name);
languageList = languageList.parallelStream()
.filter(language1 -> !language.equals(language1))
.collect(Collectors.toCollection(ArrayList::new));
}
public String toString(){
StringJoiner joiner = new StringJoiner(",");
languageList.forEach(language -> joiner.add(language.getName()));
return "["+joiner.toString()+"]";
}
public int size(){
return languageList.size();
}
}
package listtest;
import java.util.ConcurrentModificationException;
/**
* Created by lucius on 12.05.17.
*/
public class ListTestMain {
public static int repetitions = 10000;
private static String[] languages = new String[]{
"Java", "Ruby", "Ruby", "C++", "C#", "Javascript", "Go", "Groovy", "PHP", "Ruby", "ObjectiveC", "Perl", "Python", "Scala", "R", "Fritz", "Hans"
};
public static void main(String[] args){
long starttime;
long totaltime;
LanguageList list = new LanguageList();
list.add("Java");
list.add("PHP");
list.add("Ruby");
list.add("Pascal");
// list.add("test");
System.out.println("First test: "+ list.toString());
list.remove("Ruby");
System.out.println("Second test: "+ list.toString());
try {
list = new LanguageList();
list.add("Java");
list.add("PHP");
list.add("Ruby");
list.add("Pascal");
list.add("test");
System.out.println("First test: "+ list.toString());
list.remove("Ruby");
System.out.println("Second test: "+ list.toString());
} catch (ConcurrentModificationException e) {
e.printStackTrace();
}
for (int listSize = 0; listSize<5000; listSize+=100) {
System.out.println("-----------------------------------------------------------------------");
System.out.println(String.format("Starting with Listsize: %s",listSize));
totaltime = 0;
for(int i=0;i<repetitions;i++) {
//first test foreach
list = getFilledList(listSize);
starttime = System.currentTimeMillis();
list.removeWithFor("Ruby");
totaltime += System.currentTimeMillis()-starttime;
}
System.out.println(String.format("Test with removeWithFor remove took: %s",totaltime));
totaltime = 0;
for(int i=0;i<repetitions;i++) {
//first test foreach
list = getFilledList(listSize);
starttime = System.currentTimeMillis();
list.removeWithIterator("Ruby");
totaltime += System.currentTimeMillis()-starttime;
}
System.out.println(String.format("Test with removeWithIterator took: %s",totaltime));
totaltime = 0;
for(int i=0;i<repetitions;i++) {
//first test foreach
list = getFilledList(listSize);
if(list.size()>languages.length*10){
System.out.println("Skipping contains test, because takes too long");
break;
}
starttime = System.currentTimeMillis();
list.removeWithContains("Ruby");
totaltime += System.currentTimeMillis()-starttime;
}
System.out.println(String.format("Test with normal removeWithContains took: %s",totaltime));
totaltime = 0;
for(int i=0;i<repetitions;i++) {
//first test foreach
list = getFilledList(listSize);
starttime = System.currentTimeMillis();
list.removeWithFilter("Ruby");
totaltime += System.currentTimeMillis()-starttime;
}
System.out.println(String.format("Test with normal removeWithFilter took: %s",totaltime));
totaltime = 0;
for(int i=0;i<repetitions;i++) {
//first test foreach
list = getFilledList(listSize);
starttime = System.currentTimeMillis();
list.removeWithParallelFilter("Ruby");
totaltime += System.currentTimeMillis()-starttime;
}
System.out.println(String.format("Test with normal removeWithParallelFIlter took: %s",totaltime));
System.out.println("-----------------------------------------------------------------------");
System.out.println("");
System.out.println("");
System.out.println("");
}
}
public static LanguageList getFilledList(int listSize){
LanguageList list = new LanguageList();
int iterationsForListSize = (int)Math.ceil(
((float)listSize)
/
languages.length
);
if(listSize == 0){
return list;
}
for(int i=0; i<iterationsForListSize;i++){
for(String name : languages){
list.add(name);
}
}
return list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment