从 NamingEnumeration 中提取数据

2022-01-17 00:00:00 list ldap java

我的应用程序在 LDAP 服务器中搜索人员.

My application searches an LDAP server for people.

return ldapTemplate.search("", "(objectclass=person)", new AttributesMapper() {
      public Object mapFromAttributes(Attributes attrs) 
                                                     throws NamingException {

        return attrs.get("cn").getAll();
      }
    });

它返回 NamingEnumeration 对象的列表,其中包含向量.每个向量可能包含一个或多个值.我可以通过此代码打印人名

It returns list of NamingEnumeration object, which contains vectors in it. Each vector may contain one or more values. I can print person names by this code

for(NamingEnumeration ne : list){
  while (ne.hasMore()) {
      System.out.println("name is : " + ne.next().toString());
    }
  }

由于我的 ldap 搜索可以包含多个值,因此它们会出现在 NamingEnumeration 对象内的向量中.如何从中获取多个值.

As my ldap search can contain mutiple values so that comes in vector inside NamingEnumeration object. How can I get multiple values out of it.

推荐答案

当你使用 javax.naming.NamingEnumerationjava.util.List 比如这样,

As you are using a java.util.List of javax.naming.NamingEnumeration<java.util.Vector> such as this,

List<NamingEnumeration<Vector>> list

您应该能够在每个 NamingEnumeration 中迭代 Vector:

You should be able to iterate over the Vector in each NamingEnumeration:

for (NamingEnumeration<Vector> ne : list) {
    while (ne.hasMore()) {
        Vector vector = ne.next();
        for (Object object : vector) {
            System.out.println(object);
        }
    }
}

请注意,Vector 被许多人认为是过时的,尽管没有被弃用.此外,封闭的集合可以使用类型参数.如果您有选择,请考虑以下备选方案之一:

Note that Vector is considered by many to be obsolescent, although not deprecated. Also, the enclosed collection could use a type parameter. If you have a choice, consider one of these alternatives:

List<NamingEnumeration<Vector<T>>>
List<NamingEnumeration<List<T>>>

相关文章