我需要在列表上执行Calendar alarm = Calendar.getInstance();
Calendar copy = (Calendar) alarm.clone();
alarm.set(Calendar.YEAR, year1);
alarm.set(Calendar.MONTH, month1);
alarm.set(Calendar.DAY_OF_MONTH, day1);
alarm.set(Calendar.HOUR_OF_DAY, hour1);
alarm.set(Calendar.MINUTE, min1);
int result = alarm.compareTo(copy);
if (result == 0)
{
Toast.makeText(getActivity(), "You selected current time", Toast.LENGTH_SHORT).show();
}
else if (result < 0)
{
alarm.add(Calendar.DATE, 1);
Toast.makeText(getActivity(), "You selected the past time", Toast.LENGTH_SHORT).show();
}
else
{
Intent myIntent = new Intent(getActivity(), MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, alarm.getTimeInMillis(), pendingIntent);
}
。什么是最快的解决方案?我正在使用reduceByKey
运算符在reduce操作中合并2列表,但是:::
是O(n)所以我担心reduce操作最终会 O(n 2 )。
代码示例:
:::
什么是最好/最有效的解决方案?
答案 0 :(得分:2)
你能做的最好的事情是:
rdd.groupByKey.mapValues(_.flatten.toList)
这将:
如果您想要缩小地图,可以使用aggregateByKey
:
import scala.collection.mutable.ArrayBuffer
rdd.aggregateByKey(ArrayBuffer[Int]())(_ ++= _, _ ++= _).mapValues(_.toList)
但与第一种解决方案相比,它通常会更加昂贵。