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)

大家都在看

  • Spring Boot 面试问题

    说一说你对Spring Boot的理解 名词解释: Spring Boot 基于 Spring 开发, Spirng Boot 本身并 不提供 Spring 框架的核心特性以及扩展…

    Java 2023年6月7日
    064
  • BeanFactory与FactoryBean有什么区别?

    相同点:都是用来创建bean对象的 不同点:使用beanFactory创建对象的时候,必须要遵循严格的生命周期流程,太复杂了,如果想要简单的自定义某个对象的创建,同时创建好的对象想…

    Java 2023年6月8日
    042
  • Docker 核心知识回顾

    Docker 核心知识回顾 最近公司为了提高项目治理能力、提升开发效率,将之前的CICD项目扩展成 devops进行项目管理。开发人员需要对自己的负责的项目进行流水线的部署,包括写…

    Java 2023年6月7日
    084
  • 大小端 与本机大小端模式判断 面试被问到

    大小端定义 Java代码判断 思路:利用强制类型转换,获取大范围数的一部分数据,再分析获得的数据 int big = 0xFFFF0000; //0xFFFF0000&#x…

    Java 2023年6月5日
    079
  • 22.1.26 递归改动态规划

    22.1.26 递归改动态规划 1.套路: 1)递归:根据题目写出递归版本; 2)记忆化搜索:用某种结构存储已经计算过的信息,省去重复计算的过程; 3)严格表结构:将递归套路转化为…

    Java 2023年6月13日
    069
  • Java给PDF上水印

    我下载了个作为示例的PDF文件: 代码 利用PDFBox <dependency> <groupId>org.apache.pdfboxgroupId&gt…

    Java 2023年5月29日
    068
  • Spring自带的工具类,判空优雅写法

    最近发现同事写了不少重复的工具类,发现其中很多功能,Spring 自带的都有。于是整理了本文,希望能够帮助到大家! import org.springframework.util….

    Java 2023年5月30日
    0116
  • javacv 图片合成

    java;gutter:false; /*<em> * 图片组合 </em>/ @Slf4j public class ComposeImageUtil {…

    Java 2023年5月29日
    060
  • JAVA:聊聊JAVA基础(2)- 基本语法(待续)

    Java中的注释 单行注释 //&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5355;&#x884C;&a…

    Java 2023年6月5日
    067
  • 虚拟机克隆以后出现“需要整合虚拟机磁盘”的解决方法

    问题描述 在虚拟机克隆完毕以后,原始虚拟机提示”需要整合虚拟机磁盘” 在”任务与事件”栏中看到以下信息 ; 解决方法 从上面可以看到…

    Java 2023年5月30日
    082
  • Android学习笔记——Android签名机制详解

    Android签名机制详解 近期由于工作需要在学习 Android 的签名机制,因为没有现成资料,只能通过开发者文档和阅读博客的方式对 Android 签名机制进行大致了解。过程中…

    Java 2023年6月8日
    062
  • dubbo源码分析5(dubbo服务暴露入口)

    经过了前面这么多的铺垫,没错,前面说了这么一大堆的都是铺垫,我们说了spi,以及基于spring的扩展,这一篇就开始说说dubbo吧! 1.dubbo的配置文件解析 在第一篇的时候…

    Java 2023年6月6日
    078
  • 第1章 Java程序设计概述

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    Java 2023年6月7日
    071
  • (Java初学篇)IDEA项目新建流程和软件配置优化以及怎么彻底删除项目

    相信很多小伙伴们在初学 Java 时都会出现这样的情况,就是在网上一顿搜索加捣鼓终于把 JDK 和IDEA 这两款软件安装配置好,但是发现面对这个陌生的软件此时却无从下手,那么接下…

    Java 2023年6月15日
    0114
  • 源码中的设计模式–模板方法模式

    本文要解决的几个问题, 1、什么是模板方法模式; 2、模板方法模式的使用场景; 3、模板方法模式的优点; 4、源码中有哪些地方使用到了模板方法模式; 带着这几个问题,我们开始今天的…

    Java 2023年6月9日
    066
  • 【设计模式】Java设计模式-组合模式

    Java设计模式 – 组合模式 😄 不断学习才是王道🔥 继续踏上学习之路,学之分享笔记👊 总有一天我也能像各位大佬一样🏆原创作品,更多关注我CSDN: 一个有梦有戏的人…

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