Hot questions for Removing the Data in Guava

Top 10 Java Open Source / Guava / Removing the Data

Remove duplicates from List using Guava

Question: How can we remove duplicates from List with the help of Guava api?

Currently I am following this:

private List<T> removeDuplicate(List<T> list){
return new ArrayList<T>(new LinkedHashSet<T>(list));
}

Answer: Probably the most efficient way is ImmutableSet.copyOf(list).asList(), which eliminates duplicates and preserves iteration order.

But unfortunately in the real world, you often encounter situations where null does occur.

Here's a slightly longer null-safe version:

ImmutableSet.copyOf(
    Iterables.filter(
        list, Predicates.not(Predicates.isNull()))).asList();

Or, with static imports:

copyOf(filter(list, notNull())).asList();

How to remove control characters from java string?

Question: I have a string coming from UI that may contains control characters, and I want to remove all control characters except carriage returns, line feeds, and tabs.

Right now I can find two way to remove all control characters:

1- using guava:

return CharMatcher.JAVA_ISO_CONTROL.removeFrom(string);

2- using regex:

return string.replaceAll("\\p{Cntrl}", "");

Answer: You can do something like this if you want to delete all characters in other or control uni-code category

System.out.println(
    "a\u0000b\u0007c\u008fd".replaceAll("\\p{Cc}", "")
); // abcd

This actually removes (among others) '\u008f' Unicode character from the string, not the escaped form "%8F" string.


Guava - How to remove from a list, based on a predicate, keeping track of what was removed?

Question: I have an ArrayList to be filtered, and various Guava Predicates to filter it with. This list will have only 50-100 elements.

I was planning on Iterables.removeIf using each predicate in turn. It is perhaps not maximally efficient but never mind (at least removeIf has some optimization for RandomAccess lists)

For debugging, I want to concisely log what each predicate did. e.g.

Pred0 removed [a, c, g]
Pred1 removed []
Pred2 removed [b, f]

There are some obvious hack solutions but what would you suggest as the cleanest?

For bonus points, it should be reasonably efficient too.

Answer: I would capture the removed elements in your Predicate code.

List<String> removedElements = Lists.newArrayList();
final Iterables.removeIf(list, new Predicate<String>() {
    @Override
    public boolean apply(String input) {
        if ("a".equals(input)) {
            removedElements.add(input);
            return true;
        }
        return false;
    }
}); 

Add and remove from MAP with limited size

Question: I want a limited size map with some duplicated keys. When size is reached I want delete the oldest entry.

for example, this data set:

MAX_SIZE=5;
map.put(100,"OLDEST");
map.put(101,"XXXX");
map.put(101,"YYYY");
map.put(102,"ZZZZ");
map.put(103,"GGGG");

Then I want to insert a new entry in the map

myLength = map.size()
if(myLength>=MAX_SIZE){
   map.remove(the OLDEST)
}    
map.put(105,"NEW")

I was thinking in guava multimap, but how delete the oldest entry in multimap?

They KEY is a Long, maybe I need do a for? (not efficient)

oldest=MAX_LONG
for(Long key:map){
     if(key<oldest){
           oldest=key
      }
}
map.remove(oldest)

Answer: Use a LinkedListMultimap: it preserves the insertion order, so removing the oldest entry is just a matter of removing the first element of the list returned by entries()