我正在尝试阅读枚举以更好地理解它们.
从枚举的javadocs我得到以下内容:
枚举类型是一种特殊的数据类型,它使变量成为一组预定义的常量.
对我来说,这听起来很像一个final变量.
我有一个HashSet<Foo>.我有一个对象
如果我称它为hashSet.contains(fooInstance)返回仍然false.
它变得非常奇怪的是以下行返回true:
new ArrayList<Foo>(hashSet).contains(fooInstance)
Run Code Online (Sandbox Code Playgroud)
遗憾的是,事实证明,.contains()实现的差异究竟在哪里,比预期更难.但我想我会是安全的,因为.equals()和.hashCode()做工精细.
我想对Objects进行更深入的String检查,以便能够执行以下操作:
List<MyObj> myList = new ArrayList<MyObj>() {{
add(new MyObj("hello"));
add(new MyObj("world"));
}};
System.out.println(myList.contains("hello")); // true
System.out.println(myList.contains("foo")); // false
System.out.println(myList.contains("world")); // true
Run Code Online (Sandbox Code Playgroud)
但是使用以下完整代码在每个上面都会出错
/* Name of the class has to be "Main" only if the class is public. */
class Ideone {
public static class MyObj {
private String str;
private int hashCode;
public MyObj(String str) {
this.str = str;
}
public @Override boolean equals(Object obj) {
System.out.println("MyObj.equals(Object)");
if (obj == this) {
return true;
}
if (obj instanceof String) { …Run Code Online (Sandbox Code Playgroud) 有没有办法在每次调用 bean 的方法之前调用一个方法?
我在春天使用硒和黄瓜。由于这个原因,所有页面都是单例,并且因为我使用 @FindBy 注释,所以每次使用页面时我都想调用 PageFactory.initElements(driver, object) 。
使用标准的页面对象模式,这将通过在构造函数中调用它来完成。
我想避免的是在每个方法中指定这样的方法:
public void clickThis() {
PageFactory.initElements(driver, this)
...
}
public void clickThat() {
PageFactory.initElements(driver, this)
...
}
Run Code Online (Sandbox Code Playgroud)
我不想返回新页面,因为它们将无法在功能之间共享。
我有添加,更新和删除实体的通用方法.但我也想使用泛型来检索数据.
这是我获取数据的方法:
public static List<ClassA> getAllClassAData() {
Session session = HibernateUtil.getSessionFactory().openSession();
Criteria criteria = session.createCriteria(ClassA.class);
return criteria.list();
}
public static List<ClassB> getAllClassBData() {
Session session = HibernateUtil.getSessionFactory().openSession();
Criteria criteria = session.createCriteria(ClassB.class);
return criteria.list();
}
public static List<ClassC> getAllClassCData() {
Session session = HibernateUtil.getSessionFactory().openSession();
Criteria criteria = session.createCriteria(ClassC.class);
return criteria.list();
}
Run Code Online (Sandbox Code Playgroud)
我试着做这样的事情:
public static <T> List<T> getAllData() {
Session session = HibernateUtil.getSessionFactory().openSession();
Criteria criteria = session.createCriteria(???.class); //Here is where I'm stuck
return criteria.list();
}
Run Code Online (Sandbox Code Playgroud)
我坚持将类作为arg传递给标准创建:
Criteria criteria = session.createCriteria(Generic.class);
Run Code Online (Sandbox Code Playgroud)
如何让JVM知道我在标准创建过程中想要传递的类?
谁能解释这种奇怪的行为String?
这是我的代码:
String s2 = "hello";
String s4 = "hello"+"hello";
String s6 = "hello"+ s2;
System.out.println("hellohello" == s4);
System.out.println("hellohello" == s6);
System.out.println(s4);
System.out.println(s6);
Run Code Online (Sandbox Code Playgroud)
输出是:
true
false
hellohello
hellohello
Run Code Online (Sandbox Code Playgroud) 想象一下,我有一个定义a的抽象类Generic Type.所有子类都将实现此通用类型.
我可以通过声明一个强制子类返回该类型的抽象方法来实现.但有没有更优雅的方法直接从子类的'Class'对象定义实现这一点?
public class GenericTest {
public static void main(String[]args) throws Exception{
Class strClass = ClazzImplString.class;
Class intClass = ClazzImplInteger.class;
Class implTypeStr = ((AbsClazz)strClass.getConstructor().newInstance()).getGenericType();
Class implTypeInt = ((AbsClazz)intClass.getConstructor().newInstance()).getGenericType();
System.out.println("implTypeStr: " + implTypeStr);
System.out.println("implTypeInt: " + implTypeInt);
}
}
abstract class AbsClazz<GenericType> {
abstract Class getGenericType();
}
class ClazzImplString extends AbsClazz<String> {
public ClazzImplString() {}
@Override Class getGenericType() {return String.class;}
}
class ClazzImplInteger extends AbsClazz<Integer> {
public ClazzImplInteger() {}
@Override Class getGenericType() {return Integer.class;}
}
Run Code Online (Sandbox Code Playgroud)
先感谢您
String result = service.getResult();
if (result == null) {
DaoObject obj = crudRepository.findOne(..);
if (obj != null) {
result = obj.getContent();
} else {
throw NotFoundException();
}
}
service.process(result);
Run Code Online (Sandbox Code Playgroud)
如果DaoObject是一个Optional<DaoObject>,我可以做些什么来实现与上面使用java 8相同?
有些东西.orElseThrow(() -> new NotFoundException());,但上面的代码将如何与流完全一致?
问题:我应该使用() -> new NotFoundException()或NotFoundException::new?
我试图在java中替换正则表达式时遇到错误.
例如:
String h = "{hiren:}";
h=h.replaceAll(":}", ":\"\"}");
Run Code Online (Sandbox Code Playgroud)
请给我解决方案.谢谢
分析以下代码:
class Test {
public static void main(String[] args) {
int[] x = {1, 2, 3, 4};
int[] y = x;
x = new int[2];
for (int i = 0; i < y.length; i++) {
System.out.print(y[i] + " ");
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下代码的答案是A.但为什么答案不是B?