简体   繁体   English

如何在ArrayList中搜索StringBuffer对象?

[英]How to search for a StringBuffer object in an ArrayList?

Following is the code snippet I am working with. 以下是我正在使用的代码段。

Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
ArrayList<StringBuffer> al = new ArrayList<StringBuffer>();
while (N-- > 0) {
   str = new StringBuffer(sc.next());
   if (al.contains(str)) {
       System.out.println("Duplicate value " + str);
   } else {
       al.add(str);
   }    
}

If the input is: 4 如果输入是:4

abc ABC

fgh FGH

dfg DFG

abc ABC

It is showing blank output when the expected output is: 当预期输出为:时显示空白输出:

Duplicate value abc 重复值abc

Where am I going wrong here? 我在哪里错了?

StringBuffer doesn't override Object 's equals , so when you search if your List contains a certain StringBuffer instance, you are checking if the exact reference appears in the List . StringBuffer没有覆盖Objectequals ,所以当你搜索,如果你的List中包含了一定StringBuffer实例,你如果准确的引用出现在检查List

You could use a HashSet<String> to avoid duplicates, since String overrides equals , and then (if you must) create a List<StringBuffer> from the elements of the HashSet . 您可以使用HashSet<String>来避免重复,因为String覆盖equals ,然后(如果必须)从HashSet的元素创建List<StringBuffer>

BTW, StringBuilder is more efficient than StringBuffer (which should only be used if you plan to access it from multiple threads). 顺便说一句, StringBuilderStringBuffer更有效(只有在计划从多个线程访问它时才能使用它)。

Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
ArrayList<StringBuffer> al = new ArrayList<StringBuffer>();
Set<String> uniques = new HashSet<>();
while (N-- > 0) {
   uniques.add(sc.next());
}
for (String s : uniques)
    al.add (new StringBuffer(s));

If you have to report the duplicates, a small change will be required : 如果您必须报告重复项,则需要进行少量更改:

Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
ArrayList<StringBuffer> al = new ArrayList<StringBuffer>();
Set<String> uniques = new HashSet<>();
while (N-- > 0) {
   String str = sc.next();
   if (!uniques.add(str))
       System.out.println("Duplicate value " + str);
}
for (String s : uniques)
    al.add (new StringBuffer(s));

You can edit your code as below: 您可以按如下方式编辑代码:

Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
ArrayList<StringBuffer> al = new ArrayList<>();
while (N-- > 0) {
    str = new StringBuffer(sc.next());
    //if (al.contains(str)) {
    if (al.stream().anyMatch(stringBuffer -> stringBuffer.toString().equals(str.toString()))) {
        System.out.println("Duplicate value " + str);
    } else {
        al.add(str);
    }

}

In code above 在上面的代码中

if (al.contains(str)) {

Changed to 变成

if (al.stream().anyMatch(stringBuffer -> stringBuffer.toString().equals(str.toString()))) {

I am here posting the workaround to the problem that I was facing. 我在这里将解决方法发布到我面临的问题上。

The actual problem statement is here . 实际问题陈述在这里

The following is the code snippet of one of the ways which I implemented to solve the problem statement, using linkedlist: 以下是我使用linkedlist解决问题陈述的方法之一的代码片段:

import java.util.*;

public class Password {
    public static void main(String [] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        String str;
        LinkedList ll = new LinkedList();
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<N; i++) {
            str = sc.next();
            ll.add(str);
        }
        for (int i=0; i<N; i++) {
            str = (sb.append(ll.get(i))).reverse().toString();
            if (ll.contains(str)) {
                System.out.print(str.length() + " " + str.charAt(str.length()/2)); 
                break;
            }
            sb.setLength(0);
        }
        sc.close(); 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM