如何转换字符串数组为唯一值的数组? [英] How do I convert an array of strings to a array of unique values?

查看:161
本文介绍了如何转换字符串数组为唯一值的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,如何转换我的字符串数组来唯一值的阵列?

如果我有这样的字符串数组:

 字符串[]试验= {1,1,1,2}

和我想落得:

 的String [] = UQ {1,2}


解决方案

我尝试这个页面上所有的答案,没有工作原样。所以,这里是我如何解决它,由来自答案灵感的 Taig akuhn

 进口groovy.io *。
高清常用3 = [5,5,7,6,7,8,0]
清单<串GT; uniqueList =新的ArrayList<串GT;(
         新LinkedHashSet&所述;字符串>(arr.asList())的.sort());
的System.out.println(uniqueList)

In Java, how do I convert an array of strings to a array of unique values?

If I have this array of Strings:

String[] test = {"1","1","1","2"}

And I want to end up with:

String[] uq = {"1","2"}

解决方案

I tried all the answers on this page and none worked as-is. So, here is how I solved it, inspired by the answers from Taig and akuhn :

import groovy.io.*;
def arr = ["5", "5", "7", "6", "7", "8", "0"]
List<String> uniqueList = new ArrayList<String>( 
         new LinkedHashSet<String>( arr.asList() ).sort() );
System.out.println( uniqueList )

这篇关于如何转换字符串数组为唯一值的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