.profile, .bashrc 차이 및 실행순서

.bash_profile, .bashrc 차이

둘의 차이는 실행이 되는 시점이 다르다.

.bash_profile 의 경우, 로그인 되는시점에 실행
.bashrc 의 경우, 이미 로그인한 시점에서 새로운 콘솔창(세션)을 열때 실행.


리눅스의 bash의 .bash_profile, .bashrc 실행순서

OS에서는 /etc/profile, .bash_profile or .profile을 실행하고, 각 파일 내부에서 다른 파일을 실행한다.

실행순서(내부 shell script포함) : /etc/profile -> /etc/profile.d/*.sh -> .bash_profile OR .profile -> .bashrc -> /etc/bashrc

1. /etc/profile

해당파일 안의 shell script
for i in /etc/profile.d/*.sh ; do

    if [ -r "$i" ]; then

        if [ "${-#*i}" != "$-" ]; then

            . "$i"

        else

            . "$i" >/dev/null 2>&1

        fi

    fi

done


2. .bash_profile OR .profile (.bash_profile 이 없으면 .profile을 실행. .bash_profile 이 1순위이고, 우선순위에 따라 1개만 실행된다.)

해당 파일안의 shell script
if [ -f ~/.bashrc ]; then

        . ~/.bashrc

fi




[JAVA] 람다식 기본 예제 2 (sorted)

JDK 라이브러리의 코드에서 정렬, 리버싱은 많이 사용된다.
정렬을 위해 리스트에 대해 sort() 메서드를 사용한다. 이 메서드는 void메서드 이기 때문에 원본 리스트를 보존하기 위해서 복사본을 만들어 두어야 하고 원본이 아닌 복사복에 대해서 sort() 메서드는 호출해야한다.
이젠 stream 의 sorted()를 사용하면 번거러운 작업을 피할 수 있다.

예제소스1은 하나의 값으로 정렬.
예제소스2는 복수의 값을 비교하여 정렬

예제소스1

public class Person {

	  private final String name;
	  private final int age;

	  public Person(final String theName, final int theAge) {
	    name = theName;
	    age = theAge;
	  } 

	  public String getName() { return name; }
	  public int getAge() { return age; }

	  public String toString() {
	    return String.format("%s - %d", name, age);
	  }

} 


final List<Person> people = Arrays.asList( new Person("Zohn", 25), new Person("Sara", 21), new Person("Jane", 21), new Person("Greg", 35)); //java7 sort 사용 Collections.sort(people, new Comparator<Person>() { public int compare(Person o1, Person o2) { return o1.getName().compareTo(o2.getName()); } }); Collections.sort(people, new Comparator<Person>() { public int compare(Person o1, Person o2) { return o1.getName().compareTo(o2.getName()); } }); System.out.println("sort 이름순 정렬 "+people); //java8 sorted 사용 List<Person> people1= people.stream() .sorted(Comparator.comparing((Person person) -> person.getAge())).collect(Collectors.toList()); System.out.println("sorted 나이순 정렬 "+people1); List<Person> people2= people.stream() .sorted(Comparator.comparing((Person person) -> person.getName())).collect(Collectors.toList()); System.out.println("sotred 이름순 정렬 "+people2); }


출력

sort 이름순 정렬 [Greg - 35, Jane - 21, Sara - 21, Zohn - 25]

sorted 나이순 정렬 [Jane - 21, Sara - 21, Zohn - 25, Greg - 35]

sotred 이름순 정렬 [Greg - 35, Jane - 21, Sara - 21, Zohn - 25]


예제소스2
정렬 조건 : 우선노출여부가 Y인게 1순위, 영역코드 2순위, 노출순위 3순위로 정렬.
복수의 조건으로 정렬할때는 .thenComparing 을 사용한다.

public class Person {

	    public Person(String prioYn, String expsFdC, String prioOr)
	    {
	        this.prioYn = prioYn;				// 우선노출여부
	        this.expsFdC = expsFdC;				// 영역코드
	        this.prioOr = prioOr;				// 노출순위
	    }

	    /**
	     * The name of the person.
	     */
	    public String prioYn;
	    public String expsFdC;
	    public String prioOr;
		public String getPrioYn() {
			return prioYn;
		}
		public void setPrioYn(String prioYn) {
			this.prioYn = prioYn;
		}
		public String getExpsFdC() {
			return expsFdC;
		}
		public void setExpsFdC(String expsFdC) {
			this.expsFdC = expsFdC;
		}
		public String getPrioOr() {
			return prioOr;
		}
		public void setPrioOr(String prioOr) {
			this.prioOr = prioOr;
		}
		@Override
		public String toString() {
			return "Person [prioYn=" + prioYn + ", expsFdC=" + expsFdC + ", prioOr=" + prioOr + "]";
		}
	    
	}


	    List<Person> peopleList = new ArrayList<>();
	    peopleList.add(new Person("Y", "B", "1"));
	    peopleList.add(new Person("N", "B", "0"));
	    peopleList.add(new Person("Y", "B", "3"));
	    peopleList.add(new Person("Y", "B", "2"));
	    peopleList.add(new Person("", "A", ""));	    
	    peopleList.add(new Person("", "A", ""));
	    
	    for(Person personVO : peopleList) {
	    	System.out.println(personVO.toString());
	    }
	    System.out.println("---------------------------------------------------------");
	    
	    List<Person> peopleSortedList = new ArrayList<>();
	    peopleSortedList = peopleList.stream().map(p -> {        // expsFdC(영역코드)가 A인것은 우선노출여부가 없으므로 N으로 set
	    	Person obj = p;
	    	if("A".equals(obj.getExpsFdC())){
	    		obj.setPrioYn("N");
	    	}
	    	return obj;
	    }).sorted(Comparator.comparing(Person::getPrioYn).reversed()
				.thenComparing(Comparator.comparing(Person::getExpsFdC))
				.thenComparing(Comparator.comparing(Person::getPrioOr)))
	    		.collect(Collectors.toList());
	    
	    System.out.println("peopleSortedList");
	    for(Person personVO : peopleSortedList) {
	    	System.out.println(personVO.toString());
	    }
	}

