---
title: "Search for a StringBuffer object in an ArrayList?"  
description: "Search for a StringBuffer object in an ArrayList?"  
author: "Anonymous User"  
published: 2015-12-22  
updated: 2015-12-22  
canonical: https://www.mindstick.com/forum/33775/search-for-a-stringbuffer-object-in-an-arraylist  
category: "java"  
tags: ["java", "array list", "string"]  
reading_time: 2 minutes  

---

# Search for a StringBuffer object in an ArrayList?

Following is the [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) 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](https://www.mindstick.com/forum/159209/how-can-i-read-convert-an-input-stream-into-a-string-in-java) is: 4\

```
abcfghdfgabc
```

\
It is showing blank [output](https://www.mindstick.com/interview/34427/explain-the-output-in-angular) when the expected output is:\
[Duplicate](https://www.mindstick.com/forum/160911/sql-query-to-find-duplicate-records-in-a-table-in-sql-server) [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) abc\
Where am I going [wrong](https://answers.mindstick.com/qa/48468/who-wrote-the-the-wrong-enemy-america-in-afghanistan-2001-2014-and-when) here?

## Replies

### Reply by Anonymous User

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.\
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.\
BTW, StringBuilder is more efficient than StringBuffer (which should only be used if you plan to access it from multiple threads).\

```
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));
```


---

Original Source: https://www.mindstick.com/forum/33775/search-for-a-stringbuffer-object-in-an-arraylist

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
