0

I read from .txt file all of the ids and insert these ids into Vector.

            String pathSelectedfile = fileChooser.getSelectedFile().getAbsolutePath();
            File selectedFile = new File(pathSelectedfile);
            Scanner readFile = new Scanner(selectedFile);

            Vector ids=new Vector();
            while (readFile.hasNextLine()) {
                String id= readFile.nextLine();
                ids.addElement(id);
            }

then I want to remove multiple ids in Vector.i can do that by for loop but information is too big.tnx a lot

Ian
  • 391
  • 1
  • 17
  • is my way wrong to read data from text file?i just want to learn how to remove multiple elements from Vector – Serxan Resullu Dec 15 '12 at 08:28
  • 1
    If readFile is a Scanner instance it's correct... Although you should be aware that vector is deprecated. – Thihara Dec 15 '12 at 08:33
  • @SerxanResullu Now this make sense, However please give some input data, expected result. So that you will get appropriate help. – Smit Dec 15 '12 at 08:40
  • @SerxanResullu I apologize for being rude. I don't know what I was thinking. If your problem not get solved then let me know. – Smit Dec 15 '12 at 10:53

4 Answers4

2

Do completely remove the duplicated ids, you could use the following:

        Set<String> ids=new LinkedHashSet<String>();
        Set<String> duplicates=new HashSet<String>();
        while (readFile.hasNextLine()) {
            String id= readFile.nextLine();
            if(!ids.add(id)) {
                duplicates.add(id);
            }
        }
        ids.removeAll(duplicates)

Note that unlike Vector, LinkedHashSet is not synchronized. In most cases this is not a bad thing, but in the case that you actually need it to be synchronized, wrap it using Collections.synchronizedSet()

Gustav Grusell
  • 1,166
  • 6
  • 7
2

To remove multiple values

    Vector vector = new Vector();
    vector.add("value1");
    vector.add("value2");
    vector.add("value3");
    vector.add("value4");
    System.out.println("Size : "+vector.size());
    // to remove single value
    vector.remove("value1");
    System.out.println("Size : "+vector.size());
    Vector itemsToRemove = new Vector();
    itemsToRemove.add("value3");
    itemsToRemove.add("value4");
    //remove multiple values
    vector.removeAll(itemsToRemove);
    System.out.println("Size : "+vector.size());

    //to remove all elements
    vector.removeAllElements();
    // or
    vector.clear();

But instead of using Vector consider to use ArrayList since Vector is obsolete collection. Read this : Why is Java Vector class considered obsolete or deprecated?

Also use generics Like ArrayList<String> idList = new ArrayList() if you store only String elements in list.

If you want to skip duplicates when adding elements in Vector, use the following code

   Vector vector = new Vector() {
        @Override
        public synchronized boolean add(Object e) {
            if(!contains(e)){
                return super.add(e);
            }
            System.out.println("Element " + e +" is duplicate");
            return false ;
        }
    };

But if you want to add only unique elements, use Set

Community
  • 1
  • 1
vels4j
  • 11,208
  • 5
  • 38
  • 63
0

READ the javadoc and pay attention to methods starting with remove http://docs.oracle.com/javase/6/docs/api/java/util/Vector.html. This should be you first approach not SO.

Thihara
  • 7,031
  • 2
  • 29
  • 56
0

If you "want to remove multiple ids in Vector" do the following

ids = new Vector(new HashSet(ids))
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275