출력

Person [prioYn=Y, expsFdC=B, prioOr=1]

Person [prioYn=N, expsFdC=B, prioOr=0]

Person [prioYn=Y, expsFdC=B, prioOr=3]

Person [prioYn=Y, expsFdC=B, prioOr=2]

Person [prioYn=, expsFdC=A, prioOr=]

Person [prioYn=, expsFdC=A, prioOr=]

---------------------------------------------------------

peopleSortedList

Person [prioYn=Y, expsFdC=B, prioOr=1]

Person [prioYn=Y, expsFdC=B, prioOr=2]

Person [prioYn=Y, expsFdC=B, prioOr=3]

Person [prioYn=N, expsFdC=A, prioOr=]

Person [prioYn=N, expsFdC=A, prioOr=]

Person [prioYn=N, expsFdC=B, prioOr=0]




[JAVA] 람다식 기본 예제 1 (map, filter, reduce, collect)

자바 8 에서 람다식이 나오면서 stream 인터페이스가 나왔습니다. stream 인터페이스를 사용하여 람다식을 기존 JAVA코드(명령형 스타일)와 비교해보겠습니다.

아래에 소개하는 4개의 메서드를 간단히 설명하면 
map()은 엘리먼트 변경, filter()는 엘리먼트 선택, reduce(), collect()는 엘리먼트를 하나로 리턴 이다.

1. map()
map 메서드는 입력 컬렉션을 출력 컬렉션으로 매핑하거나 변경할때 유용하다.

예제 코드
list의 엘리먼트 값을 모두 대문자로 변경하여 출력.

final List<String> names = Arrays.asList("Sehoon", "Songwoo", "Chan", "Youngsuk", "Dajung");
			//java 7
			System.out.println("java 7");
			for(String name : names) {
				System.out.println(name.toUpperCase());
			}

			System.out.println("");

			//java 8 Lambda
			System.out.println("java 8");
			names.stream()
				.map(name -> name.toUpperCase())
				.forEach(name -> System.out.println(name));


