Colin Anacoura
Colin Anacoura

Reputation: 21

How remove all the longest the strings in a vector of strings

// this program prints all the shortest elements in a vector of String

I want this program to print all the shortest strings in the vector. The output should be hello,test,JAVA. The last word is not being removed. Can anybody help, because the condition is clear, I think. If it's the same length, it should be removed.

import java.util.Vector;

class printAllSmallInVectorOfString
{
    public static void main(String[] args)
    {
        Vector<String> v=new Vector();

        v.addElement("hello");
        v.addElement("test");
        v.addElement("phytons");
        v.addElement("JAVA");
        v.addElement("Program");
        v.addElement("ultrons");


        String largest="";  // define largest

        for(int i=0 ; i < v.size() ; i++)
        {
            // checks if first element is longer
            if( largest.length() <  v.elementAt(i).length())
            {
                // largest stores the string
                largest=v.elementAt(i);
            }
            // condition for elements of same length
            else if(v.elementAt(i).length() == largest.length())   
            {
                System.out.println("The largest element so far is " + largest + " at index " + v.indexOf(largest ));
                System.out.println(v.elementAt(i) + " at index " + v.indexOf(v.elementAt(i)) + " is the same length as the largest element " + largest + "\n");
                // removes second element which is equal with longest
                 v.removeElementAt(i);
            }


        }
        // removes longest element
        v.removeElementAt(v.indexOf(largest));

         for(String z : v)
         System.out.println(z);


    }
}

Upvotes: 0

Views: 304

Answers (2)

Steffen
Steffen

Reputation: 4239

Here is a simple solution without any Java 8 features.

public static void main(String[] args) {
    Vector<String> v = new Vector();

    v.addElement("hello");
    v.addElement("test");
    v.addElement("phytons");
    v.addElement("JAVA");
    v.addElement("Program");
    v.addElement("ultrons");

    System.out.println(keepShortest(v));
}

public static List<String> keepShortest(List<String> strings) {
    // Find length of shortest string
    int shortest = Integer.MAX_VALUE;
    for (String string : strings) {
        if (string.length() < shortest) {
            shortest = string.length();
        }
    }

    // Populate new list with shortest strings
    List<String> newList = new ArrayList<>();
    for (String string : strings) {
        if (string.length() == shortest) {
            newList.add(string);
        }
    }

    return newList;
}

As I've said above, you need to:

  • Find the length of the shortest string first
  • Then remove all strings which are longer; or populate a new list with all strings of equal length

Upvotes: 1

Boris the Spider
Boris the Spider

Reputation: 61128

I would do it thusly:

public static void main(String[] args) throws Exception {
    final Collection<String> strings = Arrays.asList(
            "hello",
            "test",
            "phytons",
            "JAVA",
            "Program",
            "ultrons"
    );

    final int min = strings.stream()
            .mapToInt(String::length)
            .min()
            .getAsInt();

    final Collection<String> shortest = strings.stream()
            .filter(s -> s.length() == min)
            .collect(toList());

    shortest.forEach(System.out::println);
}

First, don't use Vector.

Now, get the minimum length of of the String in the Collection.

Then take the Collection and filter for String of that length.

Finally, loop over and print then contents of the Collection.

Upvotes: 1

Related Questions