我有一个Map<String,String>具有大量键值对.现在我想从中删除选定的键Map.以下代码显示了我为实现这一点所做的工作.
Set keySet = new HashSet(); //I added keys to keySet which I want to remove.
Run Code Online (Sandbox Code Playgroud)
然后 :
Iterator entriesIterator = keySet.iterator();
while (entriesIterator.hasNext()) {
map.remove( entriesIterator.next().toString());
}
Run Code Online (Sandbox Code Playgroud)
这很有效.我只是想知道,实现我的要求会有什么更好的方法?
在Java中,private访问修饰符被认为是安全的,因为它在类之外是不可见的.然后外界也不知道那种方法.
但我认为Java反射可以用来打破这个规则.考虑以下案例:
public class ProtectedPrivacy{
private String getInfo(){
return "confidential";
}
}
Run Code Online (Sandbox Code Playgroud)
现在从另一个班级我将获得信息:
public class BreakPrivacy{
public static void main(String[] args) throws Exception {
ProtectedPrivacy protectedPrivacy = new ProtectedPrivacy();
Method method = protectedPrivacy.getClass().getDeclaredMethod("getInfo", null);
method.setAccessible(true);
Object result = method.invoke(protectedPrivacy);
System.out.println(result.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
此时我只是认为私有方法安全,因为做上面的事情我们必须知道方法名称.但是如果包含由其他人编写的私有方法的类我们没有那些可见性.
但是,由于下面的代码行,我的观点变得无效.
Method method[] = new ProtectedPrivacy().getClass().getDeclaredMethods();
Run Code Online (Sandbox Code Playgroud)
现在这method[]包含了上面需要做的所有事情.我的问题是,有没有办法避免使用Java反射这类事情?
我引用Java文档中的一些观点来澄清我的问题.
选择访问级别的提示:
如果其他程序员使用您的类,您希望确保不会发生滥用错误.访问级别可以帮助您执行此操作.使用对特定成员有意义的最严格的访问级别.除非你有充分的理由不使用私人.
我想以毫秒获得当前的UTC时间.我搜索了谷歌并得到了一些回答,System.currentTimeMillis()确实返回UTC时间.但事实并非如此.如果我这样做:
long t1 = System.currentTimeMillis();
long t2 = new Date().getTime();
long t3 = Calendar.getInstance().getTimeInMillis();
Run Code Online (Sandbox Code Playgroud)
所有三次几乎相同(由于通话,差异以毫秒为单位).
t1 = 1372060916
t2 = 1372060917
t3 = 1372060918
Run Code Online (Sandbox Code Playgroud)
这个时间不是UTC时间,而是我的时区时间.我怎样才能在android中获得当前的UTC时间?
我的目标是使Java对象不可变.我上课了Student.我用以下方式对其进行编码以实现不变性:
public final class Student {
private String name;
private String age;
public Student(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,实现Student课堂不变性的最佳方法是什么?
正如标题所示,我需要创建一个随机的,长度为17个字符的ID.像" AJB53JHS232ERO0H1" 这样的东西.字母和数字的顺序也是随机的.我想创建一个带有字母AZ和'check'变量的数组1-2.在循环中;
Randomize 'check' to 1-2.
If (check == 1) then the character is a letter.
Pick a random index from the letters array.
else
Pick a random number.
Run Code Online (Sandbox Code Playgroud)
但我觉得有一种更简单的方法可以做到这一点.在那儿?
请考虑以下代码.
public class Action {
private static int i=1;
public static void main(String[] args) {
try{
System.out.println(i);
i++;
main(args);
}catch (StackOverflowError e){
System.out.println(i);
i++;
main(args);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了4338正确的价值.捕获StackOverflowError输出后如下连线.
4336
4337
4338 // up to this point out put can understand
433943394339 // 4339 repeating thrice
434043404340
4341
434243424342
434343434343
4344
4345
434643464346
434743474347
4348
434943494349
435043504350
Run Code Online (Sandbox Code Playgroud)
在这里考虑现场演示.它正常工作i=4330.其实这是怎么发生的?
供参考:
我做了以下代码来实现这里发生的事情.
public class Action {
private static int i = 1;
private static …Run Code Online (Sandbox Code Playgroud) 请考虑以下情形.
List<String> list = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
现在我添加了String此列表的值.
我使用以下方法来查看列表中的每个元素.
选项一 - 使用 for-each
for (String i : list) {
System.out.println(i);
}
Run Code Online (Sandbox Code Playgroud)
选项二 - 使用 Iterator
Iterator it=list.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
Run Code Online (Sandbox Code Playgroud)
我只是想知道如果我使用for-each而不是有任何性能优势Iterator.现在在Java中使用Iterator也是一种不好的做法?
public static void main(String args[]) {
myMethod(); // i am calling static method from main()
}
Run Code Online (Sandbox Code Playgroud)
.
public static ? myMethod(){ // ? = what should be the return type
return value;// is String
return index;// is int
}
Run Code Online (Sandbox Code Playgroud)
myMethod()将返回String和int值.所以从main()我拿出这些返回值得出以下解决方案.
创建一个类调用 ReturningValues
public class ReturningValues {
private String value;
private int index;
// getters and setters here
}
Run Code Online (Sandbox Code Playgroud)
并改变myMethod()如下.
public static ReturningValues myMethod() {
ReturningValues rv = new ReturningValues();
rv.setValue("value");
rv.setIndex(12);
return rv;
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,有没有更简单的方法来实现这一点?
在python中你可以计算一个简单的循环,让我们说它600!是一个非常大的数字,但python可以在很短的一秒钟内轻松处理它.即使它超过200数字长.另一方面,在java中你必须使用64位文字(长数据类型).所以机器将返回0.
有没有办法克服这个?
考虑以下课程
public final class Constant {
public static final String USER_NAME="user1";
//more constant here
}
Run Code Online (Sandbox Code Playgroud)
这个类在包B中.
现在我将在包A中使用它.考虑以下两种可以使用的方法.
方法1-使用 import B.Constant
import B.Constant;
public class ValidateUser {
public static void main(String[] args) {
if(Constant.USER_NAME.equals("user1")){
}
}
}
Run Code Online (Sandbox Code Playgroud)
方法2-使用 import static B.Constant.USER_NAME;
import static B.Constant.USER_NAME;
public class ValidateUser {
public static void main(String[] args) {
if(USER_NAME.equals("user1")){
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,在这种情况下,静态导入是否有任何差异或优势?
java ×10
android ×1
datetime ×1
foreach ×1
immutability ×1
import ×1
iterator ×1
map ×1
private ×1
random ×1
reflection ×1
return ×1
return-type ×1
utc ×1