标签: java-8

JAVA-8中堆上的字符串对象数

从这个堆栈溢出的字符串对象数量,我开始知道如果我们做一些像:

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个对象.

java string heap-dump java-8

1
推荐指数
2
解决办法
553
查看次数

使用java 8 lambda将map转换为字符串

我想将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)

当然,看起来很难看,特别是我必须对逗号进行后处理.想知道是否有更清洁的方法吗?

注意:我不需要代码审查,只需要知道一种不同但更清晰的方式来编写同样的东西,如果可能的话

java lambda java-8

1
推荐指数
1
解决办法
2104
查看次数

如何将具有列表中相同值的两个字段的对象组合到一个简化列表中?

我有这个类的对象列表:

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)

我希望将其合并到此列表中: …

java java-8

1
推荐指数
1
解决办法
72
查看次数

将Scala Future转换为CompletableFuture

我在我的项目中有一个返回a的Akka层Scala Future,接收Future的部分是Java味道.

团队中的人不了解Scala,他们宁愿使用,CompletableFuture因为他们更了解Java 8 API.

有没有什么好方法可以将a变换Scala futureCompletableFuture

显然是以非阻塞的方式.

问候.

java scala java-8

1
推荐指数
1
解决办法
1304
查看次数

如何将String []转换为Map,并将数组索引转换为java lambda中的求和值?

我有以下内容

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)

arrays algorithm lambda hashmap java-8

1
推荐指数
1
解决办法
332
查看次数

Custom Comparator.仅按Java 8中的字段进行比较

我想在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)

java compare compareto comparator java-8

1
推荐指数
1
解决办法
813
查看次数

不使用加'+'组合字符串

我现在正在编写单元测试,我需要创建一个特定的字符串.

我现在定义了这样的东西:

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)

弦乐与弦乐的结合看起来非常丑陋和业余.有可能以某种方式更有效地使用其他任何东西吗?比如模式?我不知道.感谢帮助 :)

java string java-8

1
推荐指数
3
解决办法
144
查看次数

Java 8:从Long生成字符串ID

下面是我用来生成一个字符串引用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进一步优化?

java string groovy java-8

1
推荐指数
1
解决办法
129
查看次数

Java 8使用com.google.common.collect.Iterables.partition循环的方式?

看起来像这应该是Stream.of一些简单的使用方式,但....

这是我想要改进的代码(myEntryIdsLong几千个项目的长度列表):

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 guava java-8

1
推荐指数
1
解决办法
183
查看次数

从java.util.function.Function获取方法名称

是否可以获取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)

java lambda java-8

1
推荐指数
1
解决办法
567
查看次数