How to search for StringBuffer object in ArrayList?

Below is the code snippet I'm 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 input: 4

Abc

Fgh

DFG

Abc

It shows empty output when the expected result:

Duplicate value abc

Where am I wrong here?

+4
source share
3 answers

StringBufferdoes not override Object equals, so when you search if your Listcontains a specific instance StringBuffer, you check to see if the exact link is displayed in List.

You can use HashSet<String>to avoid duplicates, because it Stringoverrides equals, and then (if necessary) create List<StringBuffer>from elements HashSet.

BTW, StringBuilder , StringBuffer ( , ).

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));

, :

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));
+8

, :

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);
    }

}

if (al.contains(str)) {

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

I am posting a workaround here for the problem I am facing.

The actual statement of the problem is here .

Below is a code snippet of one of the methods that I implemented to solve the problem using a linked list:

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(); 
    }
}
0
source

Source: https://habr.com/ru/post/1621082/


All Articles