小编pet*_*ter的帖子

避免Iterator ConcurrentModificationException的方法

据我所知,有两种方法可以避免ConcurrentModificationException,而一个线程迭代集合,另一个线程修改集合.

  1. 客户端锁定,基本上在迭代期间锁定集合.其他需要访问集合的线程将阻塞,直到迭代完成.
  2. "线程限制"克隆集合并迭代副本.

我想知道还有其他选择吗?因为第一种显而易见的方式是不受欢迎的,而且性能很差,如果集合很大,其他线程可能会等待很长时间.第二种方式我不确定,因为我们克隆集合,并迭代副本,所以如果其他线程进来并修改原始的,那么复制的一个变得陈旧吧?这是否意味着我们需要通过克隆重新开始并在修改后再次迭代它?

java concurrency multithreading iterator

2
推荐指数
1
解决办法
581
查看次数

Java哈希码反向计算

我正在尝试String通过添加一个新字符来测试哈希码的工作方式,然后进行相反的操作以将其减去,但是当我进行计算时,它没有给我正确的答案。

例如

int PRIME = 31;

//this will print 1687846330 
System.out.println("mlkjihgfedcb".hashCode());   

//this will print 783628775 which equals to "mlkjihgfedcba".hashCode();
System.out.println(("mlkjihgfedcb".hashCode() * PRIME + (int) 'a')); 

//this will print 25278344 which doesn't equals to "mlkjihgfedcb".hashCode()
System.out.println(("mlkjihgfedcba".hashCode() - (int) 'a') / PRIME); 
Run Code Online (Sandbox Code Playgroud)

我想知道我在上面的最后一步中做的数学正确吗?溢出对计算不重要吗?

java int hashcode string-hashing

2
推荐指数
2
解决办法
7852
查看次数

为什么 powerset 给出了 2^N 的时间复杂度?

下面是生成powerset的递归函数

void powerset(int[] items, int s, Stack<Integer> res) {
     System.out.println(res);

     for(int i = s; i < items.length; i++) {
          res.push(items[i]);
          powerset(items, s+1, res);
          res.pop();
     }
}
Run Code Online (Sandbox Code Playgroud)

我真的不明白为什么这需要O(2^N). 这是2从哪里来的?为什么T(N) = T(N-1) + T(N-2) + T(N-3) + .... + T(1) + T(0)解决为O(2^n). 有人可以解释为什么吗?

math recursion recurrence discrete-mathematics

2
推荐指数
2
解决办法
7324
查看次数

Java8 Stream作为一个整体映射到一个Function

我想知道是否有更好的方法来重写groupA方法与lambda作为链接操作的一部分?

public class Id {
    private final int value;
    public Id(int value) {
       this.value = value;
    }

    public int value() {
       return value;
    }
}

public class Ids implements Iterable<Id> { 
   private final List<Id> ids;

   private Ids(List<Id> ids) {
      this.ids = ids;
   }

   public static Ids of(List<Id> ids) {
      return new Ids(ids);
   }

   public Ids groupA() {
      return Ids.of(ids.stream()
                       .filter(id -> id.value() > 5)
                       .collect(Collectors.toList()));
   }

   @Override
   public Iterator<Id> iterator() {
      return ids.iterator();
   }
}
Run Code Online (Sandbox Code Playgroud)

基本上我想做点什么

ids.stream()
   .filter(id -> id …
Run Code Online (Sandbox Code Playgroud)

java lambda functional-programming java-8 java-stream

2
推荐指数
1
解决办法
92
查看次数

==使用枚举名称()和toString()

任何人都可以解释为什么toString()和name()引用相同的字符串?当我使用==将它们与字符串文字进行比较时,它们都会通过!枚举名如何与JVM中的String池一起使用?

static enum User
{
   BASIC, PREMIUM;
}

System.out.println("BASIC" == User.BASIC.toString()); // true
System.out.println("BASIC" == User.BASIC.name());     // true
Run Code Online (Sandbox Code Playgroud)

java string enums jvm string-pool

2
推荐指数
1
解决办法
697
查看次数

java Queue接口多态性

我正在阅读http://www.docjar.com/html/api/java/util/LinkedList.java.html

在Java中声明队列时

    Queue<Integer> queue = new LinkedList<Integer>();
Run Code Online (Sandbox Code Playgroud)

现场背后会发生什么?因为我看到队列是只有方法签名的接口,和LinkedList不直接实现它,所以它是如何覆盖这些方法(add(),peek(),poll(),offer(),和remove()),并做多态性这样呢?我的意思是,你只能访问一些特定的方法,但并非所有的人都从LinkedList例如public void add(int index, E element)不再可用,因为它使明显的类型队列中.还没有我们需要施展吗?

java queue polymorphism interface

1
推荐指数
1
解决办法
699
查看次数

带接口的匿名类

在处理匿名内部类时,我对接口的概念感到困惑.据我所知,您无法在JAVA中实例化接口,因此以下语句会产生编译错误

     ActionListener action = new ActionListener();  // compile error
Run Code Online (Sandbox Code Playgroud)

但是当它处理匿名类时会发生什么?为什么允许使用新的?例如

     JButton button = new JButton("A");
     button.addActionListener(new ActionListener(){    //this is fine
           @Override
           public void actionPerformed(ActionEvent e){

           }
     };
Run Code Online (Sandbox Code Playgroud)

编译器只是ActionListener在场景后面创建一个类并实现吗?它是如何工作的 ?

java interface anonymous-function inner-classes

1
推荐指数
1
解决办法
815
查看次数

从文本文件中读取巨大的字符串

我有一个大文本文件,但没有任何换行符.它只包含一个长字符串(1个字符串的大字符串,包含所有ASCII字符),但到目前为止一切正常,因为我可以用Java读取整行到内存中,但我想知道是否有内存泄漏问题,因为文件变得像5GB +那么大,并且程序无法立即将整个文件读入内存,所以在这种情况下读取此类文件的最佳方法是什么?我们可以将这条巨大的线分成两部分甚至多块吗?

这是我如何阅读文件

   BufferedReader buf = new BufferedReader(new FileReader("input.txt"));
   String line;
   while((line = buf.readLine()) != null){

   }
Run Code Online (Sandbox Code Playgroud)

java string io memory-leaks

1
推荐指数
1
解决办法
7830
查看次数

Java中Trie数据结构空间的使用

我只想仔细检查在最坏的情况下Trie数据结构可能具有的总空间。我以为会是O(N * K),其中N是节点总数,K是字母表的大小(指向其他尝试),但是人们一直告诉我这是O(K ^ L),其中K是字母的大小和L是平均单词长度,但是那些空指针会占用Java中的存储空间吗?例如,如果节点之一只有总大小为K的3个分支/点,它是否使用K空间?还是只有3个?以下是Trie的实现Java

class Trie {
     private Trie [] tries;

     public Trie () {
          // A size 256 array of Trie, and they are all null
          this.tries = new Trie[256]; // K = 256;
     }
}
Run Code Online (Sandbox Code Playgroud)

java algorithm trie space-complexity

1
推荐指数
1
解决办法
1824
查看次数

在上下文切换上刷新 TLB

这可能取决于操作系统,但一般来说,据我所知,当发生页面错误(所需的页面不在主内存中)时,操作系统将指示CPU从磁盘读取页面,我想知道操作系统是否分派到另一个处理磁盘 I/O 时?如果确实如此,那么上下文切换时将完全刷新 TLB,对吗?

operating-system context-switch virtual-memory tlb page-tables

1
推荐指数
1
解决办法
7027
查看次数