import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class TestSet {

    public static void main(String[] args) {
        Set set = new HashSet();
        set.add("我是第一个");
        set.add("我是第二个");
        System.out.println(set);
        if(!set.isEmpty()){
            System.out.println(set.iterator().next());//迭代取第一个
            System.out.println(set.toArray()[0]);//变为数组取第一个
         }
         List list=new ArrayList(set);
         System.out.println(list.get(0));//变为list取第一个

    }

}

运行结果

 

Set如何获取第一个元素

 

相关文章:

  • 2021-11-29
  • 2021-10-18
  • 2022-12-23
  • 2021-12-23
  • 2022-12-23
  • 2021-08-28
  • 2022-12-23
猜你喜欢
  • 2021-09-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-10
  • 2022-12-23
相关资源
相似解决方案