假设如下:
String example = "something";
String firstLetter = "";
Run Code Online (Sandbox Code Playgroud)
是否存在差异,需要注意以下firstLetter可能影响绩效的分配方式; 哪个最好,为什么?
firstLetter = String.valueOf(example.charAt(0));
firstLetter = Character.toString(example.charAt(0));
firstLetter = example.substring(0, 1);
Run Code Online (Sandbox Code Playgroud)
第一个字母作为a返回的原因String是它在Hadoop中运行,并且需要分配给Text类型的字符串,firstLetter将作为a key从map()方法输出,例如:
public class FirstLetterMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
String line = new String();
Text firstLetter = new Text();
IntWritable wordLength = new IntWritable();
@Override
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
line = value.toString();
for (String word : line.split("\\W+")){
if …Run Code Online (Sandbox Code Playgroud) 我收到了这个错误
Exception in thread "main" java.lang.NoClassDefFoundError: javafx/application/Ap
plication
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javafx.application.Application
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 13 more
Run Code Online (Sandbox Code Playgroud)
尝试运行我的类文件时,这是源
import javafx.application.Application;
import javafx.fxml.FXMLLoader; …Run Code Online (Sandbox Code Playgroud) 我使用eclipse Juno作为Java IDE,在我的java代码中,注释和日志声明都是日语.
eclipse显示垃圾字符而不是正确的日文字符.我按照 这篇文章来解决问题并在eclipse.ini文件中进行了更改.但现在我得到字符串文字没有被双引号错误正确关闭.
我知道如果使用迭代器在某个线程遍历它时将更改Collection,则iterator.next()将抛出ConcurrentModificationException.
但它根据列表中的元素数量显示不同的行为.
我尝试了一个代码片段,在其中遍历for-each循环中的列表,在它之间,遍历使用列表的remove()方法从列表中删除了一个元素.
理想情况下,它应该在此条件下抛出ConcurrentModificationException而不依赖于列表中的元素数量,但是当列表中的元素数量为2时,它不是真的.
案例1: 列表中的元素数量 - 1
public static void main(String[] args)
{
List<String> list=new ArrayList<String>();
list.add("One");
for (String string : list)
{
System.out.println(string);
list.remove(string);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:一
线程"main"java.util.ConcurrentModificationException中的异常
这是预期的.
案例2:列表中的元素数量 - 2
public static void main(String[] args)
{
List<String> list=new ArrayList<String>();
list.add("One");
list.add("two");
for (String string : list)
{
System.out.println(string);
list.remove(string);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:一
没有例外被抛出?????????
案例3:列表中的元素数量 - 3
public static void main(String[] args)
{
List<String> list=new ArrayList<String>();
list.add("One");
list.add("Two");
list.add("Three");
for (String string …Run Code Online (Sandbox Code Playgroud) 我使用ExecutorService在不同的线程中运行许多任务.有时,在线程池中等待的Runnable实例太多可能会导致Out of Memory问题.
我尝试编写一个阻塞作业执行器来解决它.这有什么官方解决方案吗?
例如:
BlockingJobExecutor executor = new BlockingJobExecutor(3);
for (int i = 0; i < 1000; i++) {
executor.addJob(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
LogFactory.getLog(BTest.class).info("test " + System.currentTimeMillis());
}
});
}
executor.shutdown();
Run Code Online (Sandbox Code Playgroud)
这是BlockingJobExecutor类:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class BlockingJobExecutor {
AtomicInteger counter = new AtomicInteger();
ExecutorService service;
int threads;
public BlockingJobExecutor(int threads) {
if (threads < 1) {
throw new IllegalArgumentException("threads …Run Code Online (Sandbox Code Playgroud) 我需要在unix/linux OS中锁定一个文件.我用谷歌搜索并阅读java.nio.channels Filelock和Reentrant File Lock.但两者都适用于Windows,但不适用于unix.有没有其他方法在unix中实现锁定文件?任何示例代码都将受到高度赞赏.
谢谢,Vignesh
尝试使用以下内容添加到List的前面,但抛出添加.该医生说它应该改变.修复或解决方法是什么?
List<String> whatever = Arrays.asList("Blah1", "Blah2");
whatever.add(0, "BlahAll"); // <- Throws
Run Code Online (Sandbox Code Playgroud) 您好我试图用ArrayList测试assertEquals().这是我的测试代码的一部分:
ArrayList<String> n = new ArrayList<String>();
n.add("a");
n.add("b");
n.add("c");
assertEquals(n, "[a, b, c]");
Run Code Online (Sandbox Code Playgroud)
对我来说看起来完全一样,但junit说
junit.framework.AssertionFailedError: expected:<[a, b, c]> but was:<[a, b, c]>
Run Code Online (Sandbox Code Playgroud)
谁能指出我做错了什么?
我想在每两个字符后分割一个字符串.
例如:
String aString= "abcdef"
Run Code Online (Sandbox Code Playgroud)
分手后我想要 "ab cd ef"
我该怎么做?
谢谢 :)
如何在java中插入多个标签字符串?
例如:getName()+'\ t'+'\ t'+ getLastName()"不起作用.请帮助!!
我正在尝试用方法一进行初始化:
Map<String, String> mapInter = Collections.EMPTY_MAP;
mapInter = new HashMap<String, String>();
mapInter.put("one", "one");
System.out.println(mapInter.hashCode());
Run Code Online (Sandbox Code Playgroud)
方法二:
HashMap<String, String> myMap = new HashMap<String, String>(10);
myMap.put("key", "value");
System.out.println(myMap.hashCode());
Run Code Online (Sandbox Code Playgroud)
在第一种方法中,当我打印哈希码时,它打印零,但在第二种方法中,它打印哈希码。初始化后将返回hashcode。
为什么第一个案例中的 HashCode 打印为零,而第二个案例中则不是?
你如何使用string.trim()方法只在最后修剪白色空格?
我的意思是不影响正面的空间
ex:输入:"这是linkedin"o/p:"这是linkedin"
"集合","集合"和"集合"之间的区别
在java.util包中,我们将使用这三个关键字,因此这些关键字之间有什么区别.1)收集2)收集3)收藏.
collection:它是word表示Collection Object和Map对象.
集合:它是所有集合的超级接口,即以阵列格式.
集合:它是类,包含操作Collection Object的Utility方法.
java ×13
string ×5
collections ×2
list ×2
.net ×1
c#-4.0 ×1
eclipse ×1
exception ×1
file-locking ×1
iterator ×1
javafx ×1
junit ×1
linux ×1
performance ×1