Antoine
Antoine

Reputation: 75

Remove number from list

I want to remove the value 255 from my list, like if a or b or c or d equals to 255 I want to exclude it from the list. I tried this //list.removeAll (Collections.singletonList ("255")); but it doesn't work . When I compare the list's values with collection.max(list) I want to exclude 255 if one of values is equal to it.

List<Float> list = new ArrayList<Float>();

list.add(a);
list.add(b);
list.add(c);
list.add(d);

if ( a>=15||b>=15||c>=15||d>=15) {
    if ( Collections.max(list) == a) {
        Defauts_detecteur.setText("Défauts récurrents constatés sur le détecteur 1");
    }else if (Collections.max(list)==b)
    {
        Defauts_detecteur.setText("Défauts récurrents constatés sur le détecteur 2");}
    else if (Collections.max(list)==c)
    {
        Defauts_detecteur.setText("Défauts récurrents constatés sur le détecteur 3");}
    else
    {
        Defauts_detecteur.setText("Défauts récurrents constatés sur le détecteur 4");}

    Conseil_detecteur.setText("--> Par mesure de sécurité, nous vous conseillons vivement de vérifier que le détecteur est bien collé au produit à sécuriser.\nPour une adhésion optimale, remplacez l’adhésif.\nVérifiez que le détecteur est bien connecté à la centrale et qu’il est en bon état.");

}

Upvotes: 2

Views: 551

Answers (2)

Saad Sahibjan
Saad Sahibjan

Reputation: 300

Created ArrayList is type of Float. So the below snippet will delete all the occurences of 255f

list.removeAll(Collections.singleton(255f));

Below is the code snippet I tried by myself

List<Float> list = new ArrayList<Float>();  
list.add(1.0f);  
list.add(2.0f);  
list.add(255.0f);  
list.add(3f);  
list.add(255f);  
System.out.println(list.toString());  
//output [1.0, 2.0, 255.0, 3.0, 255.0]
list.removeAll(Collections.singleton(255f));  
System.out.println(list.toString());  
//output [1.0, 2.0, 3.0]

Upvotes: 0

rzwitserloot
rzwitserloot

Reputation: 103813

The major problem (I'm guessing a little bit) is that you tried .remove(255) and it did not work.

That's because of an unfortunate quirk. This code seems extremely simple:

float a = 5;

List<Float> list = new ArrayList<Float>();
list.add(a);
list.remove(5.0);
// or even
list.remove(5);

The above code is broken. It compiles fine. It runs fine (well, the last one throws at runtime with a bizarre error message if you don't follow what is going on). And nevertheless, your list still contains 5.0 once it's ran. ??? What is going on here??

What is going on?

There are 2 list.remove methods: One removes by value (you pass the value: The list will search all of the things it contains for that value and removes it), the other is by index: Remove the 9th value. They are different by way of argument type: The 'remove by value' one takes an Object, the 'remove by index' one an int. That's how java knows which one you want: What is the type of thing you pass to remove? Is it int, or Object?

When you write .remove(5), you are removing by index. It gets complicated when you have a list of numbers, .remove(5) is ambiguous: Do you mean: Remove the 5th number, or do you mean: Remove the number 5?

This confusion explains why .remove(5) doesn't get the job done. Java always interprets that as: Remove by index. Even for a List<Integer>, it would do that.

Then, there's autoboxing. Java needs to convert your primitive to an Object in order to call the remove-by-value variant. 5.0 is converted to a Double. Which is also an object. It seems weird, but you can ask a list of Cats to remove a certain Dog: The list will just go: Oookay. And do nothing. It doesn't throw an exception, and it lets you write it. Contrast to add: You can't write List<Cat> cats = ...; cats.add(new Dog()). But you can write cats.remove(new Dog()) just fine, under the theory that asking a list to remove something that isn't even in it, is fine, and is done by not doing anything.

That explains why list.remove(5.0) compiles, runs, and does nothing: It is asking to remove the Double with value 5.0, which your list doesn't have. It has a Float with value 5.0 and java does not treat those as the same.

You're doing .remove("5") which is even weirder: That is the string "5" which is utterly unrelated to the double or the float 5. Java does not try to conflate different concepts representing the same thing ("5", 5F, 5D and 5 are all different things).

How to fix it

Stop using float. Use double instead. There is almost never a good reason for floats: The fact that they are smaller is rarely going to actually happen (64-bit architecture being what it is), and if it does, it is extremely rare you're in the tiny bandwidth where the savings of a float is worth it, but rewriting the code to use an algorithm that avoids floating point altogether is not. Double is a first class citizen in java, float is not. For example, 255.0 is a double. Not a float.

Next, dance around the fact that .remove is a bit daft. Cast to object to ensure it:

List<Integer> x = new ArrayList<Integer>();
list.add(2, 1, 0);
list.remove(0); // this removes the 2!!! Because that is the item at index 0
list.remove((Object) 0); // this really does remove the 0.
System.out.println(list);
> [1]

Finally, ensure the things you try to remove match what is in your list. If you have a list of floats with 255 in it, that'd be 255F. Not 255, not 255.0 (which is a double). 255D and 255.0 are the same, though:

List<Double> list = new ArrayList<Double>();
list.add(255.0);
list.remove(255.0); // this works
list.remove(255); // this doesn't
list.remove((Object) 255); // this removes nothing! 255 isn't the same as 255.0.

Upvotes: 4

Related Questions