简体   繁体   English

有没有办法替换列表变量中所有出现的字符串?

[英]Is there a way to replace all occurrences of a string in a variable of a list?

I have a list as follows 我有如下清单

List<Summary> summary;

Summary {
    int key;
    String value1;
    String value2;
}

Is there a way in java (or any other utility library) to replace all occurrences of a String "String1" in the variable value1 without having to loop through the list? Java(或任何其他实用程序库)中是否有一种方法可以替换变量value1中所有出现的字符串“ String1”而不必遍历整个列表?

Looping the list is inevitable so you or some third party library has to do the looping. 循环列表是不可避免的,因此您或某些第三方库必须进行循环。 Is it really that hard to do: 真的很难吗:

for (Summary s : summary)
    if (s.value1.equals("String1"))
        s.value1 = "...";

? :) :)

Maybe you could use a library that allow you use it without loops, the problem is that in the low level the compiler must use something like a loop for do it. 也许您可以使用一个不带循环就可以使用它的库,问题是在低级编译器必须使用类似循环的东西来做它。 I think that direct or indirectly you will use a loop. 我认为您将直接或间接使用循环。

So, for this reason you haven´t any problem if use a loop in your code. 因此,因此,如果在代码中使用循环,则不会有任何问题。

Sorry for my English, I hope you can understand all. 对不起,我的英文,希望您能理解。

You can add a method to find by the object part: 您可以添加一个方法来按对象部分查找:

public class ListOfString {

    public static void main(String[] args) {
        List<Model> models = new ArrayList<>();

        for(int i = 0 ; i < 5; i++) {
            Model model = new Model();
            model.setStr("String"+i);
            models.add(model);
        }

        Model temp = new Model();
        temp.setStr("String1");

        System.out.println(containsObjectPart(temp, models));
    }

    // This method just a prototype, you can modify as you like...
    private static boolean containsObjectPart(Model obj, List<Model> models) {
        for(Model model : models) {
            if(model.getStr().equals(obj.getStr()))
                return true;
        }

        return false;
    }

}

class Model {

    private String str;

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }
}

不,你不能, Loopinvetible 。但是你可以在很多方面优化它,例如selecting loophow do you find the string and replacing it

You could avoid a bit of looping - at the expense of losing your list ordering - by using a TreeSet , with value1 values as keys and the Summary objects as values. 通过使用TreeSet (可以将value1值作为键,将Summary对象作为值),可以避免一些循环-以丢失列表顺序的代价为代价。 Then you can do a binary chop search to find all matching entries. 然后,您可以执行二进制印章搜索以找到所有匹配的条目。 A collection which uses hashing would have similar trade-offs and gains. 使用散列的集合将具有类似的权衡和收益。

Otherwise, as everyone else has said, looping is inevitable. 否则,正如其他人所说的那样,循环是不可避免的。 The only optimisation you could make is counting the entries as they go into the list so you know when you've found them all so you can stop looping. 您唯一可以做的优化就是对进入列表的条目进行计数,以便您在找到所有条目后就知道它们,从而可以停止循环。

But remember, premature optimisation is the root of all evil . 但是请记住, 过早的优化是万恶之源 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM