使用Java 8 lambdas,有效创建新的List<T>给定的List<K>密钥和"a "的"最佳"方法是Map<K,V>什么?在这种情况下,您将获得一些List可能的Map键,并且应该生成一个List<T>where T,这是基于Vmap值类型的某些方面构造的某种类型.
我已经探索了一些并且感到不舒服声称一种方式比另一种方式更好(可能有一个例外 - 参见代码).我将澄清"最佳"作为代码清晰度和运行时效率的组合.这些是我想出来的.我相信有人可以做得更好,这是这个问题的一个方面.我不喜欢filter大多数方面,因为它意味着需要创建中间结构和多次传递名称List.现在,我选择了例6 - 一个简单的'ol循环.(注意:代码注释中有一些神秘的想法,特别是"需要外部引用......"这意味着从lambda外部.)
public class Java8Mapping {
private final Map<String,Wongo> nameToWongoMap = new HashMap<>();
public Java8Mapping(){
List<String> names = Arrays.asList("abbey","normal","hans","delbrook");
List<String> types = Arrays.asList("crazy","boring","shocking","dead");
for(int i=0; i<names.size(); i++){
nameToWongoMap.put(names.get(i),new Wongo(names.get(i),types.get(i)));
}
}
public static void main(String[] args) {
System.out.println("in main");
Java8Mapping j = new Java8Mapping();
List<String> testNames = Arrays.asList("abbey", "froderick","igor");
System.out.println(j.getBongosExample1(testNames).stream().map(Bongo::toString).collect(Collectors.joining(", "))); …Run Code Online (Sandbox Code Playgroud) 我创建了一个将zip文件存档合并到一个存档中的实用程序.在这样做时,我最初有以下方法(请参阅此问题以获取一些背景信息ExceptionWrapper):
private void addFile(File f, final ZipOutputStream out, final Set<String> entryNames){
ZipFile source = getZipFileFromFile(f);
source.stream().forEach(ExceptionWrapper.wrapConsumer(e -> addEntryContent(out, source, e, entryNames)));
}
Run Code Online (Sandbox Code Playgroud)
下面是代码ExceptionWrapper.wrapConsumer和ConsumerWrapper
public static <T> Consumer<T> wrapConsumer(ConsumerWrapper<T> consumer){
return t -> {
try {
consumer.accept(t);
} catch (Exception e) {
throw new IllegalStateException(e);
}
};
}
public interface ConsumerWrapper<T>{
void accept(T t) throws Exception;
}
Run Code Online (Sandbox Code Playgroud)
这会导致编译错误:
Error:(128, 62) java: incompatible types: java.util.function.Consumer<capture#1 of ? extends java.util.zip.ZipEntry> cannot be converted to java.util.function.Consumer<? super …
我有一个程序,我不明白它的结果.它给了我110,但我不知道它怎么可能.我只称它一次.对我来说,它应该是3 ?? 谢谢
public class Test {
public static String operator (int n) {
return((n == 0) ? "" : operator(n / 2) + (n % 2));
}
public static void main(String[] args) {
System.out.println(operator(6));
}
}
Run Code Online (Sandbox Code Playgroud) 给定文件
\n\n橙色\n紫色\n靛蓝\n粉色\n\n
myWay为什么下面代码中的方法不给我ByteBuffervia的内容Charset.decode?请注意,我验证了 是否ByteBuffer具有文件内容,但似乎无论我在内部使用什么方法myWay,我都无法让生成的文件CharBuffer具有内容。该otherWay方法按预期工作。有谁知道发生了什么事?我已经阅读了ByteBuffer和CharBuffer的 javdoc ,但没有真正看到任何解释这一点的内容(或者我只是错过了它。)使用FileChannel.readvsFileChannel.map如果我可以显示缓冲区的内容会有什么区别read?
public class Junk {\n private static final int BUFFER_SIZE = 127;\n private static final String CHARSET = "UTF-8";\n\n public static void main(String[] args) {\n try {\n String fileName = "two.txt";\n myWay(fileName);\n otherWay(fileName);\n } catch (IOException e) {\n throw new IllegalStateException(e);\n }\n }\n\n private static void myWay(String fileName) throws …Run Code Online (Sandbox Code Playgroud) 我添加了一个while循环,以便在用户输入无效值时,代码将重新提示用户输入有效值.但是当用户输入无效值时,代码进入无限循环.任何帮助将非常感激 !!
public static void main(String[] args) {
System.out.println("Usage : enter a, b, & c from a quadratic equation");
System.out.println(" aX^2 +bX + c = 0");
System.out.println(" Result will be 2 values for X");
Scanner in = new Scanner(System.in);
double a = 0;
double b = 0;
double c = 0;
double x1 = 0 ;
double x2 = 0;
double discriminant = 0;
System.out.println("Please enter values for a , b, and c ");
while(true){
try
{
a = in.nextDouble(); …Run Code Online (Sandbox Code Playgroud) OS = Mac OS X.Shell
是bash.
我正在尝试验证在shell脚本中创建目录.但是,我似乎没有得到返回代码.这是shell代码:
#!/bin/bash
rv=$(mkdir lib)
echo "The code = $rv !"
if [ "$rv" == "0" ]
then
echo "created the dir"
else
echo "no can do, compadre."
fi
Run Code Online (Sandbox Code Playgroud)
并输出:
mkdir: lib: File exists
The code = !
no can do, compadre.
Run Code Online (Sandbox Code Playgroud)
无论是否创建目录,else条件始终为true,因为$rv变量永远不会为"0".但它似乎永远不是任何整数.手册页说
DIAGNOSTICS
The mkdir utility exits 0 on success, and >0 if an error occurs.
Run Code Online (Sandbox Code Playgroud)
Google搜索未成功.任何帮助表示赞赏.