在过去的6-7个小时里,我一直在努力想弄清楚我的Apache Tomcat服务器出了什么问题.在我所有的项目中,该jdk版本得到了切换到1.6从1.8.
为了解决这个问题version conflict,我从这里获得了帮助,验证了Tomcat版本的编译版本和JVM是否相同.
对于具有相同路径错误的多个上下文,我知道我需要删除重复的Context标签server.xml.所以,首先我检查Servers项目(当你在Eclipse中添加Apache Tomcat Server时自己创建)并在那里找不到.所以,我深入研究了目录(Eclipse WorkSpace)metadata\.plugins\org.eclipse.wst.server.core\tmp3\conf并在那里删除了重复的Context标签,并从这里获取了帮助.
现在,当我创建一个新的动态Web项目时,一切正常,我能够看到网页.但是,如果我尝试访问那些jdk发生变化的旧项目的任何HTML,XHTML文件,这会org.apache.catalina.LifecycleException继续弹出并且服务器无法启动,因此这些重复的Context标记会一次又一次地重复建立时间我启动服务器.
我的旧Web应用程序项目现在似乎都没有用.
java.util.concurrent.ExecutionException:org.apache.catalina.LifecycleException:无法启动组件[StandardEngine [Catalina] .StandardHost [localhost] .StandardContext [/ EdBurns_ChrisSchalk]] .....严重:子容器在启动时失败java.util.concurrent.ExecutionException:org.apache.catalina.LifecycleException:无法启动组件[StandardEngine [Catalina] .StandardHost [localhost]] ....引起:java.lang.RuntimeException:注释中的意外元素值种类:0
如何防止这些上下文标记在server.xml中反复建立.其次,如何将这些旧的Web项目再次恢复到工作状态?
给定一个文件,我们可以使用例如,将其转换为字符串流
Stream<String> lines = Files.lines(Paths.get("input.txt"))
Run Code Online (Sandbox Code Playgroud)
我们能否以类似的方式从标准输入构建一条线流?
给定一个java.util.List与n元素和所需的页面大小m,我想将它转换为包含地图n/m+n%m的元素.每个地图元素都应包含m元素.
这是一个整数的例子:
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// What is the equivalent Java 8 code to create the map below from my list?
Map<Integer, List<Integer>> map = new HashMap<>();
map.put(0, Arrays.asList(1,2,3));
map.put(1, Arrays.asList(4,5,6));
map.put(2, Arrays.asList(7,8,9));
map.put(3, Arrays.asList(10));
Run Code Online (Sandbox Code Playgroud)
这是可能的,使用Java 8?
我将通过java 8中的lambda表达式
当我改变线程的代码它工作正常
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("run");
}
}).start();
Run Code Online (Sandbox Code Playgroud)
转换为lambda表达式为
new Thread(
() -> System.out.println("Hello from thread")
).start();
Run Code Online (Sandbox Code Playgroud)
但我无法转换FilenameFilter表达式
File file = new File("/home/text/xyz.txt");
file.list(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
name.endsWith(".txt");
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
并且未成功转换为此
file.list(new FilenameFilter () {
(File a1, String a2) -> {
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
它像日食一样给出错误
此行有多个标记
- 语法错误,插入";" 完成Statement
- 语法错误,插入"}"完成Block
- 语法错误,插入"AssignmentOperator Expression"完成赋值
要按升序对其进行排序,我可以使用:
myMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
Run Code Online (Sandbox Code Playgroud)
我怎么能按降序排列呢?
假设我有一个Collection,和一个Predicate匹配我想从中移除的元素Collection.但我不只是想丢弃它们,我想将匹配的元素移动到一个新的集合中.我会做一些像这样在Java 7中:
List<E> removed = new LinkedList<>();
for (Iterator<E> i = data.iterator(); i.hasNext();) {
E e = i.next();
if (predicate.test(e)) {
removed.add(e);
i.remove();
}
}
Run Code Online (Sandbox Code Playgroud)
我很好奇是否有流/ Java 8方法可以做到这一点. Collections.removeIf()不幸的是简单地返回一个boolean(甚至没有删除元素的数量?太糟糕了.)我设想这样的东西(虽然当然.removeAndYield(Predicate)不存在):
List<E> removed = data.removeAndYield(predicate).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
注意:这个问题的灵感来自类似的问题 ; 这个问题是关于从集合中删除项目获取流的更一般情况.正如在链接问题中指出的那样,必要的解决方案可能更具可读性,但我很好奇是否甚至可以使用流.
编辑:显然,我们可以将任务分成两个单独的步骤,并假设适当的数据结构,它将是有效的.问题是可以在任意集合(可能没有效率.contains()等)上完成.
如果我使用JDK1.8_40或更新版本(Oracle或OpenJDK执行相同操作),以下代码和对话框调整大小将使应用程序崩溃(尝试过Windows 7,x64,64位JDK)
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JDialog dialog = new JDialog();
dialog.add(new JPanel());
dialog.setVisible(true);
dialog.setBounds(100, 100, 100, 100);
final JWindow dependentWindow = getjWindow(dialog);
dependentWindow.setVisible(true);
dependentWindow.setBounds(100, 100, 100, 100);
Timer t = new Timer(300, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dependentWindow.setVisible(!dependentWindow.isVisible());
}
});
t.start();
}
});
} …Run Code Online (Sandbox Code Playgroud) 我有以下代码片段,我想知道是否以及如何用Streams/Java 8 API替换它
for (State state : states) {
for (City city : cities) {
if (state.containsPoint(city.getLocation())) {
System.out.printf("%30s is part of %-30s\n",
city.getName(), state.getName());
}
}
}
Run Code Online (Sandbox Code Playgroud) 如何很好地将包含一个或零个元素的列表转换为Optional?
丑陋的代码:
List<Integer> integers = new ArrayList<>();
Optional<Integer> optional = integers.size() == 0 ?
Optional.empty() :
Optional.of(integers.get(0));
Run Code Online (Sandbox Code Playgroud) 为了这个例子,让我们假设我有一个Tuple带有两个属性的简单类型:
interface Tuple<T, U> {
T getFirst();
U getSecond();
}
Run Code Online (Sandbox Code Playgroud)
现在我想将一个(first, second)元组集合转换为一个映射,该映射将每个first值映射到second具有该特定first值的元组中包含的所有值的集合.该方法groupSecondByFirst()显示了我想要的可能实现:
<T, U> Map<T, Set<U>> groupSecondByFirst(Set<Tuple<T, U>> tuples) {
Map<T, Set<U>> result = new HashMap<>();
for (Tuple<T, U> i : tuples) {
result.computeIfAbsent(i.getFirst(), x -> new HashSet<>()).add(i.getSecond());
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
如果输入是[(1, "one"), (1, "eins"), (1, "uno"), (2, "two"), (3, "three")]输出的话{ 1 = ["one", "eins", "uno"], 2 = ["two"], 3 = ["three"] }
我想知道是否以及如何使用流框架实现这一点.我得到的最好的是以下表达式,它返回一个包含完整元组作为值的映射,而不仅仅是它们的 …