有没有办法在java泛型中强制类型参数来实现equals方法?例如,我写了这个类:public class Bag<Item> implements Iterable<Item>它有contains方法,使用Item.equals方法.所以我想确保泛型中传递的Object也将实现该equals方法.
我正在尝试使用 2 种方法来InputStream通过蓝牙套接字进行读取。
第一个是:
InputStream inputStream = bluetoothSocket.getInputStream();
byte[] buffer = new byte[1024];
inputStream.read(buffer);
String clientString = new String(buffer, "UTF-8");
Run Code Online (Sandbox Code Playgroud)
这个问题是,现在clientString有原始消息加上“0”,直到缓冲区已满(如果我将使用第一个字节作为指示符,我可以知道消息的长度,但我尽量不这样做)。
第二个是(使用IOUtils来自 的类Apache Commons IO):
InputStream inputStream = bluetoothSocket.getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, "UTF-8");
String clientString = writer.toString();
Run Code Online (Sandbox Code Playgroud)
这个问题是它一直copy在线并且永远不会超过这一点。
所以,我的问题是,这些方法之间有什么不同,为什么我会得到不同的结果?
客户端的代码是(C# Using 32feet):
client.Connect(BluetoothEndPoint(device.DeviceAddress, mUUID));
bluetoothStream = client.GetStream();
if (client.Connected == true && bluetoothStream != null)
{
byte[] msg = System.Text.Encoding.UTF8.GetBytes(buffer + "\n");
bluetoothStream.Write(msg, 0, msg.Length); …Run Code Online (Sandbox Code Playgroud) 我想找到给定集合的所有子集.我得到的字符串集定义如下:HashSet<String> L我想在循环中使用它的所有子集:for each
做一点事.这样做有一个简单的方法,复杂度低吗?
如何按键排序哈希表(语音)我的意思是,如果哈希表中有3个键(被调用%tags)"MWE", "wPrefix", "conjunction",如果我使用常规排序:
foreach $tag (sort keys %tags) {
print "$tag\n";
}
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
MWE
conjunction
wPrefix
Run Code Online (Sandbox Code Playgroud)
但输出应该是:
conjunction
MWE
wPrefix
Run Code Online (Sandbox Code Playgroud)