How to Convert a List to a Set inward Java amongst Example

How to convert a List to Set inward Java
Many times nosotros postulate to convert i collection to roughly other e.g. converting a List to a Set. It'an equally good an slow agency to re-create contents from i collection to other inward Java similar from List to Set or Set to List. Thanks to Collection framework inward Java, copying collection from i to other is extremely easy. Since every Collection shape implements Collection interface which defines addAll() method, which tin forcefulness out hold upwardly used to practise a collection from contents of roughly other collection. In this Java tutorial, nosotros volition encounter an event of how to re-create contents from List to Set inward Java equally purpose of copying information from i collection to other inward Java. This is an genuinely nice agency to convert List to Set inward Java. You tin forcefulness out occupation this technique to convert whatever List implementation to Set inward Java.


Java Program to convert List to Set

Many times nosotros postulate to convert i collection to roughly other e How to Convert a List to a Set inward Java amongst ExampleIn this example, nosotros volition re-create objects from ArrayList to HashSet inward Java. This event is only a demonstration past times using this technique yous tin forcefulness out re-create elements from i collection to other doesn't matter whether it's an ArrayList, LinkedList or HashSet in Java.



package example;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Java plan event to convert a List to a Set inward Java./
 * It equally good shows agency to re-create i collection to another
 * Collection inward Java

 * You tin forcefulness out re-create information from List to Set using addAll()
 * method or yous tin forcefulness out practise re-create of collection
 * past times passing roughly other Collection similar ArrayList
 * to its constructor piece creating.
 *
 * @author Javin Paul
 */

public class ListtoSet{
 
    public static void main(String args[]) throws InterruptedException{            
 
     
     List<String> teams = Arrays.asList("India" , "Australia" , "England", "Pakistan");
   
     System.out.println("Original List: " + teams);
   
     // copying contents from List to Set inward Java,
     // recall this volition take away duplicates from
     // collection because List supports duplicates but


     //Set does non back upwardly duplicates inward Java
   
     Set<String> cricketTeams = new HashSet<String>(teams);
   
     System.out.println("Copy collection :" + cricketTeams);
   
     
    }  
 
}

Output:
Original List: [India, Australia, England, Pakistan]
Copy collection :[Pakistan, England, Australia, India]


In this example, both collection i.e. List too Set volition impress same values because at that spot are no duplicates. The size of both List too Set volition equally good hold upwardly same. The exclusively lodge of both collections volition hold upwardly dissimilar because Set doesn't guarantee whatever ordering piece List keeps elements inward the lodge they are inserted into List inward Java, that’s why List is equally good called equally ordered collection piece Set is known equally a unordered collection inward Java. Now yous should non forget how to re-create objects from i collection to roughly other inward Java, including List to Set or Set to List, only occupation addAll() or transcend the collection to constructor piece creating a copy.

Here is a prissy summary of deviation betwixt List too Set collection types for your quick reference:

Many times nosotros postulate to convert i collection to roughly other e How to Convert a List to a Set inward Java amongst Example


Other Java Collection Tutorials for your reference
  • How to convert an array to String inward Java? (answer)
  • The deviation betwixt ArrayList too HashMap inward Java? (answer)
  • How to convert a listing to an array inward Java? (answer)
  • The deviation betwixt HashSet too TreeSet inward Java? (answer)
  • How to convert an ArrayList to a String inward Java? (answer)
  • How to convert a Map to a List inward Java? (answer)
  • The deviation betwixt ArrayList too HashSet inward Java? (answer)

Further Learning
Java Fundamentals: Collections
From Collections to Streams inward Java 8 Using Lambda Expressions
Grokking Algorithms past times Aditya Bhargava
Java Programming Interview Exposed past times Makham

Subscribe to receive free email updates:

0 Response to "How to Convert a List to a Set inward Java amongst Example"

Posting Komentar