[java] How to make Java Set?

Can anyone help me? example

  • A {1,2,3}
  • B {1,4,5}

Code snippet:

a.intersect(b).print()
// Result 1 . twin between two object

a.merge(b).print()
// Result 1,2,3,4,5

It is valid if I write code below? If not, which part I have to fix?

public static void main(String[] args) {
    // TODO code application logic here
   Set<Integer> a = new TreeSet<Integer>();
   a.add(1);
   a.add(2);
   a.add(6);
   a.remove(2);
   a.add(1); //gak berpengaruh karena sudah ada yang 1 sebelumnya
   //mengapa begituu ? karena et adalah collection yang tidak dapat memiliki elemen kembar.
   System.out.println("A = " + a);

   Set<Integer> b = new TreeSet<Integer>();
   b.add(2);
   b.add(6);
   b.add(1);
   System.out.println("B = " + b); //hasilnya 1,2,6.
   //loh?? kok bisa ? krn sy pake TreeSet, jadi udah terurut.

   b.retainAll(a); //ini rumus intersect XD
   for (Integer i: b)
   {
       System.out.print(i);
   }
}

}

This question is related to java collections set

The answer is


Like this:

import java.util.*;
Set<Integer> a = new HashSet<Integer>();
a.add( 1);
a.add( 2);
a.add( 3);

Or adding from an Array/ or multiple literals; wrap to a list, first.

Integer[] array = new Integer[]{ 1, 4, 5};
Set<Integer> b = new HashSet<Integer>();
b.addAll( Arrays.asList( b));         // from an array variable
b.addAll( Arrays.asList( 8, 9, 10));  // from literals

To get the intersection:

// copies all from A;  then removes those not in B.
Set<Integer> r = new HashSet( a);
r.retainAll( b);
// and print;   r.toString() implied.
System.out.println("A intersect B="+r);

Hope this answer helps. Vote for it!


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to collections

Kotlin's List missing "add", "remove", Map missing "put", etc? How to unset (remove) a collection element after fetching it? How can I get a List from some class properties with Java 8 Stream? Java 8 stream map to list of keys sorted by values How to convert String into Hashmap in java How can I turn a List of Lists into a List in Java 8? MongoDB Show all contents from all collections Get nth character of a string in Swift programming language Java 8 Distinct by property Is there a typescript List<> and/or Map<> class/library?

Examples related to set

java, get set methods golang why don't we have a set datastructure Simplest way to merge ES6 Maps/Sets? Swift Set to Array JavaScript Array to Set How to sort a HashSet? Python Set Comprehension How to get first item from a java.util.Set? Getting the difference between two sets Python convert set to string and vice versa