【问题标题】:Java get First and Last duplicate elements of List [closed]Java获取List的第一个和最后一个重复元素[关闭]
【发布时间】:2017-06-07 06:59:10
【问题描述】:


在我的应用程序中,我想从数组中获取第一个和重复的元素 例如,

String[] strings = {"one", "one", "one", "one", "one", "one", "one", "one", "one", "one", "one", "one"
                , "two", "two", "two", "two", "two", "two", "two", "two", "two", "two", "two", "two", "two", "two", "two"
                , "three", "three", "three", "three", "three", "three", "three", "three", "three", "three", "three"
                , "four", "four", "four", "four", "four", "four", "four", "four", "four", "four"
                , "five", "five", "five", "five", "five", "five"
                , "six", "six", "six", "six", "six", "six", "six", "six", "six"
                , "seven", "seven", "seven", "seven", "seven", "seven", "seven", "seven"
                , "eight", "eight", "eight", "eight", "eight", "eight"
                , "nine", "nine", "nine", "nine", "nine", "nine", "nine", "nine", "nine"
                , "ten", "ten", "ten", "ten", "ten", "ten", "ten", "ten", "ten"};

对于上面的列表,我想得到类似的输出

一个
一个
两个
两个
















【问题讨论】:

  • 将所有值映射到 LinkedHashMap,然后将值添加回列表中,只是这次只添加第一个和最后一个重复项。
  • 请向我们展示您到目前为止所做的尝试。我们不为您做您的(家庭)工作。
  • 如果你只对元素感兴趣而不是你的第一个/最后一个元素的索引,基本上你只需将每个唯一元素打印两次,换句话说,这是没有意义的。你到底想达到什么目的?
  • 为什么不是前两个重复元素?还是最后两个?还是中间两个?你怎么知道区别?你的问题毫无意义。

标签: java arraylist


【解决方案1】:

你可以试试这个

public static void main(String[] args) {

    String[] strings = {"one", "one", "one", "one", "one", "one", "one", "one", "one", "one", "one", "one"
            , "two", "two", "two", "two", "two", "two", "two", "two", "two", "two", "two", "two", "two", "two", "two"
            , "three", "three", "three", "three", "three", "three", "three", "three", "three", "three", "three"
            , "four", "four", "four", "four", "four", "four", "four", "four", "four", "four"
            , "five", "five", "five", "five", "five", "five"
            , "six", "six", "six", "six", "six", "six", "six", "six", "six"
            , "seven", "seven", "seven", "seven", "seven", "seven", "seven", "seven"
            , "eight", "eight", "eight", "eight", "eight", "eight"
            , "nine", "nine", "nine", "nine", "nine", "nine", "nine", "nine", "nine"
            , "ten", "ten", "ten", "ten", "ten", "ten", "ten", "ten", "ten"};

    Map<String,Integer> itemMap=new LinkedHashMap<>();
    for (String item:strings){
        if(!itemMap.containsKey(item)){
            itemMap.put(item,1);
        }else{
            itemMap.put(item, itemMap.get(item)+1);
        }
    }

    for(Map.Entry<String, Integer> mapEntry:itemMap.entrySet()){
        if(mapEntry.getValue()>1){
            System.out.println(mapEntry.getKey()+"\n"+mapEntry.getKey());
        }
    }
}

【讨论】:

    【解决方案2】:

    如果你使用的是 Java8,你可以像这样使用 Stream API。

        Stream.of(strings)
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                .entrySet()
                .stream()
                .filter(entry -> entry.getValue() > 1)
                .forEach(entry -> System.out.println(entry.getKey() + "\n" + entry.getKey()));
    

    【讨论】:

      猜你喜欢
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      • 2014-12-19
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 2020-12-18
      相关资源
      最近更新 更多