Deleting Duplicate Element Using HashSet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.*;
 
public class Main {
 
    public static void main(String args[]){
 
    String[] fav = {"red","red","apple""apple""hallo"};
 
    Set<String> mySet  = new HashSet<String>();
    Collections.addAll(mySet,fav);
 
       System.out.println(mySet);
    }
}
cs

result / 결과

1
[red, apple, hallo]
cs

Java String Instance Practice Example #1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package edu.handong.csee.java.string; 
 
public class FamilyNamePrinter{
    private String[] stringNames; 
    private String[] familyNames;
     
    public void printFamilyNames(){
        for(String name: familyNames){
            if(name == null){
                System.out.print(""); 
            }else{
                System.out.println(name);
            }
        }
    }
    public FamilyNamePrinter(String names){
        
       stringNames = names.split(",");
       familyNames = new String[stringNames.length];
       int i=0
       for(String name: stringNames)
         familyNames[i++= name.trim().split(" ")[1]; 
    }
 
    public static void main (String[] args) {
        String myString = args[0]; 
        FamilyNamePrinter myFamily = new FamilyNamePrinter(myString); 
        myFamily.printFamilyNames();     
    }
}
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.ArrayList;
 
public class Test {
    public static void main(String[] args) {
        ArrayList<String> pitches = new ArrayList<String>();
        pitches.add("138");
        pitches.add("129");
        pitches.add("142");
 
        System.out.println(pitches.get(1));
        System.out.println(pitches.size());
        System.out.println(pitches.contains("142"));
 
        System.out.println(pitches.remove("129"));
        System.out.println(pitches.size());
        System.out.println(pitches.remove(0));
    }
}
cs

import java.util.Scanner;


public class Tax{

  public static void main(String[] args){

    int tax_commission = 1500;

    Scanner input = new Scanner(system.in);

    System.out.print("What do you do for your living? : ");

    String job = input.nextString();

    System.out.print("How much you get paid per month? : ");

    int salary = input.nextInt();

    

    System.out.println("Since your job is  " + job + " and get paid like " + salary +" this much, the tax you have pay for this year is : " + (commission + salary * 0.13) );

  }

}



import java.util.Scanner;
public class Car{

  

  int speedlimit;

  String color;


  public void toString(){

    System.out.println("my car color is : " + color + " and speed limit is : " + speedlimit );

  }

  public void int setSpeedLimit(int speedlimit){

    this.speedlimit = speedlimit;

  }

  public void getSpeedLimit(){

    return speed;

  }

  public void string setColor(String color){

    this.color = color;

  }

  public void getColor(){

    return color;

  }

  

  public static void main(String[] args){

    Scanner input = new Scanner(System.in);

    

    String color;

    int speedlimit;

    color = input.nextString();

    speedlimit = input.nextInt();

    

    setSpeedLimit(speedlimit);

    getSpeedLimit();

    

    setColor(color);

    getColor();

    toString();

  }

}

+ Recent posts