Count distinct elements in a Vector : Vector « Collections Data Structure « Java






Count distinct elements in a Vector

  

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Main {
  public static void main(String[] argv) {
    List<String> list = new ArrayList<String>();
    list.add("A");
    list.add("2");
    list.add("c2");
    
    System.out.println(countDistinctElements(list, 0, list.size()));
  }

  static int countDistinctElements(List source, int position, int count){
    List list = source.subList(position, position + count);
    Set set = new HashSet();
    set.addAll(list);
    return set.size();
  }
}

   
    
  








Related examples in the same category

1.Use Vector in java.utilUse Vector in java.util
2.Finding elements in a vectorFinding elements in a vector
3.Serializing a vectorSerializing a vector
4.Save Vector to file
5.Create Vector test
6.Find Vector Find Vector
7.Remove Element in Vector
8.Sequence Sequence
9.Vector Util
10.Iterator out of Vector
11.Copy Elements of One Java Vector to Another Java Vector
12.Copy Elements of Vector to Java ArrayList
13.Create Java ArrayList From Enumeration
14.Find maximum element of Java Vector
15.Find Minimum element of Java Vector
16.Get Enumeration over Java Vector
17.Perform Binary Search on Java Vector
18.Replace All Elements Of Java Vector
19.Replace all occurrences of specified element of Java Vector
20.Reverse order of all elements of Java Vector
21.Shuffle elements of Java Vector
22.Swap elements of Java Vector
23.Sort Java Vector in descending order using comparator
24.Enumerate through a Vector using Java Enumeration
25.Append all elements of other Collection to Java Vector
26.Copy all elements of Java Vector to an Object Array
27.Get Size of Java Vector and loop through the elements
28.Get Sub List of Java Vector Example
29.Insert all elements of other Collection to Specified Index of Java Vector
30.Iterate through elements Java Vector using Iterator
31.Iterate through elements Java Vector using ListIterator
32.Remove all elements from Java Vector
33.Remove an element from specified index of Java Vector
34.Remove specified element from Java Vector
35.Replace an element at specified index of Java Vector
36.Search an element of Java Vector
37.Search an element of Java Vector from specific index
38.Set size of Java Vector Example
39.Sort elements of Java Vector
40.Generic Vector with String
41.Unmodifiable Vector Adapter
42.Adaptive extension of the java.util.Vector class