从这个堆栈溢出的字符串对象数量,我开始知道如果我们做一些像:
String s = new String("ABC");
Run Code Online (Sandbox Code Playgroud)
然后我们objects在堆上有两个String,一个在constant池上"ABC",
但今天我拿了堆转储,发现堆上有两个objects它自己.我使用MAT工具同样请找到下面的屏幕截图.
所以我的查询是,如果堆上有两个对象,其中一个Char[]用于String类,另一个用于常量池,那么这意味着
String s = new String("ABC") 将总共创建3个对象.
我想将map {key:column,key1:column1}转换为csv字符串"key = column,key1 = column".
我正在获取入口映射并从键和值构造字符串.这就是我所拥有的:
entry.forEach(entryVal ->{
result.append(entryVal.getKey() + "=" + entryVal.getValue());
result.append(',');
});
int index = result.lastIndexOf(",");
if(index == result.length()-1){
result.deleteCharAt(index);
return result.toString();
}
Run Code Online (Sandbox Code Playgroud)
当然,看起来很难看,特别是我必须对逗号进行后处理.想知道是否有更清洁的方法吗?
注意:我不需要代码审查,只需要知道一种不同但更清晰的方式来编写同样的东西,如果可能的话
我有这个类的对象列表:
class Item
{
private String name;
private String parentName;
private Integer someValue;
private Double anotherValue;
public Item() {}
//...elided getters/setters...
}
Run Code Online (Sandbox Code Playgroud)
我有一个列表,其中包含以下值:
//PSEUDO CODE (Not JavaScript, but using JSON is easier to follow)
List<Item> items = [
{
"name": "Joe",
"parentName": "Frank",
"someValue": 10,
"anotherValue": 15.0
},
{
"name": "Joe",
"parentName": "Frank",
"someValue": 40,
"anotherValue": 0.5
},
{
"name": "Joe",
"parentName": "Jack",
"someValue": 10,
"anotherValue": 10.0
},
{
"name": "Jeff",
"parentName": "Frank",
"someValue": 10,
"anotherValue": 10.0
}
];
Run Code Online (Sandbox Code Playgroud)
我希望将其合并到此列表中: …
我在我的项目中有一个返回a的Akka层Scala Future,接收Future的部分是Java味道.
团队中的人不了解Scala,他们宁愿使用,CompletableFuture因为他们更了解Java 8 API.
有没有什么好方法可以将a变换Scala future成CompletableFuture?
显然是以非阻塞的方式.
问候.
我有以下内容
new ConstructStateMachine(new String[] {
"a", "b", "c", "aa", "d", "b"
}, 1, 5);
Run Code Online (Sandbox Code Playgroud)
我想将此数组转换为Map<String, Integer>.
这样字符串将数组中的字符串元素作为我的映射中的键,并且该值将作为整数列表作为值的数组的索引.
我还需要保留重复键,但当然这在Map中是不可能的,但解决方案是我们忽略重复键,但我们总结重复键的值为,而不是具有List我们将Integer作为值与总和为重复键的所有值.
假设我们有这个表:
indices | 0 | 1 | 2 | 3 | 4 | 5 |
item | a | b | c | aa | d | b |
value | 1 | 2 | 3 | 4 | 5 | 6 |
Run Code Online (Sandbox Code Playgroud)
所以我们的地图应该保留以下内容:
// pseudo-code
Map<String, Integer> dictionary = new HashMap<>(
("b" => 8) // because "b" appeared in …Run Code Online (Sandbox Code Playgroud) 我想在Java 8中实现一个比较器,只使用Comparator.comparing(....)来比较字段.
我想要实现的功能如下:
List<DocumentLink> documentList = documentLinkService.getDocumentList(baseInstance);
//call of custom comparator for DigitalFileCategory due to compare only by Name
documentList = documentList.stream()
.filter(doc -> category.comp(doc.getDigitalFileCategory()))
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
所以我需要一个布尔值返回值.DigitalFileCategory中的Comp方法:
public boolean comp(Object obj) {
return super.equals(obj) ||
(obj != null &&
getName() != null &&
getName().equals(((DigitalFileCategory) obj).getName()));
}
Run Code Online (Sandbox Code Playgroud)
任何想法,我该怎么做?当我尝试实现Comparator.comparing时,我要求getName为static.
DigitalFileCategory.class
public class DigitalFileCategory extends _Base {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "iddigitalfilecategory", nullable = false)
private Integer iddigitalfilecategory;
@Column(name = "Name", length = 64) …Run Code Online (Sandbox Code Playgroud) 我现在正在编写单元测试,我需要创建一个特定的字符串.
我现在定义了这样的东西:
private final String at = "@:";
private String error, effect, one, two, three, four;
Run Code Online (Sandbox Code Playgroud)
在setUp(@Before)中:
error = RandomStringUtils.randomAlphabetic (3);
one = RandomStringUtils.randomAlphabetic (6);
two = RandomStringUtils.randomAlphabetic (8);
three = RandomStringUtils.randomAlphabetic (2);
four = RandomStringUtils.randomAlphabetic (6);
effect = (error + at + one + at + two + at + three + at + four);
Run Code Online (Sandbox Code Playgroud)
弦乐与弦乐的结合看起来非常丑陋和业余.有可能以某种方式更有效地使用其他任何东西吗?比如模式?我不知道.感谢帮助 :)
下面是我用来生成一个字符串引用id的方法,它的长度为12,以'X'开头,以输入结束number,String的中间用零填充
public String generateRefId(Long number){
int digits = 1 + (int)Math.floor(Math.log10(number));
int length = 11 - digits;
StringBuilder refid = new StringBuilder(12);
refid.append('X');
for(int i= length;i> 0;i--) {
refid.append('0');
}
refid.append(number);
Assert.assertEquals(refid.length(),12);
return refid.toString();
}
Run Code Online (Sandbox Code Playgroud)
以下是用例
Input Output
12345 X00000012345
999999999 X00999999999
Run Code Online (Sandbox Code Playgroud)
上面的方法工作正常,但我想知道上述方法是否可以使用java 8进一步优化?
看起来像这应该是Stream.of一些简单的使用方式,但....
这是我想要改进的代码(myEntryIds是Long几千个项目的长度列表):
List<MyEntityType> results = new ArrayList<>();
// batch up into groups of 1000
for (final List<Long> partitionedEntryIds :
com.google.common.collect.Iterables.partition(myEntryIds, 1000)) {
results.addAll(BeanConverter.convertList(
myJpaRepository.findAll(partitionedEntryIds)));
}
return results;
Run Code Online (Sandbox Code Playgroud) 是否可以获取java.util.function.Function的方法名称.我想每次都记录正在使用的方法的名称.下面的示例打印Lambda对象,但我还没有找到一种简单的方法来获取方法名称:
public class Example {
public static void main(String[] args) {
Example ex = new Example();
ex.callService(Integer::getInteger, "123");
}
private Integer callService(Function<String, Integer> sampleMethod, String input) {
Integer output = sampleMethod.apply(input);
System.out.println("Calling method "+ sampleMethod);
return output;
}
}
Run Code Online (Sandbox Code Playgroud)