How to use size() in android CopyOnWriteArraySet?

AndroidMobile DevelopmentApps/Applications

<p>&nbsp; &nbsp;Before getting into an example, we should know what CopyOnWriteArraySet is. It is a thread-safe variant of ArrayList and operations add, set, and so on by making a fresh copy of the underlying array.</p><p>This example demonstrates about How to use size() in android CopyOnWriteArraySet</p><p><strong>Step 1</strong> &minus; Create a new project in Android Studio, go to File &rArr; New Project and fill all required details to create a new project.</p><p><strong>Step 2</strong> &minus; Add the following code to res/layout/activity_main.xml.</p><pre class="prettyprint notranslate">&lt;?xml version = &quot;1.0&quot; encoding = &quot;utf-8&quot;?&gt; &lt;LinearLayout xmlns:android = &quot;; &nbsp; &nbsp;xmlns:app = &quot;; &nbsp; &nbsp;xmlns:tools = &quot;; &nbsp; &nbsp;android:layout_width = &quot;match_parent&quot; &nbsp; &nbsp;android:gravity = &quot;center&quot; &nbsp; &nbsp;android:layout_height = &quot;match_parent&quot; &nbsp; &nbsp;tools:context = &quot;.MainActivity&quot; &nbsp; &nbsp;android:orientation = &quot;vertical&quot;&gt; &nbsp; &nbsp;&lt;TextView &nbsp; &nbsp; &nbsp; android:id = &quot;@+id/actionEvent&quot; &nbsp; &nbsp; &nbsp; android:textSize = &quot;40sp&quot; &nbsp; &nbsp; &nbsp; android:layout_marginTop = &quot;30dp&quot; &nbsp; &nbsp; &nbsp; android:layout_width = &quot;wrap_content&quot; &nbsp; &nbsp; &nbsp; android:layout_height = &quot;match_parent&quot; /&gt; &lt;/LinearLayout&gt;</pre><p>In the above code, we have taken a text view to show CopyOnWriteArraySet elements.</p><p><strong>Step 3</strong> &minus; Add the following code to src/</p><pre class="prettyprint notranslate">package com.example.myapplication; import android.os.Build; import android.os.Bundle; import; import; import android.view.View; import android.widget.TextView; import java.util.concurrent.CopyOnWriteArraySet; public class MainActivity extends AppCompatActivity { &nbsp; &nbsp;CopyOnWriteArraySet copyOnWriteArraySet; &nbsp; &nbsp;String head; &nbsp; &nbsp;@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) &nbsp; &nbsp;@Override &nbsp; &nbsp;protected void onCreate(Bundle savedInstanceState) { &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState); &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main); &nbsp; &nbsp; &nbsp; copyOnWriteArraySet = new CopyOnWriteArraySet&lt;String&gt;(); &nbsp; &nbsp; &nbsp; final TextView actionEvent = findViewById(; &nbsp; &nbsp; &nbsp; copyOnWriteArraySet.add(&quot;sai&quot;); &nbsp; &nbsp; &nbsp; copyOnWriteArraySet.add(&quot;ram&quot;); &nbsp; &nbsp; &nbsp; copyOnWriteArraySet.add(&quot;krishna&quot;); &nbsp; &nbsp; &nbsp; copyOnWriteArraySet.add(&quot;prasad&quot;); &nbsp; &nbsp; &nbsp; copyOnWriteArraySet.add(&quot;ram&quot;); &nbsp; &nbsp; &nbsp; actionEvent.setText(&quot;&quot; + copyOnWriteArraySet); &nbsp; &nbsp; &nbsp; actionEvent.setOnClickListener(new View.OnClickListener() { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Override &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public void onClick(View v) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; actionEvent.setText(&quot;&quot;+copyOnWriteArraySet.size()); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp;} }</pre><p>Let&#39;s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from an android studio, open one of your project&#39;s activity files and click Run&nbsp;<img src="" class="fr-fic fr-dii" width="14" height="14">&nbsp;icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen &ndash;</p><p><img src="" class="fr-fic fr-dib" style="width:278px; height:556px; border:1px solid black;" width="278" height="556"></p><p>Now click on text view, it will show result as shown below &ndash;</p><p><img src="" class="fr-fic fr-dib" style="width:278px; height:556px; border:1px solid black;" width="278" height="556"></p><p>Click <a href="/android/projects/how_to_use_size_in_android_copyonwritearrayset/">here</a> to download the project code</p>
Updated on 30-Jul-2019 22:30:25