小编chr*_*.fy的帖子

Java 8流组条目通过数字邻近度

我有一个unix时间戳列表,例如:

[1111111 1200000 1200060 1200120 1200180 1300000 1400000 140060]
Run Code Online (Sandbox Code Playgroud)

我希望按照彼此60秒内的簇对它们进行分组,其中键是第一个时间戳,例如:

{1111111=[1111111], 1200000=[1200000,120060, 1200120], 1300000=[1300000], 1400060=[1400000, 1400060]} 
Run Code Online (Sandbox Code Playgroud)

我通过使用for循环实现了这一点,我希望有一种更好的方法,最好使用Java 8流.

(我对Java不太好,所以如果没有办法使用流,那么构造for循环是否更好?)

List <Integer> timestamps = new ArrayList<Integer>();
timestamps.add(1111111);
timestamps.add(1200000);
timestamps.add(1200060);
timestamps.add(1200120);
timestamps.add(1200180);
timestamps.add(1300000);
timestamps.add(1400000);
timestamps.add(1400060);

HashMap <Integer, List <Integer>> grouped = new HashMap<Integer, List <Integer>>();

List <Integer> subList = new ArrayList<Integer>();

for (int i = 0; i < timestamps.size(); i++) {
  if(i > 0 && (timestamps.get(i - 1) + 60 < timestamps.get(i))) {
        grouped.put(subList.get(0), new ArrayList <Integer>(subList));
        subList.removeAll(subList);
   }
   subList.add(timestamps.get(i));  
}
grouped.put(subList.get(0), …
Run Code Online (Sandbox Code Playgroud)

java java-8 java-stream

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

为textView链接findViewById()时未定义的方法setText()

我想知道是否有人能告诉我原因:

TextView textblock = (TextView) findViewById(R.id.label).setText("Google is your friend.", TextView.BufferType.EDITABLE);
Run Code Online (Sandbox Code Playgroud)

我得到一个未定义的方法错误(对于这种类型的视图,setText未定义).但是,当我不链接时,例如:

TextView textblock = (TextView)findViewById(R.id.label);
textblock.setText("Google is your friend.", TextView.BufferType.EDITABLE);
Run Code Online (Sandbox Code Playgroud)

(我知道这是一个非常基本的问题,但我是Java的新手,在我的搜索中找不到任何内容)

java android

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

标签 统计

java ×2

android ×1

java-8 ×1

java-stream ×1