package my.com;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class Paths {
public static void main(String[] args) {
String str = "A1, A1\\BB1\\CCC1, A2\\BB1, A2\\BB1\\CCC1";
ArrayList<String> paths = new ArrayList<String>(Arrays.asList(str.split(",")));
System.out.println("====== Беган =======");
System.out.println("Было");
for(String i:paths){
System.out.println(i);
}
Set<String> out = new TreeSet<String>(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o2.compareTo(o1);
}
});
for(String i : paths) {
ArrayList<String> path = new ArrayList<String>(Arrays.asList(i.trim().split("\\\\")));
for(int j = 1; j <= path.size(); j++) {
List<String> s = path.subList(0, j);
out.add(String.join("\\",s));
}
}
System.out.println("=================");
System.out.println("Сортировка туда");
for(String i:out) {
System.out.println(i);
}
System.out.println("=================");
System.out.println("Сортировка обратно");
Set<String> out1 = new TreeSet<String>();
out1.addAll(out);
for(String i:out1) {
System.out.println(i);
}
System.out.println("======= Доне ======");
}
}
====== Беган =======
Было
A1
A1\BB1\CCC1
A2\BB1
A2\BB1\CCC1
=================
Сортировка туда
A2\BB1\CCC1
A2\BB1
A2
A1\BB1\CCC1
A1\BB1
A1
=================
Сортировка обратно
A1
A1\BB1
A1\BB1\CCC1
A2
A2\BB1
A2\BB1\CCC1
======= Доне ======
$ java org.apache.derby.tools.ij
ij> connect 'jdbc:derby:MyDbTest';
The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
List<String> names = ....
Iterator<String> i = names.iterator();
while (i.hasNext()) {
String s = i.next(); // must be called before you can call i.remove()
// Do something
i.remove();
}
package jtests;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
public class MyTest3 {
public static void main(String[] args) {
DateTimeFormatter fmt = new DateTimeFormatterBuilder()
.appendPattern("dd.MM.yyyy. HH:mm:ss")
.toFormatter();
LocalDateTime t1 = LocalDateTime.parse("01.01.2016. 00:00:00", fmt);
LocalDateTime t2 = LocalDateTime.parse("01.01.2013. 00:00:00", fmt);
System.out.println(t1);
System.out.println(t2);
System.out.println(t1.isAfter(t2));
System.out.println(t2.isAfter(t1));
}
}
Господа, что-то в разум не возьму... Всегда сам считал, что Exception - зло, а тут решил проверить. Я понимаю, что JVM может чего и оптимизнула...
IF execution time: 1231
EXCEPT execution time: 1260
package jtests;
import java.util.HashMap;
public class MyTest2 {
String testIf(HashMap<String, String> h, int i) {
String r = "None";
if(h.containsKey("NONEKEY")) {
r = h.get("NONEKEY");
}
return r;
}
String testExept(HashMap<String, String> h, int i) {
String r = "None";
try {
r = h.get("NONEKEY");
} catch(Exception e) {
return e.getMessage();
}
return r;
}
public static void main(String[] args) {
MyTest2 m = new MyTest2();
HashMap<String, String> h = new HashMap<String, String>();
h.put("KEY", "VALUE");
long startTime = System.currentTimeMillis();
for (int i = 0; i < 900000000; i++) {
m.testIf(h,i);
}
long endTime = System.currentTimeMillis();
System.out.println("IF execution time: " + (endTime - startTime) );
startTime = System.currentTimeMillis();
for (int i = 0; i < 900000000; i++) {
m.testExept(h,i);
}
endTime = System.currentTimeMillis();
System.out.println("EXCEPT execution time: " + (endTime - startTime) );
}
}