小编卢声远*_* Lu的帖子

在Java中过滤java.util.Collection

我编写了一个util类来过滤元素,java.util.Collection如下所示:

public class Util{
  public static <T> void filter(Collection<T> l, Filter<T> filter) {
    Iterator<T> it= l.iterator();
    while(it.hasNext()) {
      if(!filter.match(it.next())) {
        it.remove();
      }
    }
  }
}

public interface Filter<T> {
  public boolean match(T o);
}
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 你认为有必要编写这个方法吗?
  2. 有关方法的任何改进?

java collections

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

如何在Java中控制位数据

我想写入最好的控件中的文件或字符串,这应该通过逐位控制来实现.

例如,' 00101111 '(2Fx)应该写入String.我应该在这里使用哪一堂课?我必须使用字节来表示位吗?

java string bitmap bit

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

SQL中日志记录表的适当查询和索引

假设一个名为'log'的表,其中有大量记录.

应用程序通常通过简单的SQL检索数据:

SELECT * 
FROM log 
WHERE logLevel=2 AND (creationData BETWEEN ? AND ?)
Run Code Online (Sandbox Code Playgroud)

logLevel并且creationData有索引,但记录数量使得检索数据需要更长的时间.

我们如何解决这个问题?

mysql sql sql-server oracle database-agnostic

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

使主线程等待事件

我正在做大学任务(坦率地说).问题是我应该在任何时候运行4个客户端线程(上升数字n).因此,当任何线程终止时,必须生成一个新线程.

  public static void main(String[] args) throws IOException,InterruptedException

   {
     /* some declarations.. */

     ClientThread client=new ClientThread ();

 Runnable intr =client;

 for(count=1;count<=number;count++)

 {
                 /* If 4 client threads has been spawned, wait until 1 of them exits */
           while(Thread.activeCount()<5) 
            ;
            new Thread(intr).start();
   }


       /* Wait until all other threads exits. */
       while(Thread.activeCount()!=1)
        ;   

 System.out.println("\n The sum of factorials is: "+client.getResult());
   }
Run Code Online (Sandbox Code Playgroud)

我想删除忙碌的等待,因为它违背了我的程序的目的.我怎样才能制作主线程wait?(它显示的wait是非静态方法,不能从静态方法调用.)请帮忙.

java multithreading

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

如何在Java中打印拆分结果

如何打印b的内容而不是其内存地址?

public class Testing {
    public static void main (String [] args){
        String a = "A#b#C ";
        String[] b = a.split("#");
        System.out.println(b);
    }
}
Run Code Online (Sandbox Code Playgroud)

java split println

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

为什么人们设计难以编写和部署的EJB2?

许多J2EE开发人员都知道EJB2强制他们编写"无用的"Home接口.此外,应用程序服务器之间的部署XML也不同.

所以我不知道为什么EJB2多年来一直是J2EE规范的一部分?有任何非技术性的利益吗?

java java-ee ejb-2.x

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

不可变的番石榴收藏品的缺陷?

我不确定我理解的不可变集合的缺陷是否正确,所以我在这个答案中列出它们.希望有人在这里纠正我.

a):与Collections.unmodifiableXXX()相比,ImmutableXXX.copyOf()丢失了源集合功能.例如,当将linkedList放入ImmutableList.copyOf()时,ImmutableList不再链接.与基于树的集合相同.

b):人们认为Collections.unmodifiableXXX只使用相同的源集合引用,因此一旦源集合发生更改,Collections.unmodifiableXXX也会更改.但我的解决方案是将源集合包装到临时集合中,该集合将传递给ImmutableXXX.copyOf().见下面的代码:

List<String> l = new ArrayList<String>();
List<String>  unmodifiableList = Collections.unmodifiableList(l);
ImmutableList<String> immutableList= ImmutableList.copyOf(l);
l.add("a");//unmodifiableList is also added "a", immutableList not.

/*My solution as follows:
So unmodifiableList2 is also immutable as ImmutableList.copyOf(l) does*/
List<String> unmodifiableList2= Collections.unmodifiableList(new ArrayList(l));
Run Code Online (Sandbox Code Playgroud)

您对Immutable系列的理解是什么?谢谢!

java collections unmodifiable immutability guava

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

使用Java Generics的2个集合之间的元素差异算术元素

假设我们有两个有序的数字集合.我们想要逐个元素地计算算术差异.我认为我们需要使用数字列表来模拟"有序的数字集合"的概念.问题是没有为Number定义算术差异(如3-2中的平庸' - ').我可以将所有内容都转换为Double,但我宁愿选择一个干净的解决方案.

public static <E extends Number> List<E> listDifferenceElementByElement(List<E> minuend, List<E> subtrahend) throws Exception {
    if (minuend.size() != subtrahend.size()) {
        throw new Exception("Collections must have the same size"); //TODO: better exception handling
    }
    List<E> difference = new ArrayList<E>();
    int i = 0;
    for (E currMinuend : minuend) {
        difference.add(currMinuend-subtrahend.get(i)); //error: The operator - is undefined for the argument type(s) E, E
        i++;
    }
    return difference;
}
Run Code Online (Sandbox Code Playgroud)

任何的想法?

java generics collections list array-difference

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