我有一个HashSet,其中我有10000个元素.我想从该HashSet中提取随机的100个元素.所以我认为我可以在套装上使用shuffle,但它不起作用.
Set<String> users = new HashSet<String>();
// for randomness, but this doesn't work
Collections.shuffle(users, new Random(System.nanoTime()));
// and use for loop to get 100 elements
Run Code Online (Sandbox Code Playgroud)
我现在不能使用shuffle,有没有其他最好的方法从Java中获取HashSet中的100个随机元素?
我有一个下面的代码,我们试图从纪元时间以微秒为单位获取当前时间戳,但我们正在使用steady_clock.
inline uint64_t get_timestamp()
{
std::chrono::time_point<std::chrono::steady_clock> ts = std::chrono::steady_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(ts.time_since_epoch()).count();
}
Run Code Online (Sandbox Code Playgroud)
这是否是正确的方法,因为根据我的理解steady_clock,用来衡量时间的流逝,不是为了获得当前的时间?或者我应该如此使用system_clock,如下所示:
inline uint64_t get_timestamp()
{
std::chrono::time_point<std::chrono::system_clock> ts = std::chrono::system_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(ts.time_since_epoch()).count();
}
Run Code Online (Sandbox Code Playgroud)
我只需要使用std::chrono包,因为这是我们所有代码所使用的.
我有一个以毫秒为单位的时间戳,我想将其转换为小时、分钟、秒,但仅采用以下格式:
20hrs 10mins 50sec
Run Code Online (Sandbox Code Playgroud)
我想提出一些更好的读取数据的格式,因此决定使用上面的格式。到目前为止,我正在使用DurationFormatUtilsApache Commons 中的类以这种格式提供结果:HH:mm:ss,SSS
DurationFormatUtils.formatDuration(durationForStep, "HH:mm:ss,SSS")
Run Code Online (Sandbox Code Playgroud)
有什么方法可以DurationFormatUtils只使用类来获得上述格式的结果吗?如果不是,那么我认为手工工作将是我猜的唯一其他选择?
我正在研究一个问题,以找到数组中的最小值和最大值.我有我的下面的程序,每当我运行它,我看到java.lang.StackOverflowError:
public class MinMaxInArray {
public static void main(String[] args) {
int a1[] = { 3, 4, 2, 6, 8, 1, 9, 12, 15, 11 };
Pair result = getMinMax(a1, 0, a1.length - 1);
System.out.println("Min: " + result.min);
System.out.println("Max: " + result.max);
}
public static Pair getMinMax(int[] arr, int low, int high) {
Pair result = new Pair();
Pair left = new Pair();
Pair right = new Pair();
// if there is only one element arr= {1}
if …Run Code Online (Sandbox Code Playgroud) 我在mysql中有一个表,我想改变一个表以允许列为null.
当我在mysql表上描述时,我在mysql工作台中看到了这些东西:
Field Type Null Key Default Extra
Run Code Online (Sandbox Code Playgroud)
我想为特定列设置Null字段YES.这是我正在尝试哪些工作正常,但我需要提供其数据类型,同时将DEFAULT设置为NULL?
ALTER TABLE abc.xyz CHANGE hello hello int(10) unsigned DEFAULT NULL;
ALTER TABLE abc.xyz CHANGE world world int(10) unsigned DEFAULT NULL;
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以选择列名称并将默认设置为NULL而不是使用其数据类型?我不想int(10)在将其默认值设置为NULL时提供.
deviceId我使用下面的代码从 Spring Authentication 类中提取。
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import com.google.common.base.Optional;
public static Optional<String> getId() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (!auth.isAuthenticated()) {
return Optional.absent();
}
// I see a warning as "Type safety: Unchecked cast from Object to Map<String,String>"
Map<String, String> details = (Map<String, String>) auth.getDetails();
return Optional.of(details.get("deviceId"));
}
Run Code Online (Sandbox Code Playgroud)
如何克服此类型安全警告消息?我想避免添加Suprress Warnings标签。
类型安全:未经检查的强制转换从
Object到Map<String,String>
我有JSON响应,我需要检查response字段是否有空值.如果response字段具有空值,那么我需要退出程序.
[
{
"results": {
"response": null,
"type": "ABC"
},
"error": null
}
]
Run Code Online (Sandbox Code Playgroud)
检查这个的最简单方法是什么?我知道的一个选项是将JSON转换为POJO,然后检查响应字段.还有其他方法吗?
我有一个通用记录,如下所示,其中holder是一个值为字符串的地图。
{
"name" : "holder",
"type" : {
"type" : "map",
"values" : "string"
}
}
Run Code Online (Sandbox Code Playgroud)
以下是持有者地图的数据:
"holder": {
"cossn": "0",
"itwrqm": "20003"
}
Run Code Online (Sandbox Code Playgroud)
我想提取字符串和字符串映射中的持有者数据。我不知道我该怎么做?我尝试了两个选项,如下所示:
这个返回对象:
GenericRecord record = decoder.decode(data.value());
Object holder = record.get("holder");
Run Code Online (Sandbox Code Playgroud)
这返回 Map 但我并没有真正看到它是键和值的映射。
GenericRecord record = decoder.decode(data.value());
Map<String, String> holder = (Map<String, String>) record.get("holder");
Run Code Online (Sandbox Code Playgroud)
当我打印时,holder我看到这样的内容,这显然不是键/值的映射。我做错了什么?如何从 GenericRecord 中提取输入的地图?
{cossn=0, itwrqm=200006033213}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下命令通过终端在 linux 框中安装 eclipse,但它不起作用。
wget "http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/luna/SR2/eclipse-jee-luna-SR2-linux-gtk-x86_64.tar.gz&mirror_id=454"
Run Code Online (Sandbox Code Playgroud)
当它被下载时,我看到这个文件名是错误的?
download.php?file=%2Ftechnology%2Fepp%2Fdownloads%2Frelease%2Fluna%2FSR2%2Feclipse-jee-luna-SR2-linux-gtk-x86_64.tar.gz
Run Code Online (Sandbox Code Playgroud)
相反,它应该是 -eclipse-jee-luna-SR2-linux-gtk-x86_64.tar.gz怎么了?
我将文件重命名为正确的名称并尝试解压缩它,但出现如下所示的错误:
tar -xvzf eclipse-jee-luna-SR2-linux-gtk-x86_64.tar.gz
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now
Run Code Online (Sandbox Code Playgroud)
怎么了?
我正在与Kafka合作并尝试通过撰写本文来设置消费者群体.唯一的区别是我创建了自己的抽象类,处理程序使设计更简单.
下面是我的抽象类:
public abstract class Consumer implements Runnable {
private final Properties consumerProps;
private final String consumerName;
public Consumer(String consumerName, Properties consumerProps) {
this.consumerName = consumerName;
this.consumerProps = consumerProps;
}
protected abstract void shutdown();
protected abstract void run(String consumerName, Properties consumerProps);
@Override
public final void run() {
run(consumerName, consumerProps);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我KafkaConsumerA在抽象类之上的内容:
public class KafkaConsumerA extends Consumer {
private KafkaConsumer<byte[], DataHolder> consumer;
public KafkaConsumerA(String consumerName, Properties consumerProps) {
super(consumerName, consumerProps);
}
@Override
public void shutdown() { …Run Code Online (Sandbox Code Playgroud)