Sunday, November 24, 2019

How To Convert A Listing To A Railroad Train Inwards Coffee Amongst Example

How to convert a List to Set inwards Java
Many times nosotros postulate to convert ane collection to approximately other e.g. converting a List to a Set. It'an besides an tardily agency to re-create contents from ane collection to other inwards Java similar from List to Set or Set to List. Thanks to Collection framework inwards Java, copying collection from ane to other is extremely easy. Since every Collection course of written report implements Collection interface which defines addAll() method, which tin post away locomote used to exercise a collection from contents of approximately other collection. In this Java tutorial, nosotros volition run into an instance of how to re-create contents from List to Set inwards Java equally purpose of copying information from ane collection to other inwards Java. This is an truly nice agency to convert List to Set inwards Java. You tin post away utilisation this technique to convert whatsoever List implementation to Set inwards Java.


Java Program to convert List to Set

Many times nosotros postulate to convert ane collection to approximately other e How to Convert a List to a Set inwards Java amongst ExampleIn this example, nosotros volition re-create objects from ArrayList to HashSet inwards Java. This instance is simply a demonstration yesteryear using this technique you lot tin post away re-create elements from ane 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 instance to convert a List to a Set inwards Java./
 * It besides shows agency to re-create ane collection to another
 * Collection inwards Java

 * You tin post away re-create information from List to Set using addAll()
 * method or you lot tin post away exercise re-create of collection
 * yesteryear passing approximately other Collection similar ArrayList
 * to its constructor patch 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 inwards Java,
     // recall this volition take away duplicates from
     // collection because List supports duplicates but


     //Set does non back upwards duplicates inwards 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 as well as Set volition impress same values because at that spot are no duplicates. The size of both List as well as Set volition besides locomote same. The entirely companionship of both collections volition locomote dissimilar because Set doesn't guarantee whatsoever ordering patch List keeps elements inwards the companionship they are inserted into List inwards Java, that’s why List is besides called equally ordered collection patch Set is known equally a unordered collection inwards Java. Now you lot should non forget how to re-create objects from ane collection to approximately other inwards Java, including List to Set or Set to List, simply utilisation addAll() or overstep the collection to constructor patch creating a copy.

Here is a prissy summary of divergence betwixt List as well as Set collection types for your quick reference:

Many times nosotros postulate to convert ane collection to approximately other e How to Convert a List to a Set inwards Java amongst Example


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

Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures as well as Algorithms: Deep Dive Using Java

No comments:

Post a Comment