Convert Set to array in Java

This post will discuss how to convert a set to an array in plain Java, Java 8, and the Guava library.

1. Naive solution

A naive solution is to iterate over the given set and copy each encountered element in the Integer array one by one.

2. Using Set.toArray() method

Set interface provides the toArray() method that returns an Object array containing the elements of the set.

The JVM doesn’t know the desired object type, so the toArray() method returns an Object[]. We can pass a typed array to the overloaded toArray(T[] a) method to let JVM know your desired object type.

We can also pass an empty array of the specified type, and JVM will allocate the necessary memory:

3. Using Java 8

In Java 8, we can use the Stream to convert a set to an array. The idea is to convert a given set to stream using Set.stream() method and use Stream.toArray() method to return an array containing the stream elements. There are two ways to do so:

⮚ Using Streams with method reference

⮚ Using Streams with lambda expression

4. Using Guava Library

⮚ Using FluentIterable class:

The FluentIterable is an expanded Iterable API that provides similar functionality as Java 8’s Stream. We can get a fluent iterable that wraps an iterable set and returns an array containing all the elements from the fluent iterable.

⮚ Using Iterables class:

Guava’s Iterables class also provides the toArray() method that copies an iterable’s elements into an array.

That’s all about converting Set to array in Java.

Original: https://www.cnblogs.com/kungfupanda/p/16466032.html
Author: 功夫 熊猫
Title: Convert Set to array in Java

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/537012/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 建造者设计模式

    可以看到这个学生类的属性是非常多的,所以构造方法不是一般的长,如果我们现在直接通过new的方式去创建: 可以看到,我们光是填参数就麻烦,我们还得一个一个对应着去填,一不小心可能就把…

    Java 2023年6月6日
    084
  • Java 基础(类的加载与ClassLoader的理解)

    当程序主动使用某个类时,如果该类还未被加载到内存中,则系统会通过如下三个步骤来对该类进行初始化 加载 将class文件字节码内容加载到内存中,并将这些静态数据转换成方法区的运行时数…

    Java 2023年5月29日
    098
  • mac下安装MYSQL-python

    今天在mac电脑上安装python的mysql时出现如下问题: 该安装时是使用sudo pip install MySQL-python来安装mysql模块,安装没有问题,但是在p…

    Java 2023年6月8日
    085
  • Spring 源码(12)Spring Bean 的创建过程(3)

    继续上一篇 Spring Bean的创建过程的解读,上一篇介绍了 Spring在创建过程中 doGetBean方法,在执行过程中会调用 getSingleton方法并且设置一个 l…

    Java 2023年6月14日
    068
  • 集成 Redis & 异步任务 SpringBoot 2.7 .2实战基础

    SpringBoot 2.7 .2实战基础 – 09 – 集成 Redis & 异步任务 1 集成Redis 《docker 安装 MySQL 和 …

    Java 2023年6月16日
    070
  • []利用memcached在多台服务器之间共享PHP的session数据

    最近我的几篇文章都是是最近项目的一点心得^^这个项目一开始就设计的”很大”,考虑了很多负载均衡方面的东西,比如采用nginx将php的请求分摊到若干个运行p…

    Java 2023年5月30日
    074
  • Java学习 (九)基础篇 包机制&JavaDoc

    包机制 为了更好地组织类,Java提供了包机制,用于区别类名的命名空间 包语句的语法为: package pkg[.pkg2[.pkg3…]]; 一般利用公司域名倒置作为包名;…

    Java 2023年6月8日
    080
  • 若依 前端框架部署 apache 代替nginx的try_file

    ngin是这样的 location / {try_files $uri $uri/ /index.html;index index.html index.htm;} apache是…

    Java 2023年5月30日
    077
  • Linux系统中Redis和Tomcat的PID文件路径设置

    Tomcat: /bin/catalina.sh 文件头注释下面添加一行:CATALINA_PID=/var/run/tomcat.pid Redis: redis.conf配置文…

    Java 2023年6月5日
    0101
  • Buuctf-Web-[极客大挑战 2019]EasySQL

    前言 刷题网址:https://buuoj.cn/challenges 首先在网页测试输入 admin, admin,如下图,发现是GET传输 然后在后面输入一个 ‘,之后发现报错…

    Java 2023年6月13日
    078
  • 防止重复提交解决方案-(基于JAVA注解+AOP切面)

    1、前言 近期在构建项目脚手架时,关于接口幂等性问题,考虑做成独立模块工具放进脚手架中进行通用。如何保证接口幂等性,换句话说就是如何防止接口重复提交。通常,前后端都需要考虑如何实现…

    Java 2023年5月29日
    065
  • linux部署jar包脚本和注册开机启动

    前言 之前开发了windows部署jar包为服务的工具在实际生产环境中使用非常简单方便,公司部分项目是部署在linux系统中的,所以决定整合一个通用的jar包部署脚本,并注册为开机…

    Java 2023年6月15日
    071
  • JAVA入门基础_从零开始的培训_JDBC和数据库连接池_基于MYSQL

    JDBC是什么? JDBC实际上就是为了能够访问不同的数据库,而提供的一套接口规范。 各个数据库厂商实现这套接口规范再提供相对应的jar包让JAVA程序能够操作对应的数据库。 学习…

    Java 2023年6月9日
    069
  • ActiveMQ 即时通讯服务 浅析

    解决方法: 需要用到eclipse的jdt来编译class,不能再使用javac的默认编译方式。 在eclipse或MyEclipse的eclipse/plugin目录中找到org…

    Java 2023年5月29日
    061
  • java.math.BigDecimal

    BigDecimal类位于java.math.BigDecimal包下。使用此类可以完成大的小数操作,而且也可以使用此类进行精确的四舍五入,这一点在开发中经常使用。 对于不需要任何…

    Java 2023年5月29日
    053
  • Java开发笔记(一百四十八)通过JDBC查询数据记录

    前面介绍了通过JDBC如何管理数据库,当时提到Statement专门提供了executeQuery方法用于查询操作,为什么查询操作这么特殊呢?这是因为其它语句跑完一次就了事了,顶多…

    Java 2023年6月6日
    055
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球