Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 属性文件中的唯一字数_Java - Fatal编程技术网

Java 属性文件中的唯一字数

Java 属性文件中的唯一字数,java,Java,在java(java 1.8)中,计算propertyfile中唯一字数(仅值)的最佳方法是什么 例如,条目可以是: key1=This is my value for error {0} key2=This is success message.Great. 输出应为10(包括{0}) 我试过的 property.load(in); Enumeration em = property.keys(); while (em.hasMoreElem

在java(java 1.8)中,计算propertyfile中唯一字数(仅值)的最佳方法是什么 例如,条目可以是:

key1=This is my value for error {0}
key2=This is success message.Great.
输出应为10(包括{0})

我试过的

property.load(in);

            Enumeration em = property.keys();
            while (em.hasMoreElements()) {
                String str = (String) em.nextElement();
                completeString =completeString+property.get(str);

            }
Set<String> myset=new HashSet<>();
        String s[]=completeString.split("[ .]");

        for(int i=1;i<s.length;i++){

                myset.add(s[i]);

         }

         for (String sss: myset){

            System.out.println(sss);

        }
property.load(in);
枚举em=property.keys();
而(em.hasMoreElements()){
String str=(String)em.nextElement();
completeString=completeString+property.get(str);
}
Set myset=newhashset();
字符串s[]=completeString.split(“[.]”);
对于(int i=1;使用的iData:
我使用了一个虚拟的
Properties

Properties prop = new Properties();
prop.put("A", "This is my value for error {0}");
prop.put("B", "This is success message.Great.");

好老爪哇: 使用您使用的相同逻辑,您可以简单地拆分迭代中每个属性的
字符串

Set<String> set = new HashSet<>();

Enumeration em = property.keys();

while (em.hasMoreElements()) {
    String str = (String) em.nextElement();
    for(String s : str.split("[ .]")){
        set.add(s);
    }
}
现在,首先让我们获得我们的值的

您可以选择
列表

(不确定哪个更有效)


然后,您所要做的就是
flatMap
将每个
字符串
拆分为新的

结果将是一个漂亮的
打印如下:

[Great, success, for, This, {0}, is, my, error, message, value]
总结:
Set=
prop.entrySet().stream()
.map(e->(字符串)e.getValue())
.flatMap(Pattern.compile(Pattern)::splitAsStream)
.collect(收集器.toSet());

在这里发布你的代码。实际解决方案有什么问题吗?除了你应该在迭代中填充
HashSet
,而不是包含
completeString
。请发布你的完整代码,并修复格式。他问是否有更好的Java 8解决方案,恕我冒犯,也反对你的帖子这一点很好。但我想流和过滤器可能会有更好的解决方案:)事实上,即使这在Java8中仍然有效;)将添加流解决方案…从我的pov中可以看出,根据Java8和Java7,流提供的代码通常更短。谢谢你,伙计;)在xxxvodnikxxx;)没有冒犯然后检查新的解决方案。不确定这是否真的更好…感谢您的解决方案-Java8提供了更短的时间,但有时看起来很神秘
Stream<String> stream = 
    //Create a `List<Object>` from the enumeration and stream it
    Collections.list(prop.elements()).stream() 
        //Convert in String
        .map(o -> (String)o); 
Stream<String> stream =
    prop.entrySet().stream() //Iterate the Map.Entry<Object,Object>
        .map(e -> (String)e.getValue())
    stream.flatMap(pattern::splitAsStream) //split based on the pattern define and return a new `Stream<String>`
    .collect(Collectors.toSet()); //collect in a `Set<String>`
[Great, success, for, This, {0}, is, my, error, message, value]
Set<String> set = 
        prop.entrySet().stream()
            .map(e -> (String)e.getValue())
            .flatMap(Pattern.compile(pattern)::splitAsStream)
            .collect(Collectors.toSet());