假设以下是我们的字符串数组-
String arr[] = { "Andy", "John", "Kevin", "Mary", "Ryan" };
现在,将数组转换为使用HashSet进行设置-
Set<String>set = new HashSet<>(Arrays.asList(arr));
以下是在Java中将Array转换为Set的程序-
import java.util.*; public class Demo { public static void main(String[] args) { String arr[] = { "Andy", "John", "Kevin", "Mary", "Ryan" }; System.out.println("Array = "+ Arrays.toString(arr)); Set<String>set = new HashSet<>(Arrays.asList(arr)); System.out.println("Set = " + set); } }
输出结果
Array = [Andy, John, Kevin, Mary, Ryan] Set = [Kevin, Ryan, John, Andy, Mary]