草庐IT

new_setting

全部标签

java - return (string expr) 和 return New String(string expr) 有什么区别?

这两种方法有区别吗?publicStringtoString(){returnthis.from.toString()+this.to.toString();}publicStringtoString(){returnnewString(this.from.toString()+this.to.toString());}(当然,假设from.toString()和to.toString()方法返回字符串)。基本上我对Java中的字符串处理感到困惑,因为有时字符串被视为原始类型,即使它们是类实例。 最佳答案 实际没有区别因为您的两个函

java - 无法在内部类中声明 Public static final String s = new String ("123")

我尝试声明一个类,如下所示classOuter{privatefinalclassInner{publicstaticfinalStrings1=newString("123");publicstaticfinalbyte[]bytes=newbyte[]{0x00,0x01};publicstaticfinalStrings2="123";publicstaticfinalbytebyte1=0x02;}}在上面的代码中,s1和bytes无法编译,但s2和byte1可以编译。如果我将整个常量声明放在外部类中,它就可以正常工作。我错过了什么。有帮助吗? 最佳

java - Vaadin 网格表 : How to disable Sort Function and set the color of one column

我在Vaadin中使用Grid表来表示数据。为此,我试图找出以下两个问题:1.)如何关闭每列表头的排序功能2.)如何设置Grid表格中一列的颜色 最佳答案 首先,我找到了Vaadindocs一个开始寻求帮助的好地方。对于练习的其余部分,假设我们有一个包含3个简单列c1、c2和c3的Grid:Gridgrid=newGrid();grid.addColumn("c1",String.class);grid.addColumn("c2",String.class);grid.addColumn("c3",String.class);1.

java - 调用 createEntityManagerFactory 时为 "You cannot set autocommit during a managed transaction"

我正在尝试通过在我的应用程序中分离模式来实现Multi-Tenancy。为此,我有一个Tenant实体,其中包含一个StringschemaName,并且我有一个SingletonStartupEJB在启动时创建EntityManagerFactory的映射;一个工厂分配给每个Tenant。这是我的EJB:@Startup@SingletonpublicclassTenantManagementServiceImplimplementsTenantManagementService{privateMapentityManagerFactoryMap;@PersistenceContex

java - 收集 Set 字段的所有值

我有一个集合,其中有一个Set类型的字段,其中包含一些值。我需要创建一个新集合来收集所有这些值。我想知道这是否可以使用lambda表达式。下面是代码行:SetteacherId=batches.stream().filter(b->!CollectionUtils.isEmpty(b.getTeacherIds())).map(b->b.getTeacherIds()).collect(Collectors.toSet());问题是后映射操作,它包含一组字符串的集合。所以collect操作返回一个Set>但我希望将所有值聚合到一个集合中。 最佳答案

java - 为什么有些类在创建实例时不需要单词 "New"?

我是Java新手。让我感到困惑的一件事是为什么有些类需要new来实例化,而其他一些类不需要new来实例化。比如我在看log4j,它不需要new。//getaloggerinstancenamed"com.foo"Loggerlogger=Logger.getLogger("com.foo");logger.setLevel(Level.INFO);为什么其他一些类需要new?例如,一个Employee类:EmployeeX=newEmployee(John);X.getwork();等等等等为什么我们没有说Loggerlogger=newLogger(...);?以及为什么即使没有ne

java.lang.AssertionError : Content type not set while junit Spring MVC Controller? 错误

我正在使用JUnit来测试我的SpringMVCController。下面是我的方法,它返回一个index.jsp页面并在屏幕上显示HelloWorld-@RequestMapping(value="index",method=RequestMethod.GET)publicHashMaphandleRequest(){HashMapmodel=newHashMap();Stringname="HelloWorld";model.put("greeting",name);returnmodel;}下面是我对上述方法的JUnit测试:publicclassControllerTest{p

java - 为什么Java中的Set数据结构内部使用Map?

我想知道为什么HashSet使用HashMap,TreeSet使用TreeMap,LinkedHashSet在内部使用LinkedHashMap?因为Set只是携带和存储键而不是值,所以使用额外的内存空间不是不经济吗?HashMap的Entry内部类如下classEntryimplementsMap.Entry{finalKkey;Vvalue;Entrynext;finalinthash;.......}对于Set我们真的不需要那个Vvalue变量,对吗?那么在内部使用map对象的好处和主要原因是什么? 最佳答案 更少的代码、更少

java - Collections.synchronizedMap(new LinkedHashMap());没有使 Map 线程安全

我正在使用以下构造来创建线程安全的Map。Collections.synchronizedMap(newLinkedHashMap());尽管我遇到了ConcurrentModificationException错误。 最佳答案 如果没有代码,很难猜测真正的问题是什么,但我的猜测是,您没有使用返回的集合来执行操作。根据javadocInordertoguaranteeserialaccess,itiscriticalthatallaccesstothebackingcollectionisaccomplishedthroughthe

java - 处理 Set<Foo> 的元素并使用流创建 Set<Bar>

我有一个Set的"hostname:port"对,然后我想创建一个Set.我这样试过:SetISAAddresses=StrAddresses.stream().map(addr->newInetSocketAddress(addr.split(":")[0],Integer.parseInt(addr.split(":")[1])));但这会在IntelliJ中产生以下错误:Incompatibletypes.RequiredSetbut'map'wasinferredtoStream:noinstance(s)oftypevariable(s)RexistsothatStreamc