Java集合容器优化

public static boolean hasRoleAuth(String roleName) {
   Authentication auth = SecurityContextHolder.getContext().getAuthentication();
      GrantedAuthority[] ga=auth.getAuthorities();
      for (int i = 0; i < ga.length; i++) {
   GrantedAuthority authority = ga[i];
   if(authority.getAuthority().equals(roleName))
      return true;
}
      return false;
  }


public static boolean hasRoleAuth(String roleName) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    for (GrantedAuthority ga : auth.getAuthorities()) {
        if (ga.getAuthority().equals(roleName)) {
            return true;
        }
    }
    return false;
}

---------------------------------------------------------------------------------------

  public static Role getRole(){
   Authentication auth = SecurityContextHolder.getContext().getAuthentication();
      GrantedAuthority[] ga=auth.getAuthorities();

      if(ga.length>0){
       Role role=roleManager.getRoleByName(ga[0].getAuthority());
       return role;
      }

      return null;
  }

public static Role getRole() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    Collection<? extends GrantedAuthority> ga = auth.getAuthorities();
    if (ga.iterator().hasNext()) {
        Role role = roleManager.getRoleByName(ga.iterator().next().getAuthority());
        return role;
    }
    return null;
}
---------------------------------------------------------------------------------------

 public static String[] getRoleNames(){
   Authentication auth = SecurityContextHolder.getContext().getAuthentication();
      GrantedAuthority[] ga=auth.getAuthorities();
      String[] roleNames=new String[ga.length];
      for (int i = 0; i < ga.length; i++) {
   GrantedAuthority authority = ga[i];
   roleNames[i]=authority.getAuthority();
}

      return roleNames;
  }

public static String[] getRoleNames() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    Collection<? extends GrantedAuthority> collection = auth.getAuthorities();
    String[] roleNames = new String[collection.size()];
    int i = 0;
    for (GrantedAuthority g : collection) {
        roleNames[i++] = g.getAuthority();
    }
    return roleNames;
}
### Java 集合容器的使用与原理 #### 什么是Java集合框架? Java集合框架是一个用于表示和操作数据组的标准库,它提供了多种接口和类来存储、检索以及管理一组对象。这些容器可以动态调整大小并提供灵活的数据结构支持[^1]。 #### 主要分类 Java集合主要分为两大类别:Collection 和 Map 接口下的子集。 - **Collection** 是单列数据的顶级父接口,其下又细分为 List, Set 及 Queue。 - **List**: 存储有序可重复元素,典型代表有 ArrayList 和 LinkedList。 ```java List<String> list = new ArrayList<>(); list.add("Element"); System.out.println(list.get(0)); // 输出 "Element" ``` - **Set**: 不允许存储重复元素,常用 HashSet 或 TreeSet 实现去重功能。 ```java Set<Integer> set = new HashSet<>(); set.add(1); set.add(2); boolean isAdded = set.add(1); // 返回 false 表明未成功添加因为已存在该元素 ``` - **Queue**: 提供先进先出(FIFO)队列行为,PriorityQueue 支持优先级排序。 ```java Queue<Character> queue = new PriorityQueue<>(); queue.offer('a'); char firstChar = queue.poll(); // 移除并返回 'a' ``` - **Map** 则用来保存键值对映射关系,其中 HashMap 和 ConcurrentHashMap 是两种重要变体[^2]。 - **HashMap**: 非线程安全的基础哈希表实现,在多线程环境下可能导致数据不一致问题。 ```java Map<String, Integer> map = new HashMap<>(); map.put("key", 100); int value = map.get("key"); // 获取对应 key 的 value 值 ``` - **ConcurrentHashMap**: 线程安全版本适合高并发场景应用。JDK7采用分段锁机制提升效率;而到了JDK8则改用了CAS算法配合红黑树优化内部结构以进一步增强性能表现。 #### 性能考量与最佳实践 当选用具体类型的集合时需考虑实际应用场景的要求如访问速度、内存占用量等因素从而做出合理决策。例如对于频繁读取但很少修改的操作建议使用不可变或者只读视图形式呈现给外部调用者降低潜在风险同时提高运行效能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值