java 7
SEHOON
SONGWOO
CHAN
YOUNGSUK
DAJUNG

java 8
SEHOON
SONGWOO
CHAN
YOUNGSUK
DAJUNG

 

출력


2. filter()
filter 메서드는 컬렉션을 조건에 의한 선택을 할때 유용하다. filter 메서드는
boolean 결과를 리턴하는 람다표현식이 필요하다.
예제의 collection 메서드는 filter 표현식에 나온값을 list로 변경한다. 

예제 코드
'S' 로 시작하는 이름을 출력.

		final Listt<String> names = Arrays.asList("Sehoon", "Songwoo", "Chan", "Youngsuk", "Dajung");

		//java 7
		System.out.println("java 7");
		final List startsWithN1 = new ArrayList();
		for (String name : names) {
			if (name.startsWith("S")) {
				startsWithN1.add(name);
			}
		}

		System.out.println(startsWithN1);

		System.out.println("");

		//java 8 Lambda
		System.out.println("java 8");
		final List startsWithN2 =  
				names.stream().filter(name -> name.startsWith("S"))
								.collect(Collectors.toList());

		System.out.println(startsWithN2); 


출력

java 7
[Sehoon, Songwoo]

java 8
[Sehoon, Songwoo]



3. reduce()
reduce 메서드는 엘리먼트를 비교하고 컬렉션에서 하나의 값으로 연산한다.
람다 예제 소스를 보면 첫번째로 리스트에 있는 처음 두개 엘리먼트를 사용한다. 그리고 람다 표현식의 결과는 다음호출에 사용된다. 두번째 호출에서는 name1은 이전 호출의 결과이며 name2는 컬렉션의 세번째 엘리먼트 이다.

예제코드
특정 스트링값의 길이보다 크고, 리스트의 가장 긴이름을 가진 엘리먼트를 출력.

		final List<String> names = Arrays.asList("Sehoon", "Songwoo", "Chan", "Youngsuk", "Dajung");

		//java 7
		String LongerEliment1  = "";
		for (String name : names) {
			if(("hoone".length() <= name.length()) && (LongerEliment1.length() <= name.length())) {
				LongerEliment1 = name;
			}
		}
		
		System.out.println("java 7 "+LongerEliment1);

		//java 8 Lambda
		String LongerEliment2 = names.stream()
				.reduce("hoone", (name1, name2) -> 
					name1.length() >= name2.length() ? name1 : name2);
		System.out.println("java 8 "+LongerEliment2);

출력

java 7 Youngsuk
java 8 Youngsuk


4. collect()
collect 메서드는 reduce() 메서드와 동일하게 값을 하나로 모으는 다른형태인데, collect는 여러 convenience method를 제공한다.
아래 예제는 리스트의 엘리먼트를 콤마로 구분하여 출력하는데, 기존 for문으로는 마지막 엘리먼트에 콤마를 안붙이는게 쉽지는 않다. 하지만 collect 머서드를 사용하면 간단하게 만들 수 있다.

예제코드
리스트의 엘리먼트를 콤마로 구분하여 출력. 단 마지막 엘리먼트에 콤마가 없어야한다.

 		final List<String> names = Arrays.asList("Sehoon", "Songwoo", "Chan", "Youngsuk", "Dajung");

		System.out.println("java 7");
		//java 7
		for(int i = 0; i < names.size() - 1; i++) {
			System.out.print(names.get(i).toUpperCase() + ", ");
		}
		
		if(names.size() > 0) { 
			System.out.println(names.get(names.size() - 1).toUpperCase());
		}

		System.out.println("java 8");
		//java 8 Lambda
		System.out.println(names.stream()
					.map(String::toUpperCase)
					.collect(Collectors.joining(", ")));


출력

java 7
SEHOON, SONGWOO, CHAN, YOUNGSUK, DAJUNG

java 8
SEHOON, SONGWOO, CHAN, YOUNGSUK, DAJUNG